Notifications cache should use `localStorage` wrapper

This commit is contained in:
Robin Ward 2015-09-01 14:41:10 -04:00
parent bf2c18fddb
commit 6fb69d4434
3 changed files with 12 additions and 5 deletions

View File

@ -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;

View File

@ -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;
});
}

View File

@ -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 {