Notifications cache should use `localStorage` wrapper
This commit is contained in:
parent
bf2c18fddb
commit
6fb69d4434
|
@ -46,4 +46,12 @@ KeyValueStore.prototype = {
|
|||
}
|
||||
};
|
||||
|
||||
// API compatibility with `localStorage`
|
||||
KeyValueStore.prototype.getItem = KeyValueStore.prototype.get;
|
||||
KeyValueStore.prototype.removeItem = KeyValueStore.prototype.remove;
|
||||
KeyValueStore.prototype.setItem = function(key, value) {
|
||||
this.set({ key, value });
|
||||
};
|
||||
|
||||
|
||||
export default KeyValueStore;
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
import StaleResult from 'discourse/lib/stale-result';
|
||||
import { hashString } from 'discourse/lib/hash';
|
||||
|
||||
// Mix this in to an adapter to provide stale caching in localStorage
|
||||
// Mix this in to an adapter to provide stale caching in our key value store
|
||||
export default {
|
||||
storageKey(type, findArgs) {
|
||||
const hashedArgs = Math.abs(hashString(JSON.stringify(findArgs)));
|
||||
|
@ -11,12 +11,11 @@ export default {
|
|||
findStale(store, type, findArgs) {
|
||||
const staleResult = new StaleResult();
|
||||
try {
|
||||
const stored = localStorage.getItem(this.storageKey(type, findArgs));
|
||||
const stored = this.keyValueStore.getItem(this.storageKey(type, findArgs));
|
||||
if (stored) {
|
||||
const parsed = JSON.parse(stored);
|
||||
staleResult.setResults(parsed);
|
||||
}
|
||||
|
||||
} catch(e) {
|
||||
// JSON parsing error
|
||||
}
|
||||
|
@ -25,7 +24,7 @@ export default {
|
|||
|
||||
find(store, type, findArgs) {
|
||||
return this._super(store, type, findArgs).then((results) => {
|
||||
localStorage.setItem(this.storageKey(type, findArgs), JSON.stringify(results));
|
||||
this.keyValueStore.setItem(this.storageKey(type, findArgs), JSON.stringify(results));
|
||||
return results;
|
||||
});
|
||||
}
|
||||
|
|
|
@ -15,7 +15,7 @@ function inject() {
|
|||
}
|
||||
|
||||
function injectAll(app, name) {
|
||||
inject(app, name, 'controller', 'component', 'route', 'view', 'model');
|
||||
inject(app, name, 'controller', 'component', 'route', 'view', 'model', 'adapter');
|
||||
}
|
||||
|
||||
export default {
|
||||
|
|
Loading…
Reference in New Issue