diff --git a/README.md b/README.md index 4bc5d31..4fa99fc 100644 --- a/README.md +++ b/README.md @@ -125,6 +125,8 @@ setTimeout(() => { - methods - **rehydrate** *function* - returns *IHydrateResult* + - **dispose** *function* + - disposes reaction which is useful if you want to call `create` again with different config ## Examples diff --git a/lib/index.d.ts b/lib/index.d.ts index b1d19cf..8255311 100644 --- a/lib/index.d.ts +++ b/lib/index.d.ts @@ -9,5 +9,6 @@ export interface optionsType { } export interface IHydrateResult extends Promise { rehydrate: () => IHydrateResult; + dispose: () => void; } -export declare function create({storage, jsonify, debounce}?: any): (key: string, store: T, initialState?: any, customArgs?: any) => IHydrateResult; +export declare function create({ storage, jsonify, debounce }?: any): (key: string, store: T, initialState?: any, customArgs?: any) => IHydrateResult; diff --git a/lib/index.js b/lib/index.js index b95dede..1000a65 100644 --- a/lib/index.js +++ b/lib/index.js @@ -45,7 +45,7 @@ function create(_a) { return promise; } var result = hydration(); - mobx_1.reaction(function () { return serializr_1.serialize(schema, store); }, function (data) { return storage.setItem(key, !jsonify ? data : JSON.stringify(data)); }, { + result.dispose = mobx_1.reaction(function () { return serializr_1.serialize(schema, store); }, function (data) { return storage.setItem(key, !jsonify ? data : JSON.stringify(data)); }, { delay: debounce }); return result; diff --git a/src/index.ts b/src/index.ts index 515ebb0..97e972e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -30,7 +30,8 @@ export interface optionsType { } export interface IHydrateResult extends Promise { - rehydrate: () => IHydrateResult + rehydrate: () => IHydrateResult, + dispose: () => void } export function create({ @@ -58,7 +59,7 @@ export function create({ return promise } const result = hydration() - reaction( + result.dispose = reaction( () => serialize(schema, store), (data: any) => storage.setItem(key, !jsonify ? data : JSON.stringify(data)), {