1
我有一个类,加载indexedDB。在类中的方法可以访问它之前,我需要预先加载indexedDB。目前,我在使用init()
方法之前的任何其他方法没有this.db
初始化。干净的方式来实现异步indexedDB在类
我正在寻找一种更清洁的方式来实现我所拥有的,这绝对不是干的。基本上,每种方法目前都使用下面的相同代码模式来实现。
问题点是:
- 另一方法
init()
的要求,以便正确地处理 索引资料的intialization。 if (!this.db) {
段最终会在稍后重复。
export default class Persist {
constructor(storage) {
if (storage) {
this.storage = storage;
}
else {
throw new Error('A storage object must be passed to new Persist()');
}
}
// needed to ensure that indexedDB is initialized before other methods can access it.
init() {
// initialize indexedDB:
const DBOpenRequest = this.storage.open('db', 1);
DBOpenRequest.onupgradeneeded =() => {
const db = DBOpenRequest.result;
db.createObjectStore('db', { keyPath: 'id', autoIncrement: true });
};
return new Promise((resolve, reject) => {
DBOpenRequest.onerror = event => {
reject(event);
};
DBOpenRequest.onsuccess = event => {
console.log(`IndexedDB successfully opened: ${event.target.result}`);
resolve(event.result);
this.db = DBOpenRequest.result;
};
});
}
toStorage(session) {
if (!this.db) {
return this.init().then(() => {
const db = this.db;
const tx = db.transaction('db', 'readwrite');
const store = tx.objectStore('db');
const putData = store.put(session.toJS());
return new Promise((resolve, reject) => {
putData.onsuccess =() => {
resolve(putData.result);
};
putData.onerror =() => {
reject(putData.error);
};
});
});
}
// basically a repeat of above
const db = this.db;
const tx = db.transaction('db', 'readwrite');
const store = tx.objectStore('db');
const putData = store.put(session.toJS());
return new Promise((resolve, reject) => {
putData.onsuccess =() => {
resolve(putData.result);
};
putData.onerror =() => {
reject(putData.error);
};
});
}
感谢您抽出时间写下Josh。我从你的文章中学到了很多东西!事实上,我终于第一次使用'async/await'了:)。 – mythereal