keeweb/app/scripts/storage/io-browser-cache.js

138 lines
4.3 KiB
JavaScript
Raw Normal View History

2017-02-19 18:51:52 +01:00
const idb = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB;
2020-06-01 16:53:51 +02:00
const IoBrowserCache = function (config) {
2017-02-19 18:51:52 +01:00
this.db = null;
this.cacheName = config.cacheName;
this.logger = config.logger;
};
2019-09-17 22:17:40 +02:00
Object.assign(IoBrowserCache.prototype, {
2017-02-19 18:51:52 +01:00
initDb(callback) {
if (this.db) {
return callback && callback();
}
try {
const req = idb.open(this.cacheName);
2020-06-01 16:53:51 +02:00
req.onerror = (e) => {
2017-02-19 18:51:52 +01:00
this.logger.error('Error opening indexed db', e);
2019-08-16 23:05:39 +02:00
if (callback) {
callback(e);
}
2017-02-19 18:51:52 +01:00
};
2020-06-01 16:53:51 +02:00
req.onsuccess = (e) => {
2017-02-19 18:51:52 +01:00
this.db = e.target.result;
2019-08-16 23:05:39 +02:00
if (callback) {
callback();
}
2017-02-19 18:51:52 +01:00
};
2020-06-01 16:53:51 +02:00
req.onupgradeneeded = (e) => {
2017-02-19 18:51:52 +01:00
const db = e.target.result;
db.createObjectStore('files');
};
} catch (e) {
this.logger.error('Error opening indexed db', e);
2019-08-16 23:05:39 +02:00
if (callback) {
callback(e);
}
2017-02-19 18:51:52 +01:00
}
},
save(id, data, callback) {
this.logger.debug('Save', id);
2020-06-01 16:53:51 +02:00
this.initDb((err) => {
2017-02-19 18:51:52 +01:00
if (err) {
return callback && callback(err);
}
try {
const ts = this.logger.ts();
2019-08-16 23:05:39 +02:00
const req = this.db
.transaction(['files'], 'readwrite')
.objectStore('files')
.put(data, id);
2017-02-19 18:51:52 +01:00
req.onsuccess = () => {
this.logger.debug('Saved', id, this.logger.ts(ts));
2019-08-16 23:05:39 +02:00
if (callback) {
callback();
}
2017-02-19 18:51:52 +01:00
};
req.onerror = () => {
this.logger.error('Error saving to cache', id, req.error);
2019-08-16 23:05:39 +02:00
if (callback) {
callback(req.error);
}
2017-02-19 18:51:52 +01:00
};
} catch (e) {
this.logger.error('Error saving to cache', id, e);
2019-08-16 23:05:39 +02:00
if (callback) {
callback(e);
}
2017-02-19 18:51:52 +01:00
}
});
},
load(id, callback) {
this.logger.debug('Load', id);
2020-06-01 16:53:51 +02:00
this.initDb((err) => {
2017-02-19 18:51:52 +01:00
if (err) {
return callback && callback(err, null);
}
try {
const ts = this.logger.ts();
2020-06-01 16:53:51 +02:00
const req = this.db.transaction(['files'], 'readonly').objectStore('files').get(id);
2017-02-19 18:51:52 +01:00
req.onsuccess = () => {
this.logger.debug('Loaded', id, this.logger.ts(ts));
2019-08-16 23:05:39 +02:00
if (callback) {
callback(null, req.result);
}
2017-02-19 18:51:52 +01:00
};
req.onerror = () => {
this.logger.error('Error loading from cache', id, req.error);
2019-08-16 23:05:39 +02:00
if (callback) {
callback(req.error);
}
2017-02-19 18:51:52 +01:00
};
} catch (e) {
this.logger.error('Error loading from cache', id, e);
2019-08-16 23:05:39 +02:00
if (callback) {
callback(e, null);
}
2017-02-19 18:51:52 +01:00
}
});
},
remove(id, callback) {
this.logger.debug('Remove', id);
2020-06-01 16:53:51 +02:00
this.initDb((err) => {
2017-02-19 18:51:52 +01:00
if (err) {
return callback && callback(err);
}
try {
const ts = this.logger.ts();
2019-08-16 23:05:39 +02:00
const req = this.db
.transaction(['files'], 'readwrite')
.objectStore('files')
.delete(id);
2017-02-19 18:51:52 +01:00
req.onsuccess = () => {
this.logger.debug('Removed', id, this.logger.ts(ts));
2019-08-16 23:05:39 +02:00
if (callback) {
callback();
}
2017-02-19 18:51:52 +01:00
};
req.onerror = () => {
this.logger.error('Error removing from cache', id, req.error);
2019-08-16 23:05:39 +02:00
if (callback) {
callback(req.error);
}
2017-02-19 18:51:52 +01:00
};
} catch (e) {
this.logger.error('Error removing from cache', id, e);
2019-08-16 23:05:39 +02:00
if (callback) {
callback(e);
}
2017-02-19 18:51:52 +01:00
}
});
}
});
2019-09-15 14:16:32 +02:00
export { IoBrowserCache };