Skip to content

Commit 0db73d3

Browse files
dougkeenrt2zz
authored andcommitted
Give transformers access to the full state (i.e., across all available keys) (#682)
1 parent de1dbcf commit 0db73d3

File tree

4 files changed

+15
-11
lines changed

4 files changed

+15
-11
lines changed

src/createPersistoid.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export default function createPersistoid(config: PersistConfig): Persistoid {
5050

5151
let key = keysToProcess.shift()
5252
let endState = transforms.reduce((subState, transformer) => {
53-
return transformer.in(subState, key)
53+
return transformer.in(subState, key, lastState)
5454
}, lastState[key])
5555
if (typeof endState !== 'undefined') stagedWrite(key, endState)
5656
}

src/createTransform.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,13 @@ export default function createTransform(
2222
}
2323

2424
return {
25-
in: (state: Object, key: string) =>
26-
!whitelistBlacklistCheck(key) && inbound ? inbound(state, key) : state,
27-
out: (state: Object, key: string) =>
28-
!whitelistBlacklistCheck(key) && outbound ? outbound(state, key) : state,
25+
in: (state: Object, key: string, fullState: Object) =>
26+
!whitelistBlacklistCheck(key) && inbound
27+
? inbound(state, key, fullState)
28+
: state,
29+
out: (state: Object, key: string, fullState: Object) =>
30+
!whitelistBlacklistCheck(key) && outbound
31+
? outbound(state, key, fullState)
32+
: state,
2933
}
3034
}

src/getStoredState.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ export default function getStoredState(
88
config: PersistConfig
99
): Promise<Object | void> {
1010
const transforms = config.transforms || []
11-
const storageKey = `${config.keyPrefix !== undefined
12-
? config.keyPrefix
13-
: KEY_PREFIX}${config.key}`
11+
const storageKey = `${
12+
config.keyPrefix !== undefined ? config.keyPrefix : KEY_PREFIX
13+
}${config.key}`
1414
const storage = config.storage
1515
const debug = config.debug
1616
const deserialize = config.serialize === false ? x => x : defaultDeserialize
@@ -22,7 +22,7 @@ export default function getStoredState(
2222
let rawState = deserialize(serialized)
2323
Object.keys(rawState).forEach(key => {
2424
state[key] = transforms.reduceRight((subState, transformer) => {
25-
return transformer.out(subState, key)
25+
return transformer.out(subState, key, rawState)
2626
}, deserialize(rawState[key]))
2727
})
2828
return state

src/types.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ export type MigrationManifest = {
4141
}
4242

4343
export type Transform = {
44-
in: (Object | string, string) => Object,
45-
out: (Object | string, string) => Object,
44+
in: (Object | string, string, Object | string) => Object,
45+
out: (Object | string, string, Object | string) => Object,
4646
config?: PersistConfig,
4747
}
4848

0 commit comments

Comments
 (0)