-
Notifications
You must be signed in to change notification settings - Fork 13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use localForage #16
base: master
Are you sure you want to change the base?
Use localForage #16
Changes from 4 commits
9abf6db
8bbfd57
021efbd
4fd103d
da8ec11
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
node_modules | ||
lib | ||
npm-debug.log* |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,8 @@ | |
import CacheRecordStore from './CacheRecordStore'; | ||
import type { CacheRecord } from './CacheRecordStore'; | ||
|
||
import localForage from 'localforage'; | ||
|
||
const DEFAULT_CACHE_KEY: string = '__RelayCacheManager__'; | ||
|
||
type CacheWriterOptions = { | ||
|
@@ -20,9 +22,8 @@ export default class CacheWriter { | |
constructor(options: CacheWriterOptions = {}) { | ||
this.cacheKey = options.cacheKey || DEFAULT_CACHE_KEY | ||
try { | ||
let localCache = localStorage.getItem(this.cacheKey); | ||
let localCache = localForage.getItem(this.cacheKey); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
localForage.getItem(this.cacheKey)
.then(localCache => {
if (!localCache) {
this.cache = new CacheRecordStore();
} else {
this.cache = CacheRecordStore.fromJSON(localCache)
}
})
.catch(err => this.cache = new CacheRecordStore()) |
||
if (localCache) { | ||
localCache = JSON.parse(localCache); | ||
this.cache = CacheRecordStore.fromJSON(localCache); | ||
} else { | ||
this.cache = new CacheRecordStore(); | ||
|
@@ -33,7 +34,7 @@ export default class CacheWriter { | |
} | ||
|
||
clearStorage() { | ||
localStorage.removeItem(this.cacheKey); | ||
localForage.removeItem(this.cacheKey); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here, lets use the Promise API to reset |
||
this.cache = new CacheRecordStore(); | ||
} | ||
|
||
|
@@ -53,8 +54,7 @@ export default class CacheWriter { | |
record[field] = value; | ||
this.cache.records[dataId] = record; | ||
try { | ||
const serialized = JSON.stringify(this.cache); | ||
localStorage.setItem(this.cacheKey, serialized); | ||
localForage.setItem(this.cacheKey, this.cache); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can move this out of the |
||
} catch (err) { | ||
/* noop */ | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You need to call
localforage.config
before using it, so lets let users pass in their own config for this, and default to an empty object.