Skip to content
Permalink

Comparing changes

This is a direct comparison between two commits made in this repository or its related repositories. View the default comparison for this range or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: agilgur5/mst-persist
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 319546f654aee1bb6271c9c4f4b16769985b2ac9
Choose a base ref
..
head repository: agilgur5/mst-persist
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: f66b21cf880d190f052adcdf8208b1d1d54052fc
Choose a head ref
Showing with 10 additions and 8 deletions.
  1. +8 −5 src/asyncLocalStorage.ts
  2. +1 −2 src/index.ts
  3. +1 −1 test/index.spec.ts
13 changes: 8 additions & 5 deletions src/asyncLocalStorage.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// using istanbul ignore on portions of code that are not currently used internally

interface IAsyncLocalStorage {
clear(): Promise<void>
getItem(key: string): Promise<string>
@@ -6,25 +8,26 @@ interface IAsyncLocalStorage {
}

export const AsyncLocalStorage: IAsyncLocalStorage = {
// istanbul ignore clear and removeItem as they're not currently used internally
// must use wrapper functions when passing localStorage functions (https://github.com/agilgur5/mst-persist/issues/18)
clear /* istanbul ignore next */ () {
return callWithPromise(window.localStorage.clear)
return callWithPromise(() => window.localStorage.clear())
},
getItem (key) {
return callWithPromise(window.localStorage.getItem, key)
return callWithPromise(() => window.localStorage.getItem(key))
},
removeItem /* istanbul ignore next */ (key) {
return callWithPromise(window.localStorage.removeItem, key)
return callWithPromise(() => window.localStorage.removeItem(key))
},
setItem (key, value) {
return callWithPromise(window.localStorage.setItem, key, value)
return callWithPromise(() => window.localStorage.setItem(key, value))
}
}

function callWithPromise (func: Function, ...args: any[]): Promise<any> {
try {
return Promise.resolve(func(...args))
} catch (err) {
/* istanbul ignore next */
return Promise.reject(err)
}
}
3 changes: 1 addition & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -14,7 +14,7 @@ export interface IOptions {
type StrToAnyMap = {[key: string]: any}

export const persist: IArgs = (name, store, options = {}) => {
let {storage, jsonify, whitelist, blacklist} = options
let {storage, jsonify = true, whitelist, blacklist} = options

// use AsyncLocalStorage by default (or if localStorage was passed in)
if (
@@ -30,7 +30,6 @@ export const persist: IArgs = (name, store, options = {}) => {
'engine via the `storage:` option.')
}

if (!jsonify) { jsonify = true } // default to true like mobx-persist
const whitelistDict = arrToDict(whitelist)
const blacklistDict = arrToDict(blacklist)

2 changes: 1 addition & 1 deletion test/index.spec.ts
Original file line number Diff line number Diff line change
@@ -47,7 +47,7 @@ describe('persist options', () => {

user.changeName('Joe') // fire action to trigger onSnapshot
// if not jsonified, localStorage will store as '[object Object]'
expect(window.localStorage.getItem('user')).toStrictEqual('[object Object]')
expect(window.localStorage.getItem('user')).toBe('[object Object]')
})

it('should whitelist', async () => {