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