Skip to content

Commit

Permalink
Give transformers access to the full state (i.e., across all availabl…
Browse files Browse the repository at this point in the history
…e keys) (#682)
  • Loading branch information
dougkeen authored and rt2zz committed Feb 1, 2018
1 parent de1dbcf commit 0db73d3
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/createPersistoid.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
12 changes: 8 additions & 4 deletions src/createTransform.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}
8 changes: 4 additions & 4 deletions src/getStoredState.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ export default function getStoredState(
config: PersistConfig
): Promise<Object | void> {
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
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,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,
}

Expand Down

0 comments on commit 0db73d3

Please sign in to comment.