Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: persist as option for actions #103

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.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",
Expand Down
18 changes: 13 additions & 5 deletions src/stateMachine.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
AnyCallback,
AnyActions,
ActionsOutput,
ActionOptions,
} from './types';
import { STORE_ACTION_NAME, STORE_DEFAULT_NAME } from './constants';

Expand Down Expand Up @@ -37,17 +38,24 @@ function actionTemplate<TCallback extends AnyCallback>(
setState: React.Dispatch<React.SetStateAction<GlobalState>>,
callback: TCallback,
) {
return (payload: Parameters<TCallback>[1]) => {
return (
payload: Parameters<TCallback>[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(
Expand Down
22 changes: 16 additions & 6 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -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<TCallback> = Record<string, TCallback>;

export type ActionsOutput<TCallback extends AnyCallback, TActions extends AnyActions<TCallback>> = {
[K in keyof TActions]: (payload: Parameters<TActions[K]>[1]) => void;
}
export type ActionsOutput<
TCallback extends AnyCallback,
TActions extends AnyActions<TCallback>
> = {
[K in keyof TActions]: (
payload: Parameters<TActions[K]>[1],
options?: ActionOptions,
) => void;
};

export type StateMachineContextValue = {
state: GlobalState;
setState: React.Dispatch<React.SetStateAction<GlobalState>>
setState: React.Dispatch<React.SetStateAction<GlobalState>>;
};

export type MiddleWare = (
Expand Down