|
|
|
@ -16,6 +16,7 @@ module.exports = () => { |
|
|
|
var defaultStorages = {}; |
|
|
|
const eventStart = 'storage:start'; |
|
|
|
const eventEnd = 'storage:end'; |
|
|
|
const eventError = 'storage:error'; |
|
|
|
|
|
|
|
return { |
|
|
|
/** |
|
|
|
@ -111,15 +112,17 @@ module.exports = () => { |
|
|
|
* @return {this} |
|
|
|
* @example |
|
|
|
* storageManager.add('local2', { |
|
|
|
* load: function(keys, clb) { |
|
|
|
* load: function(keys, clb, clbErr) { |
|
|
|
* var res = {}; |
|
|
|
* for (var i = 0, len = keys.length; i < len; i++){ |
|
|
|
* var v = localStorage.getItem(keys[i]); |
|
|
|
* if(v) res[keys[i]] = v; |
|
|
|
* } |
|
|
|
* clb(res); // might be called inside some async method
|
|
|
|
* // In case of errors...
|
|
|
|
* // clbErr('Went something wrong');
|
|
|
|
* }, |
|
|
|
* store: function(data, clb) { |
|
|
|
* store: function(data, clb, clbErr) { |
|
|
|
* for(var key in data) |
|
|
|
* localStorage.setItem(key, data[key]); |
|
|
|
* clb(); // might be called inside some async method
|
|
|
|
@ -184,10 +187,16 @@ module.exports = () => { |
|
|
|
} |
|
|
|
|
|
|
|
return st |
|
|
|
? st.store(toStore, res => { |
|
|
|
clb && clb(res); |
|
|
|
this.onEnd('store', res); |
|
|
|
}) |
|
|
|
? st.store( |
|
|
|
toStore, |
|
|
|
res => { |
|
|
|
clb && clb(res); |
|
|
|
this.onEnd('store', res); |
|
|
|
}, |
|
|
|
err => { |
|
|
|
this.onError('store', err); |
|
|
|
} |
|
|
|
) |
|
|
|
: null; |
|
|
|
}, |
|
|
|
|
|
|
|
@ -216,17 +225,23 @@ module.exports = () => { |
|
|
|
} |
|
|
|
|
|
|
|
if (st) { |
|
|
|
st.load(keysF, res => { |
|
|
|
// Restore keys name
|
|
|
|
var reg = new RegExp('^' + c.id + ''); |
|
|
|
for (var itemKey in res) { |
|
|
|
var itemKeyR = itemKey.replace(reg, ''); |
|
|
|
result[itemKeyR] = res[itemKey]; |
|
|
|
} |
|
|
|
st.load( |
|
|
|
keysF, |
|
|
|
res => { |
|
|
|
// Restore keys name
|
|
|
|
var reg = new RegExp('^' + c.id + ''); |
|
|
|
for (var itemKey in res) { |
|
|
|
var itemKeyR = itemKey.replace(reg, ''); |
|
|
|
result[itemKeyR] = res[itemKey]; |
|
|
|
} |
|
|
|
|
|
|
|
clb && clb(result); |
|
|
|
this.onEnd('load', result); |
|
|
|
}); |
|
|
|
clb && clb(result); |
|
|
|
this.onEnd('load', result); |
|
|
|
}, |
|
|
|
err => { |
|
|
|
this.onError('load', err); |
|
|
|
} |
|
|
|
); |
|
|
|
} else { |
|
|
|
clb && clb(result); |
|
|
|
} |
|
|
|
@ -272,6 +287,18 @@ module.exports = () => { |
|
|
|
} |
|
|
|
}, |
|
|
|
|
|
|
|
/** |
|
|
|
* On error callback |
|
|
|
* @private |
|
|
|
*/ |
|
|
|
onError(ctx, data) { |
|
|
|
if (em) { |
|
|
|
em.trigger(eventError, data); |
|
|
|
ctx && em.trigger(`${eventError}:${ctx}`, data); |
|
|
|
this.onEnd(ctx, data); |
|
|
|
} |
|
|
|
}, |
|
|
|
|
|
|
|
/** |
|
|
|
* Check if autoload is possible |
|
|
|
* @return {Boolean} |
|
|
|
|