Skip to content

Commit

Permalink
Merge pull request #949 from ninjacarr/master
Browse files Browse the repository at this point in the history
Add configurable handler to onWriteFail
  • Loading branch information
aguynamedben authored Dec 22, 2018
2 parents 1bc909f + a5d4dcc commit 1a46791
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 0 deletions.
4 changes: 4 additions & 0 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,13 @@ The Persistor is a redux store unto itself, plus
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
writeFailHandler?: Function, // will be called if the storage engine fails during setItem()
}
```

Persisting state involves calling setItem() on the storage engine. By default, this will fail silently if the storage/quota is exhausted.
Provide a writeFailHandler(error) function to be notified if this occurs.

### `type MigrationManifest`
```js
{
Expand Down
2 changes: 2 additions & 0 deletions src/createPersistoid.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export default function createPersistoid(config: PersistConfig): Persistoid {
}${config.key}`
const storage = config.storage
const serialize = config.serialize === false ? x => x : defaultSerialize
const writeFailHandler = config.writeFailHandler || null

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

function onWriteFail(err) {
// @TODO add fail handlers (typically storage full)
if (writeFailHandler) writeFailHandler(err)
if (err && process.env.NODE_ENV !== 'production') {
console.error('Error storing data', err)
}
Expand Down
1 change: 1 addition & 0 deletions src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export type PersistConfig = {
debug?: boolean,
serialize?: boolean,
timeout?: number,
writeFailHandler?: Function,
}

export type PersistorOptions = {
Expand Down

0 comments on commit 1a46791

Please sign in to comment.