Skip to content

Commit

Permalink
(persist): Add option to opt-out of serializing and deserializing sta…
Browse files Browse the repository at this point in the history
…te from storage (#507)

* Add serializer key to PersistConfiog to opt-in serializing input & output from storage

* (docs): Add serialize key to PersistConfig
  • Loading branch information
reblws authored and rt2zz committed Nov 17, 2017
1 parent 8161146 commit e92def2
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 6 deletions.
5 changes: 3 additions & 2 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ The Persistor is a redux store unto itself, plus
keyPrefix?: string, // will be prefixed to the storage key
debug?: boolean, // true -> verbose logs
stateReconciler?: false | StateReconciler, // false -> do not automatically reconcile state
serialize?: boolean, // false -> do not call JSON.parse & stringify when setting & getting from storage
}
```

Expand Down Expand Up @@ -93,7 +94,7 @@ Where Persistoid is [defined below](#type-persistoid).

### `type Persistoid`
```js
{
{
update: (State) => void
}
```
Expand All @@ -118,4 +119,4 @@ A function which reconciles:
- **inboundState**: the state being rehydrated from storage
- **originalState**: the state before the REHYDRATE action
- **reducedState**: the store state *after* the REHYDRATE action but *before* the reconcilliation
into final "rehydrated" state.
into final "rehydrated" state.
4 changes: 2 additions & 2 deletions src/createPersistoid.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ export default function createPersistoid(config: PersistConfig): Persistoid {
const storageKey = `${config.keyPrefix !== undefined
? config.keyPrefix
: KEY_PREFIX}${config.key}`

const storage = config.storage
const serialize = config.serialize === false ? x => x : defaultSerialize

// initialize stateful values
let lastState = {}
Expand Down Expand Up @@ -106,6 +106,6 @@ export default function createPersistoid(config: PersistConfig): Persistoid {
}

// @NOTE in the future this may be exposed via config
function serialize(data) {
function defaultSerialize(data) {
return JSON.stringify(data)
}
4 changes: 2 additions & 2 deletions src/getStoredState.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export default function getStoredState(
: KEY_PREFIX}${config.key}`
const storage = config.storage
const debug = config.debug

const deserialize = config.serialize === false ? x => x : defaultDeserialize
return storage.getItem(storageKey).then(serialized => {
if (!serialized) return undefined
else {
Expand All @@ -38,6 +38,6 @@ export default function getStoredState(
})
}

function deserialize(serial) {
function defaultDeserialize(serial) {
return JSON.parse(serial)
}
1 change: 1 addition & 0 deletions src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export type PersistConfig = {
stateReconciler?: false | Function,
getStoredState?: PersistConfig => Promise<PersistedState>, // used for migrations
debug?: boolean,
serialize?: boolean,
}

export type PersistorOptions = {
Expand Down

0 comments on commit e92def2

Please sign in to comment.