From 9e78113da73a5945f20b708701f5f0f2ac4dcc0c Mon Sep 17 00:00:00 2001 From: Doug Keen Date: Fri, 26 Jan 2018 14:14:25 -0800 Subject: [PATCH] Give transformers access to the full state (i.e., across all available keys) --- src/createPersistoid.js | 11 ++++++----- src/createTransform.js | 12 ++++++++---- src/getStoredState.js | 8 ++++---- src/types.js | 4 ++-- 4 files changed, 20 insertions(+), 15 deletions(-) diff --git a/src/createPersistoid.js b/src/createPersistoid.js index 16dfe852e..c1a4327d0 100644 --- a/src/createPersistoid.js +++ b/src/createPersistoid.js @@ -10,9 +10,9 @@ export default function createPersistoid(config: PersistConfig): Persistoid { const whitelist: ?Array = config.whitelist || null const transforms = config.transforms || [] const throttle = config.throttle || 0 - const storageKey = `${config.keyPrefix !== undefined - ? config.keyPrefix - : KEY_PREFIX}${config.key}` + const storageKey = `${ + config.keyPrefix !== undefined ? config.keyPrefix : KEY_PREFIX + }${config.key}` const storage = config.storage const serialize = config.serialize === false ? x => x : defaultSerialize @@ -50,7 +50,7 @@ export default function createPersistoid(config: PersistConfig): Persistoid { let key = keysToProcess.shift() let endState = transforms.reduce((subState, transformer) => { - return transformer.in(subState, key) + return transformer.in(subState, key, lastState) }, lastState[key]) if (typeof endState !== 'undefined') stagedWrite(key, endState) } @@ -79,7 +79,8 @@ export default function createPersistoid(config: PersistConfig): Persistoid { } function passWhitelistBlacklist(key) { - if (whitelist && whitelist.indexOf(key) === -1 && key !== '_persist') return false + if (whitelist && whitelist.indexOf(key) === -1 && key !== '_persist') + return false if (blacklist && blacklist.indexOf(key) !== -1) return false return true } diff --git a/src/createTransform.js b/src/createTransform.js index 3da5a1592..04a973380 100644 --- a/src/createTransform.js +++ b/src/createTransform.js @@ -22,9 +22,13 @@ export default function createTransform( } return { - in: (state: Object, key: string) => - !whitelistBlacklistCheck(key) && inbound ? inbound(state, key) : state, - out: (state: Object, key: string) => - !whitelistBlacklistCheck(key) && outbound ? outbound(state, key) : state, + in: (state: Object, key: string, fullState: Object) => + !whitelistBlacklistCheck(key) && inbound + ? inbound(state, key, fullState) + : state, + out: (state: Object, key: string, fullState: Object) => + !whitelistBlacklistCheck(key) && outbound + ? outbound(state, key, fullState) + : state, } } diff --git a/src/getStoredState.js b/src/getStoredState.js index f98b34882..bcf78553c 100644 --- a/src/getStoredState.js +++ b/src/getStoredState.js @@ -8,9 +8,9 @@ export default function getStoredState( config: PersistConfig ): Promise { const transforms = config.transforms || [] - const storageKey = `${config.keyPrefix !== undefined - ? config.keyPrefix - : KEY_PREFIX}${config.key}` + const storageKey = `${ + config.keyPrefix !== undefined ? config.keyPrefix : KEY_PREFIX + }${config.key}` const storage = config.storage const debug = config.debug const deserialize = config.serialize === false ? x => x : defaultDeserialize @@ -22,7 +22,7 @@ export default function getStoredState( let rawState = deserialize(serialized) Object.keys(rawState).forEach(key => { state[key] = transforms.reduceRight((subState, transformer) => { - return transformer.out(subState, key) + return transformer.out(subState, key, rawState) }, deserialize(rawState[key])) }) return state diff --git a/src/types.js b/src/types.js index 2873bdb74..5ca2e5060 100644 --- a/src/types.js +++ b/src/types.js @@ -40,8 +40,8 @@ export type MigrationManifest = { } export type Transform = { - in: (Object | string, string) => Object, - out: (Object | string, string) => Object, + in: (Object | string, string, Object | string) => Object, + out: (Object | string, string, Object | string) => Object, config?: PersistConfig, }