diff --git a/package.json b/package.json index 86b3e75..907b762 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "files": [ "dist" ], - "version": "4.0.0", + "version": "4.1.0-beta.4", "main": "dist/little-state-machine.js", "module": "dist/little-state-machine.es.js", "unpkg": "dist/little-state-machine.umd.js", diff --git a/src/stateMachine.tsx b/src/stateMachine.tsx index 27424f0..e87264f 100644 --- a/src/stateMachine.tsx +++ b/src/stateMachine.tsx @@ -8,6 +8,7 @@ import { AnyCallback, AnyActions, ActionsOutput, + ActionOptions, } from './types'; import { STORE_ACTION_NAME, STORE_DEFAULT_NAME } from './constants'; @@ -37,17 +38,24 @@ function actionTemplate( setState: React.Dispatch>, callback: TCallback, ) { - return (payload: Parameters[1]) => { + return ( + payload: Parameters[1], + options: ActionOptions = { + persist: true, + }, + ) => { if (process.env.NODE_ENV !== 'production') { window[STORE_ACTION_NAME] = callback ? callback.name : ''; } storeFactory.state = callback(storeFactory.state, payload); - storeFactory.storageType.setItem( - storeFactory.name, - JSON.stringify(storeFactory.state), - ); + if (options.persist) { + storeFactory.storageType.setItem( + storeFactory.name, + JSON.stringify(storeFactory.state), + ); + } if (storeFactory.middleWares.length) { storeFactory.state = storeFactory.middleWares.reduce( diff --git a/src/types.ts b/src/types.ts index ec4a12b..52918d8 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,18 +1,28 @@ import * as React from 'react'; -export interface GlobalState {}; +export interface GlobalState {} -export type AnyCallback = (state: GlobalState, payload: any) => GlobalState +export type AnyCallback = (state: GlobalState, payload: any) => GlobalState; + +export type ActionOptions = { + persist: boolean; +}; export type AnyActions = Record; -export type ActionsOutput> = { - [K in keyof TActions]: (payload: Parameters[1]) => void; -} +export type ActionsOutput< + TCallback extends AnyCallback, + TActions extends AnyActions +> = { + [K in keyof TActions]: ( + payload: Parameters[1], + options?: ActionOptions, + ) => void; +}; export type StateMachineContextValue = { state: GlobalState; - setState: React.Dispatch> + setState: React.Dispatch>; }; export type MiddleWare = (