Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

little-state-machine

Package Overview
Dependencies
Maintainers
1
Versions
210
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

little-state-machine - npm Package Compare versions

Comparing version 4.0.0-beta.2 to 4.0.0-rc.0

77

dist/little-state-machine.es.js

@@ -1,2 +0,2 @@

import { createContext, useState, useMemo, createElement, useContext, useCallback } from 'react';
import { createContext, useState, createElement, useMemo, useContext } from 'react';

@@ -23,10 +23,3 @@ const STORE_DEFAULT_NAME = '__LSM__';

? window.sessionStorage
: {
getItem: (payload) => payload,
setItem: (payload) => payload,
clear: () => { },
length: 0,
key: (payload) => payload.toString(),
removeItem: () => { },
};
: {};
this.name = name;

@@ -54,8 +47,2 @@ }

const storeFactory = new StoreFactory(STORE_DEFAULT_NAME, isClient);
const middleWare = (data) => {
if (data) {
window[STORE_ACTION_NAME] = data;
}
return data;
};
function createStore(defaultStoreData, options = {

@@ -67,7 +54,9 @@ name: STORE_DEFAULT_NAME,

options.storageType && (storeFactory.storageType = options.storageType);
if (process.env.NODE_ENV !== 'production' && isClient) {
window[STORE_DEFAULT_NAME] = storeFactory.name;
}
storeFactory.updateMiddleWares(options.middleWares);
if (process.env.NODE_ENV !== 'production') {
if (isClient) {
window['__LSM_NAME__'] = storeFactory.name;
}
}
if (process.env.NODE_ENV !== 'production') {
setUpDevTools(storeFactory.storageType, storeFactory.name, storeFactory.store);

@@ -83,36 +72,30 @@ }

const [globalState, updateStore] = useState(storeFactory.store);
const value = useMemo(() => ({
store: globalState,
updateStore,
}), [globalState]);
return createElement(StateMachineContext.Provider, Object.assign({ value: value }, props));
return (createElement(StateMachineContext.Provider, Object.assign({ value: useMemo(() => ({
store: globalState,
updateStore,
}), [globalState]) }, props)));
}
const actionTemplate = ({ options, callback, updateStore, }) => (payload) => {
if (process.env.NODE_ENV !== 'production') {
const debugName = callback ? callback.name : '';
middleWare(debugName);
}
if (callback) {
function actionTemplate(updateStore, callback) {
return (payload) => {
if (process.env.NODE_ENV !== 'production') {
window[STORE_ACTION_NAME] = callback ? callback.name : '';
}
storeFactory.store = callback(storeFactory.store, payload);
}
storeFactory.storageType.setItem(storeFactory.name, JSON.stringify(storeFactory.store));
if (storeFactory.middleWares.length) {
storeFactory.store = storeFactory.middleWares.reduce((currentValue, currentFunction) => currentFunction(currentValue) || currentValue, storeFactory.store);
}
!options && updateStore(storeFactory.store);
};
function useStateMachine(updateStoreFunction, options) {
const { store: globalState, updateStore } = useContext(StateMachineContext);
return {
actions: updateStoreFunction
? Object.entries(updateStoreFunction).reduce((previous, [key, callback]) => (Object.assign(Object.assign({}, previous), { [key]: useCallback(actionTemplate({
options,
callback,
updateStore,
}), []) })), {})
: {},
state: globalState,
storeFactory.storageType.setItem(storeFactory.name, JSON.stringify(storeFactory.store));
if (storeFactory.middleWares.length) {
storeFactory.store = storeFactory.middleWares.reduce((currentValue, currentFunction) => currentFunction(currentValue) || currentValue, storeFactory.store);
}
updateStore(storeFactory.store);
};
}
function useStateMachine(actions) {
const { store, updateStore } = useContext(StateMachineContext);
return useMemo(() => ({
actions: actions
? Object.entries(actions).reduce((previous, [key, callback]) => (Object.assign(Object.assign({}, previous), { [key]: actionTemplate(updateStore, callback) })), {})
: {},
state: store,
}), [store, actions]);
}
export { StateMachineProvider, createStore, useStateMachine };

@@ -96,16 +96,3 @@ 'use strict';

this.middleWares = [];
this.storageType = isClient && typeof sessionStorage !== 'undefined' ? window.sessionStorage : {
getItem: function (payload) {
return payload;
},
setItem: function (payload) {
return payload;
},
clear: function () {},
length: 0,
key: function (payload) {
return payload.toString();
},
removeItem: function () {}
};
this.storageType = isClient && typeof sessionStorage !== 'undefined' ? window.sessionStorage : {};
this.name = name;

@@ -154,10 +141,2 @@ }

var middleWare = function (data) {
if (data) {
window[STORE_ACTION_NAME] = data;
}
return data;
};
function createStore(defaultStoreData, options) {

@@ -173,9 +152,10 @@ if (options === void 0) {

options.storageType && (storeFactory.storageType = options.storageType);
storeFactory.updateMiddleWares(options.middleWares);
if (process.env.NODE_ENV !== 'production' && isClient) {
window[STORE_DEFAULT_NAME] = storeFactory.name;
if (process.env.NODE_ENV !== 'production') {
if (isClient) {
window['__LSM_NAME__'] = storeFactory.name;
}
}
storeFactory.updateMiddleWares(options.middleWares);
if (process.env.NODE_ENV !== 'production') {

@@ -200,27 +180,19 @@ setUpDevTools(storeFactory.storageType, storeFactory.name, storeFactory.store);

var value = React.useMemo(function () {
return {
store: globalState,
updateStore: updateStore
};
}, [globalState]);
return React.createElement(StateMachineContext.Provider, __assign({
value: value
value: React.useMemo(function () {
return {
store: globalState,
updateStore: updateStore
};
}, [globalState])
}, props));
}
var actionTemplate = function (_a) {
var options = _a.options,
callback = _a.callback,
updateStore = _a.updateStore;
function actionTemplate(updateStore, callback) {
return function (payload) {
if (process.env.NODE_ENV !== 'production') {
var debugName = callback ? callback.name : '';
middleWare(debugName);
window[STORE_ACTION_NAME] = callback ? callback.name : '';
}
if (callback) {
storeFactory.store = callback(storeFactory.store, payload);
}
storeFactory.store = callback(storeFactory.store, payload);
storeFactory.storageType.setItem(storeFactory.name, _JSON$stringify(storeFactory.store));

@@ -236,29 +208,27 @@

!options && updateStore(storeFactory.store);
updateStore(storeFactory.store);
};
};
}
function useStateMachine(updateStoreFunction, options) {
var _context2;
function useStateMachine(actions) {
var _a = React.useContext(StateMachineContext),
globalState = _a.store,
store = _a.store,
updateStore = _a.updateStore;
return {
actions: updateStoreFunction ? _reduceInstanceProperty(_context2 = _Object$entries(updateStoreFunction)).call(_context2, function (previous, _a) {
var _b;
return React.useMemo(function () {
var _context2;
var _c = __read(_a, 2),
key = _c[0],
callback = _c[1];
return {
actions: actions ? _reduceInstanceProperty(_context2 = _Object$entries(actions)).call(_context2, function (previous, _a) {
var _b;
return __assign(__assign({}, previous), (_b = {}, _b[key] = React.useCallback(actionTemplate({
options: options,
callback: callback,
updateStore: updateStore
}), []), _b));
}, {}) : {},
state: globalState
};
var _c = __read(_a, 2),
key = _c[0],
callback = _c[1];
return __assign(__assign({}, previous), (_b = {}, _b[key] = actionTemplate(updateStore, callback), _b));
}, {}) : {},
state: store
};
}, [store, actions]);
}

@@ -265,0 +235,0 @@

@@ -27,10 +27,3 @@ 'use strict';

? window.sessionStorage
: {
getItem: (payload) => payload,
setItem: (payload) => payload,
clear: () => { },
length: 0,
key: (payload) => payload.toString(),
removeItem: () => { },
};
: {};
this.name = name;

@@ -58,8 +51,2 @@ }

const storeFactory = new StoreFactory(STORE_DEFAULT_NAME, isClient);
const middleWare = (data) => {
if (data) {
window[STORE_ACTION_NAME] = data;
}
return data;
};
function createStore(defaultStoreData, options = {

@@ -71,7 +58,9 @@ name: STORE_DEFAULT_NAME,

options.storageType && (storeFactory.storageType = options.storageType);
if (process.env.NODE_ENV !== 'production' && isClient) {
window[STORE_DEFAULT_NAME] = storeFactory.name;
}
storeFactory.updateMiddleWares(options.middleWares);
if (process.env.NODE_ENV !== 'production') {
if (isClient) {
window['__LSM_NAME__'] = storeFactory.name;
}
}
if (process.env.NODE_ENV !== 'production') {
setUpDevTools(storeFactory.storageType, storeFactory.name, storeFactory.store);

@@ -87,35 +76,29 @@ }

const [globalState, updateStore] = React.useState(storeFactory.store);
const value = React.useMemo(() => ({
store: globalState,
updateStore,
}), [globalState]);
return React.createElement(StateMachineContext.Provider, Object.assign({ value: value }, props));
return (React.createElement(StateMachineContext.Provider, Object.assign({ value: React.useMemo(() => ({
store: globalState,
updateStore,
}), [globalState]) }, props)));
}
const actionTemplate = ({ options, callback, updateStore, }) => (payload) => {
if (process.env.NODE_ENV !== 'production') {
const debugName = callback ? callback.name : '';
middleWare(debugName);
}
if (callback) {
function actionTemplate(updateStore, callback) {
return (payload) => {
if (process.env.NODE_ENV !== 'production') {
window[STORE_ACTION_NAME] = callback ? callback.name : '';
}
storeFactory.store = callback(storeFactory.store, payload);
}
storeFactory.storageType.setItem(storeFactory.name, JSON.stringify(storeFactory.store));
if (storeFactory.middleWares.length) {
storeFactory.store = storeFactory.middleWares.reduce((currentValue, currentFunction) => currentFunction(currentValue) || currentValue, storeFactory.store);
}
!options && updateStore(storeFactory.store);
};
function useStateMachine(updateStoreFunction, options) {
const { store: globalState, updateStore } = React.useContext(StateMachineContext);
return {
actions: updateStoreFunction
? Object.entries(updateStoreFunction).reduce((previous, [key, callback]) => (Object.assign(Object.assign({}, previous), { [key]: React.useCallback(actionTemplate({
options,
callback,
updateStore,
}), []) })), {})
: {},
state: globalState,
storeFactory.storageType.setItem(storeFactory.name, JSON.stringify(storeFactory.store));
if (storeFactory.middleWares.length) {
storeFactory.store = storeFactory.middleWares.reduce((currentValue, currentFunction) => currentFunction(currentValue) || currentValue, storeFactory.store);
}
updateStore(storeFactory.store);
};
}
function useStateMachine(actions) {
const { store, updateStore } = React.useContext(StateMachineContext);
return React.useMemo(() => ({
actions: actions
? Object.entries(actions).reduce((previous, [key, callback]) => (Object.assign(Object.assign({}, previous), { [key]: actionTemplate(updateStore, callback) })), {})
: {},
state: store,
}), [store, actions]);
}

@@ -122,0 +105,0 @@ exports.StateMachineProvider = StateMachineProvider;

@@ -1,1 +0,1 @@

import{createContext as e,useState as t,useMemo as o,createElement as s,useContext as r,useCallback as n}from"react";const _="undefined"!=typeof window,a=new class{constructor(e,t){this.name="__LSM__",this.store=void 0,this.middleWares=[],this.storageType=t&&"undefined"!=typeof sessionStorage?window.sessionStorage:{getItem:e=>e,setItem:e=>e,clear:()=>{},length:0,key:e=>e.toString(),removeItem:()=>{}},this.name=e}updateStore(e){this.store=((e,t)=>{try{return JSON.parse(e.getItem(t))}catch(e){return""}})(this.storageType,this.name)||e}updateMiddleWares(e){return this.middleWares=e}}("__LSM__",_);function i(e,t={name:"__LSM__",middleWares:[]}){var o,s,r;t.name&&(a.name=t.name),t.storageType&&(a.storageType=t.storageType),"production"!==process.env.NODE_ENV&&_&&(window.__LSM__=a.name),a.updateMiddleWares(t.middleWares),"production"!==process.env.NODE_ENV&&(o=a.storageType,s=a.name,r=a.store,"undefined"!=typeof window&&(window.__LSM_DEBUG__=e=>o.setItem("___LSM_DEBUG__",e),window.__LSM_RESET__=()=>o.clear(),window.__LSM_GET_STORE__=()=>o.getItem(s),window.__LSM_SAVE_TO__=e=>window.localStorage.setItem(e,JSON.stringify(r)),window.__LSM_LOAD__=({storeName:e,data:t})=>o.setItem(s||"___LSM_DEBUG__",t||window.localStorage.getItem(e)||""))),a.updateStore(e)}const d=e({store:a.store,updateStore:e=>e});function c(e){const[r,n]=t(a.store),_=o(()=>({store:r,updateStore:n}),[r]);return s(d.Provider,Object.assign({value:_},e))}const m=({options:e,callback:t,updateStore:o})=>s=>{if("production"!==process.env.NODE_ENV){const e=t?t.name:"";(r=e)&&(window.__LSM_NAME__=r)}var r;t&&(a.store=t(a.store,s)),a.storageType.setItem(a.name,JSON.stringify(a.store)),a.middleWares.length&&(a.store=a.middleWares.reduce((e,t)=>t(e)||e,a.store)),!e&&o(a.store)};function p(e,t){const{store:o,updateStore:s}=r(d);return{actions:e?Object.entries(e).reduce((e,[o,r])=>Object.assign(Object.assign({},e),{[o]:n(m({options:t,callback:r,updateStore:s}),[])}),{}):{},state:o}}export{c as StateMachineProvider,i as createStore,p as useStateMachine};
import{createContext as e,useState as t,createElement as o,useMemo as r,useContext as s}from"react";const n="undefined"!=typeof window,_=new class{constructor(e,t){this.name="__LSM__",this.store=void 0,this.middleWares=[],this.storageType=t&&"undefined"!=typeof sessionStorage?window.sessionStorage:{},this.name=e}updateStore(e){this.store=((e,t)=>{try{return JSON.parse(e.getItem(t))}catch(e){return""}})(this.storageType,this.name)||e}updateMiddleWares(e){return this.middleWares=e}}("__LSM__",n);function a(e,t={name:"__LSM__",middleWares:[]}){var o,r,s;t.name&&(_.name=t.name),t.storageType&&(_.storageType=t.storageType),_.updateMiddleWares(t.middleWares),"production"!==process.env.NODE_ENV&&n&&(window.__LSM_NAME__=_.name),"production"!==process.env.NODE_ENV&&(o=_.storageType,r=_.name,s=_.store,"undefined"!=typeof window&&(window.__LSM_DEBUG__=e=>o.setItem("___LSM_DEBUG__",e),window.__LSM_RESET__=()=>o.clear(),window.__LSM_GET_STORE__=()=>o.getItem(r),window.__LSM_SAVE_TO__=e=>window.localStorage.setItem(e,JSON.stringify(s)),window.__LSM_LOAD__=({storeName:e,data:t})=>o.setItem(r||"___LSM_DEBUG__",t||window.localStorage.getItem(e)||""))),_.updateStore(e)}const i=e({store:_.store,updateStore:e=>e});function d(e){const[s,n]=t(_.store);return o(i.Provider,Object.assign({value:r(()=>({store:s,updateStore:n}),[s])},e))}function c(e,t){return o=>{"production"!==process.env.NODE_ENV&&(window.__LSM_NAME__=t?t.name:""),_.store=t(_.store,o),_.storageType.setItem(_.name,JSON.stringify(_.store)),_.middleWares.length&&(_.store=_.middleWares.reduce((e,t)=>t(e)||e,_.store)),e(_.store)}}function m(e){const{store:t,updateStore:o}=s(i);return r(()=>({actions:e?Object.entries(e).reduce((e,[t,r])=>Object.assign(Object.assign({},e),{[t]:c(o,r)}),{}):{},state:t}),[t,e])}export{d as StateMachineProvider,a as createStore,m as useStateMachine};
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('react')) :
typeof define === 'function' && define.amd ? define(['exports', 'react'], factory) :
(global = global || self, factory(global.ReactHookForm = {}, global.React));
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.ReactHookForm = {}, global.React));
}(this, (function (exports, React) { 'use strict';

@@ -26,10 +26,3 @@

? window.sessionStorage
: {
getItem: (payload) => payload,
setItem: (payload) => payload,
clear: () => { },
length: 0,
key: (payload) => payload.toString(),
removeItem: () => { },
};
: {};
this.name = name;

@@ -62,31 +55,26 @@ }

const [globalState, updateStore] = React.useState(storeFactory.store);
const value = React.useMemo(() => ({
store: globalState,
updateStore,
}), [globalState]);
return React.createElement(StateMachineContext.Provider, Object.assign({ value: value }, props));
return (React.createElement(StateMachineContext.Provider, Object.assign({ value: React.useMemo(() => ({
store: globalState,
updateStore,
}), [globalState]) }, props)));
}
const actionTemplate = ({ options, callback, updateStore, }) => (payload) => {
if (callback) {
function actionTemplate(updateStore, callback) {
return (payload) => {
storeFactory.store = callback(storeFactory.store, payload);
}
storeFactory.storageType.setItem(storeFactory.name, JSON.stringify(storeFactory.store));
if (storeFactory.middleWares.length) {
storeFactory.store = storeFactory.middleWares.reduce((currentValue, currentFunction) => currentFunction(currentValue) || currentValue, storeFactory.store);
}
!options && updateStore(storeFactory.store);
};
function useStateMachine(updateStoreFunction, options) {
const { store: globalState, updateStore } = React.useContext(StateMachineContext);
return {
actions: updateStoreFunction
? Object.entries(updateStoreFunction).reduce((previous, [key, callback]) => (Object.assign(Object.assign({}, previous), { [key]: React.useCallback(actionTemplate({
options,
callback,
updateStore,
}), []) })), {})
: {},
state: globalState,
storeFactory.storageType.setItem(storeFactory.name, JSON.stringify(storeFactory.store));
if (storeFactory.middleWares.length) {
storeFactory.store = storeFactory.middleWares.reduce((currentValue, currentFunction) => currentFunction(currentValue) || currentValue, storeFactory.store);
}
updateStore(storeFactory.store);
};
}
function useStateMachine(actions) {
const { store, updateStore } = React.useContext(StateMachineContext);
return React.useMemo(() => ({
actions: actions
? Object.entries(actions).reduce((previous, [key, callback]) => (Object.assign(Object.assign({}, previous), { [key]: actionTemplate(updateStore, callback) })), {})
: {},
state: store,
}), [store, actions]);
}

@@ -93,0 +81,0 @@ exports.StateMachineProvider = StateMachineProvider;

/// <reference types="react" />
import { UpdateStore, Options, Actions, StateMachineOptions } from './types';
export declare const middleWare: <T extends unknown>(data: T) => T;
import { StateMachineOptions, ActionsArg, Actions } from './types';
export declare function createStore<T>(defaultStoreData: T, options?: StateMachineOptions): void;
export declare function StateMachineProvider<T>(props: T): JSX.Element;
export declare function useStateMachine<T>(updateStoreFunction?: UpdateStore<T>, options?: Options): {
actions: Actions;
export declare function useStateMachine<T, TActions extends ActionsArg<T> = ActionsArg<T>>(actions?: TActions): {
actions: Actions<T, TActions>;
state: T;
};
export declare type Store = Record<string, any>;
export declare type StoreUpdateFunction<T> = (store: T, payload: any) => T;
export declare type UpdateStore<T> = {
[key: string]: StoreUpdateFunction<T>;
export declare type DeepPartial<T> = {
[P in keyof T]?: DeepPartial<T[P]>;
};
export declare type Options = {
shouldReRenderApp?: boolean;
};
export declare type Action = <T extends object>(payload: T) => void;
export declare type Actions = {
[key: string]: Action;
};
export declare type StoreUpdateFunction<T> = (store: T, payload: DeepPartial<T>) => T;
export declare type Action<T> = (payload: DeepPartial<T>) => void;
export declare type Actions<T, K> = Record<keyof K, Action<T>>;
export declare type ActionArg<T> = (globalStore: T, payload: DeepPartial<T>) => T;
export declare type ActionsArg<T> = Record<string, ActionArg<T>>;
export declare type MiddleWare = <T>(arg: T) => T;

@@ -14,0 +11,0 @@ export declare type StateMachineOptions = {

{
"name": "little-state-machine",
"description": "State management made super simple",
"sideEffects": false,
"version": "4.0.0-beta.2",
"files": [
"dist"
],
"version": "4.0.0-rc.0",
"main": "dist/little-state-machine.js",

@@ -16,7 +20,6 @@ "module": "dist/little-state-machine.es.js",

"build:ie11": "rollup -c ./rollup/rollup.ie11.config.js",
"watch": "tsc --watch",
"release": "npm version",
"postrelease": "yarn publish && git push --follow-tags",
"test": "jest",
"testw": "yarn test -- --watchAll",
"test:watch": "yarn test -- --watchAll",
"prepublish": "yarn run clean && yarn build"

@@ -39,17 +42,6 @@ },

"@rollup/plugin-replace": "^2.3.2",
"@types/enzyme": "^3.9.0",
"@types/enzyme-adapter-react-16": "^1.0.5",
"@types/jest": "^24.0.17",
"@types/react": "^16.8.8",
"@types/react-test-renderer": "^16.8.1",
"coveralls": "^3.0.3",
"enzyme": "^3.9.0",
"enzyme-adapter-react-16": "^1.9.1",
"jest": "24.7.1",
"npm-run-all": "^4.1.5",
"prettier": "^2.1.2",
"react": "^16.8.4",
"react-dom": "^16.8.4",
"react-test-renderer": "^16.8.3",
"rimraf": "^2.6.3",
"rollup": "^2.10.9",

@@ -59,9 +51,7 @@ "rollup-plugin-terser": "^6.1.0",

"ts-jest": "^24.0.0",
"typescript": "^4.0.0",
"uglify-es": "^3.3.9"
"typescript": "^4.0.0"
},
"peerDependencies": {
"react": "^16.8.0",
"react-dom": "^16.8.0"
"react": "^16.8.0 || ^17"
}
}
<div align="center"><a href="https://lrz5wloklm.csb.app/"><img src="https://github.com/bluebill1049/little-state-machine/blob/master/docs/logo.png?raw=true" alt="Little State Machine - React Hooks for state management" width="140px" /></a>
<h1>Little State Machine</h2>
<h1>Little State Machine</h1>

@@ -65,11 +65,6 @@ State management made super simple

```tsx
const { actions, state } = useStateMachine<T>(
{
removeNameAction,
updateUserNameAction,
},
{
shouldReRenderApp: false, // This will prevent App from re-render and only update the store
},
);
const { actions, state } = useStateMachine<T>({
removeNameAction,
updateUserNameAction,
});
```

@@ -79,3 +74,3 @@

Check out the <a href="https://codesandbox.io/s/little-state-machine-demo-v4-u4njf">Demo</a>.
Check out the <a href="https://codesandbox.io/s/icy-dew-by1wd">Demo</a>.

@@ -91,3 +86,3 @@ 📋 `app.js`

createStore({
yourDetail: {},
yourDetail: { name: '' },
});

@@ -94,0 +89,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc