Skip to content

Commit

Permalink
fix: the persist option check (#126)
Browse files Browse the repository at this point in the history
  • Loading branch information
bluebill1049 authored May 29, 2022
1 parent f47288e commit bc10b65
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 4 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"files": [
"dist"
],
"version": "4.3.3-beta.6",
"version": "4.5.0",
"main": "dist/little-state-machine.js",
"module": "dist/little-state-machine.es.js",
"unpkg": "dist/little-state-machine.umd.js",
Expand Down
3 changes: 2 additions & 1 deletion src/StateMachineContext.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as React from 'react';
import storeFactory from './logic/storeFactory';
import { StateMachineContextValue } from './types';
import { PERSIST_BEFORE_UNLOAD } from './constants';

type PropsChildren = {
children?: React.ReactNode;
Expand All @@ -14,7 +15,7 @@ export const StateMachineProvider: React.FC<PropsChildren> = ({ children }) => {
const [state, setState] = React.useState(storeFactory.state);

React.useEffect(() => {
if (storeFactory.options.persist === 'beforeUnload') {
if (storeFactory.options.persist === PERSIST_BEFORE_UNLOAD) {
window.onbeforeunload = () => storeFactory.saveStore();
storeFactory.options.storageType.removeItem(storeFactory.options.name);
}
Expand Down
1 change: 1 addition & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export const STORE_DEFAULT_NAME = '__LSM__';
export const STORE_ACTION_NAME = '__LSM_NAME__';
export const PERSIST_BEFORE_UNLOAD = 'beforeUnload';
5 changes: 3 additions & 2 deletions src/stateMachine.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
AnyActions,
ActionsOutput,
} from './types';
import { STORE_ACTION_NAME } from './constants';
import { PERSIST_BEFORE_UNLOAD, STORE_ACTION_NAME } from './constants';

export function createStore(
defaultState: GlobalState,
Expand Down Expand Up @@ -53,7 +53,8 @@ const actionTemplate =
}

setState(storeFactory.state);
storeFactory.options.persist === 'onAction' && storeFactory.saveStore();
storeFactory.options.persist !== PERSIST_BEFORE_UNLOAD &&
storeFactory.saveStore();
};

export function useStateMachine<
Expand Down

0 comments on commit bc10b65

Please sign in to comment.