diff --git a/docs/api.md b/docs/api.md index 350b619aa..be180465f 100644 --- a/docs/api.md +++ b/docs/api.md @@ -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 { diff --git a/src/createPersistoid.js b/src/createPersistoid.js index 56be8dc52..c4423af60 100644 --- a/src/createPersistoid.js +++ b/src/createPersistoid.js @@ -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 = {} @@ -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) } diff --git a/src/types.js b/src/types.js index 3f7f9fd78..13549104b 100644 --- a/src/types.js +++ b/src/types.js @@ -25,6 +25,7 @@ export type PersistConfig = { debug?: boolean, serialize?: boolean, timeout?: number, + writeFailHandler?: Function, } export type PersistorOptions = {