From 07055435550aebdeb948b9a0f78238c15ef4bf1b Mon Sep 17 00:00:00 2001 From: Eric Carr Date: Fri, 14 Dec 2018 16:17:45 +0100 Subject: [PATCH 1/2] Make onWriteFail configurable --- src/createPersistoid.js | 2 ++ src/types.js | 1 + 2 files changed, 3 insertions(+) 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 = { From a5d4dccd0562726c58f442dbe3f0b68ac2ced305 Mon Sep 17 00:00:00 2001 From: Eric Carr Date: Sat, 15 Dec 2018 00:32:14 +0100 Subject: [PATCH 2/2] Documented PersistConfig.writeFailHandler --- docs/api.md | 4 ++++ 1 file changed, 4 insertions(+) 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 {