Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add configurable handler to onWriteFail #949

Merged
merged 2 commits into from
Dec 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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