-
Notifications
You must be signed in to change notification settings - Fork 864
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #158 from rt2zz/immutable
v4
- Loading branch information
Showing
7 changed files
with
178 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,15 @@ | ||
import createAsyncLocalStorage from './defaults/asyncLocalStorage' | ||
|
||
import autoRehydrate from './autoRehydrate' | ||
import createPersistor from './createPersistor' | ||
import createTransform from './createTransform' | ||
import getStoredState from './getStoredState' | ||
import persistStore from './persistStore' | ||
import purgeStoredState from './purgeStoredState' | ||
|
||
import createAsyncLocalStorage from './defaults/asyncLocalStorage' | ||
const storages = { | ||
asyncLocalStorage: createAsyncLocalStorage('local'), | ||
asyncSessionStorage: createAsyncLocalStorage('session') | ||
} | ||
|
||
export { autoRehydrate, createPersistor, createTransform, getStoredState, persistStore, storages } | ||
export { autoRehydrate, createPersistor, createTransform, getStoredState, persistStore, purgeStoredState, storages } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { KEY_PREFIX } from './constants' | ||
|
||
export default function purgeStoredState (config, keys) { | ||
const storage = config.storage | ||
const keyPrefix = config.keyPrefix || KEY_PREFIX | ||
|
||
// basic validation | ||
if (Array.isArray(config)) throw new Error('redux-persist: purgeStoredState requires config as a first argument (found array). An array of keys is the optional second argument.') | ||
if (!storage) throw new Error('redux-persist: config.storage required in purgeStoredState') | ||
|
||
if (typeof keys === 'undefined') { // if keys is not defined, purge all keys | ||
return new Promise((resolve, reject) => { | ||
storage.getAllKeys((err, allKeys) => { | ||
if (err && process.env.NODE_ENV !== 'production') { | ||
console.warn('redux-persist: error during purgeStoredState in storage.getAllKeys') | ||
reject(err) | ||
} else { | ||
resolve(purgeStoredState(config, allKeys.filter((key) => key.indexOf(keyPrefix) === 0).map((key) => key.slice(keyPrefix.length)))) | ||
} | ||
}) | ||
}) | ||
} else { // otherwise purge specified keys | ||
return Promise.all(keys.map((key) => { | ||
return storage.removeItem(`${keyPrefix}${key}`, warnIfRemoveError(key)) | ||
})) | ||
} | ||
} | ||
|
||
function warnIfRemoveError (key) { | ||
return function removeError (err) { | ||
if (err && process.env.NODE_ENV !== 'production') { console.warn('Error storing data for key:', key, err) } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import test from 'ava' | ||
|
||
import { purgeStoredState, storages } from '../src' | ||
|
||
test('purgeStoredState (all) returns a promise', t => { | ||
let purgeResult = purgeStoredState({ storage: storages.asyncLocalStorage }) | ||
t.true(isPromise(purgeResult)) | ||
return purgeResult | ||
}) | ||
|
||
test('purgeStoredState (whitelist) returns a promise', t => { | ||
let purgeResult = purgeStoredState({ storage: storages.asyncLocalStorage }, ['foo']) | ||
t.true(isPromise(purgeResult)) | ||
return purgeResult | ||
}) | ||
|
||
function isPromise (something) { | ||
return typeof something === 'object' && typeof something.then === 'function' | ||
} |