2015-11-07 20:02:45 +01:00
|
|
|
'use strict';
|
|
|
|
|
2017-01-31 07:50:28 +01:00
|
|
|
const StorageBase = require('./storage-base');
|
2015-12-12 09:53:50 +01:00
|
|
|
|
2017-01-31 07:50:28 +01:00
|
|
|
const idb = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB;
|
2015-11-07 20:02:45 +01:00
|
|
|
|
2017-01-31 07:50:28 +01:00
|
|
|
const StorageCache = StorageBase.extend({
|
2015-11-07 20:02:45 +01:00
|
|
|
name: 'cache',
|
|
|
|
enabled: !!idb,
|
2016-03-13 17:08:25 +01:00
|
|
|
system: true,
|
2015-11-07 20:02:45 +01:00
|
|
|
|
|
|
|
db: null,
|
|
|
|
errorOpening: null,
|
|
|
|
|
2016-03-27 09:06:23 +02:00
|
|
|
initDb: function(callback) {
|
2015-11-07 20:02:45 +01:00
|
|
|
if (this.db) {
|
2015-12-07 22:00:44 +01:00
|
|
|
return callback && callback();
|
2015-11-07 20:02:45 +01:00
|
|
|
}
|
|
|
|
try {
|
2017-01-31 07:50:28 +01:00
|
|
|
const req = idb.open('FilesCache');
|
2016-07-17 13:30:38 +02:00
|
|
|
req.onerror = e => {
|
|
|
|
this.logger.error('Error opening indexed db', e);
|
|
|
|
this.errorOpening = e;
|
2015-12-07 22:00:44 +01:00
|
|
|
if (callback) { callback(e); }
|
2015-11-07 20:02:45 +01:00
|
|
|
};
|
2016-07-17 13:30:38 +02:00
|
|
|
req.onsuccess = e => {
|
|
|
|
this.db = e.target.result;
|
2015-12-07 22:00:44 +01:00
|
|
|
if (callback) { callback(); }
|
2015-11-07 20:02:45 +01:00
|
|
|
};
|
2016-07-17 13:30:38 +02:00
|
|
|
req.onupgradeneeded = e => {
|
2017-01-31 07:50:28 +01:00
|
|
|
const db = e.target.result;
|
2015-11-07 20:02:45 +01:00
|
|
|
db.createObjectStore('files');
|
|
|
|
};
|
|
|
|
} catch (e) {
|
2016-07-17 13:30:38 +02:00
|
|
|
this.logger.error('Error opening indexed db', e);
|
2015-12-07 22:00:44 +01:00
|
|
|
if (callback) { callback(e); }
|
2015-11-07 20:02:45 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-03-12 17:49:52 +01:00
|
|
|
save: function(id, opts, data, callback) {
|
2016-07-17 13:30:38 +02:00
|
|
|
this.logger.debug('Save', id);
|
|
|
|
this.initDb(err => {
|
2015-11-07 20:02:45 +01:00
|
|
|
if (err) {
|
2015-12-07 22:00:44 +01:00
|
|
|
return callback && callback(err);
|
2015-11-07 20:02:45 +01:00
|
|
|
}
|
|
|
|
try {
|
2017-01-31 07:50:28 +01:00
|
|
|
const ts = this.logger.ts();
|
|
|
|
const req = this.db.transaction(['files'], 'readwrite').objectStore('files').put(data, id);
|
2016-07-17 13:30:38 +02:00
|
|
|
req.onsuccess = () => {
|
|
|
|
this.logger.debug('Saved', id, this.logger.ts(ts));
|
2015-12-07 22:00:44 +01:00
|
|
|
if (callback) { callback(); }
|
2015-11-07 20:02:45 +01:00
|
|
|
};
|
2016-07-17 13:30:38 +02:00
|
|
|
req.onerror = () => {
|
|
|
|
this.logger.error('Error saving to cache', id, req.error);
|
2015-12-07 22:00:44 +01:00
|
|
|
if (callback) { callback(req.error); }
|
2015-11-07 20:02:45 +01:00
|
|
|
};
|
|
|
|
} catch (e) {
|
2016-07-17 13:30:38 +02:00
|
|
|
this.logger.error('Error saving to cache', id, e);
|
2015-12-07 22:00:44 +01:00
|
|
|
if (callback) { callback(e); }
|
2015-11-07 20:02:45 +01:00
|
|
|
}
|
2016-03-27 09:06:23 +02:00
|
|
|
});
|
2015-11-07 20:02:45 +01:00
|
|
|
},
|
|
|
|
|
2016-03-12 17:49:52 +01:00
|
|
|
load: function(id, opts, callback) {
|
2016-07-17 13:30:38 +02:00
|
|
|
this.logger.debug('Load', id);
|
|
|
|
this.initDb(err => {
|
2015-11-07 20:02:45 +01:00
|
|
|
if (err) {
|
2015-12-07 22:00:44 +01:00
|
|
|
return callback && callback(err, null);
|
2015-11-07 20:02:45 +01:00
|
|
|
}
|
|
|
|
try {
|
2017-01-31 07:50:28 +01:00
|
|
|
const ts = this.logger.ts();
|
|
|
|
const req = this.db.transaction(['files'], 'readonly').objectStore('files').get(id);
|
2016-07-17 13:30:38 +02:00
|
|
|
req.onsuccess = () => {
|
|
|
|
this.logger.debug('Loaded', id, this.logger.ts(ts));
|
2015-12-07 22:00:44 +01:00
|
|
|
if (callback) { callback(null, req.result); }
|
2015-11-07 20:02:45 +01:00
|
|
|
};
|
2016-07-17 13:30:38 +02:00
|
|
|
req.onerror = () => {
|
|
|
|
this.logger.error('Error loading from cache', id, req.error);
|
2015-12-07 22:00:44 +01:00
|
|
|
if (callback) { callback(req.error); }
|
2015-11-07 20:02:45 +01:00
|
|
|
};
|
|
|
|
} catch (e) {
|
2016-07-17 13:30:38 +02:00
|
|
|
this.logger.error('Error loading from cache', id, e);
|
2015-12-07 22:00:44 +01:00
|
|
|
if (callback) { callback(e, null); }
|
2015-11-07 20:02:45 +01:00
|
|
|
}
|
2016-03-27 09:06:23 +02:00
|
|
|
});
|
2015-11-07 20:02:45 +01:00
|
|
|
},
|
|
|
|
|
2016-03-12 17:49:52 +01:00
|
|
|
remove: function(id, opts, callback) {
|
2016-07-17 13:30:38 +02:00
|
|
|
this.logger.debug('Remove', id);
|
|
|
|
this.initDb(err => {
|
2015-11-07 20:02:45 +01:00
|
|
|
if (err) {
|
2015-12-07 22:00:44 +01:00
|
|
|
return callback && callback(err);
|
2015-11-07 20:02:45 +01:00
|
|
|
}
|
|
|
|
try {
|
2017-01-31 07:50:28 +01:00
|
|
|
const ts = this.logger.ts();
|
|
|
|
const req = this.db.transaction(['files'], 'readwrite').objectStore('files').delete(id);
|
2016-07-17 13:30:38 +02:00
|
|
|
req.onsuccess = () => {
|
|
|
|
this.logger.debug('Removed', id, this.logger.ts(ts));
|
2015-12-07 22:00:44 +01:00
|
|
|
if (callback) { callback(); }
|
2015-11-07 20:02:45 +01:00
|
|
|
};
|
2016-07-17 13:30:38 +02:00
|
|
|
req.onerror = () => {
|
|
|
|
this.logger.error('Error removing from cache', id, req.error);
|
2015-12-07 22:00:44 +01:00
|
|
|
if (callback) { callback(req.error); }
|
2015-11-07 20:02:45 +01:00
|
|
|
};
|
2016-07-17 13:30:38 +02:00
|
|
|
} catch (e) {
|
|
|
|
this.logger.error('Error removing from cache', id, e);
|
2015-12-07 22:00:44 +01:00
|
|
|
if (callback) { callback(e); }
|
2015-11-07 20:02:45 +01:00
|
|
|
}
|
2016-03-27 09:06:23 +02:00
|
|
|
});
|
2015-11-07 20:02:45 +01:00
|
|
|
}
|
2016-03-27 09:06:23 +02:00
|
|
|
});
|
2015-11-07 20:02:45 +01:00
|
|
|
|
2016-03-27 09:46:43 +02:00
|
|
|
module.exports = new StorageCache();
|