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; export default KeyValueStore;

View File

@ -1,7 +1,7 @@
import StaleResult from 'discourse/lib/stale-result'; import StaleResult from 'discourse/lib/stale-result';
import { hashString } from 'discourse/lib/hash'; 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 { export default {
storageKey(type, findArgs) { storageKey(type, findArgs) {
const hashedArgs = Math.abs(hashString(JSON.stringify(findArgs))); const hashedArgs = Math.abs(hashString(JSON.stringify(findArgs)));
@ -11,12 +11,11 @@ export default {
findStale(store, type, findArgs) { findStale(store, type, findArgs) {
const staleResult = new StaleResult(); const staleResult = new StaleResult();
try { try {
const stored = localStorage.getItem(this.storageKey(type, findArgs)); const stored = this.keyValueStore.getItem(this.storageKey(type, findArgs));
if (stored) { if (stored) {
const parsed = JSON.parse(stored); const parsed = JSON.parse(stored);
staleResult.setResults(parsed); staleResult.setResults(parsed);
} }
} catch(e) { } catch(e) {
// JSON parsing error // JSON parsing error
} }
@ -25,7 +24,7 @@ export default {
find(store, type, findArgs) { find(store, type, findArgs) {
return this._super(store, type, findArgs).then((results) => { 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; return results;
}); });
} }

View File

@ -15,7 +15,7 @@ function inject() {
} }
function injectAll(app, name) { function injectAll(app, name) {
inject(app, name, 'controller', 'component', 'route', 'view', 'model'); inject(app, name, 'controller', 'component', 'route', 'view', 'model', 'adapter');
} }
export default { export default {