little-state-machine
Advanced tools
Comparing version 4.0.0 to 4.0.1-beta.0
@@ -1,2 +0,2 @@ | ||
import{createContext as e,useState as t,createElement as n,useContext as r,useMemo as o}from"react";var a=new(function(){function e(e){void 0===e&&(e="__LSM__"),this.name=e,this.state={},this.middleWares=[],this.storageType="undefined"!=typeof sessionStorage?window.sessionStorage:{}}var t=e.prototype;return t.updateStore=function(e){this.state=function(e,t){try{return JSON.parse(e.getItem(t))}catch(e){return null}}(this.storageType,this.name)||e},t.updateMiddleWares=function(e){return this.middleWares=e},e}()),i=e(void 0),s=function(e){var r=e.children,o=t(a.state);return n(i.Provider,{value:{state:o[0],setState:o[1]}},r)};function _(e,t){var n,r,o;void 0===t&&(t={name:"__LSM__",middleWares:[]}),t.name&&(a.name=t.name),t.storageType&&(a.storageType=t.storageType),a.updateMiddleWares(t.middleWares),"production"!==process.env.NODE_ENV&&(n=a.storageType,r=a.name,o=a.state,"undefined"!=typeof window&&(window.__LSM_NAME__=r,window.__LSM_DEBUG__=function(e){return n.setItem("___LSM_DEBUG__",e)},window.__LSM_RESET__=function(){return n.clear()},window.__LSM_GET_STORE__=function(){return n.getItem(r)},window.__LSM_SAVE_TO__=function(e){return window.localStorage.setItem(e,JSON.stringify(o))},window.__LSM_LOAD__=function(e){return n.setItem(r||"___LSM_DEBUG__",e.data||window.localStorage.getItem(e.storeName)||"")})),a.updateStore(e)}function d(e){var t,n=(t=r(i),"production"!==process.env.NODE_ENV&&(t||console.error("StateMachine context is undefined, please verify you are calling useStateMachine() as child of a <StateMachineProvider> component.")),t),s=n.state,_=n.setState;return o(function(){return{actions:e?Object.entries(e).reduce(function(e,t){var n;return Object.assign({},e,((n={})[t[0]]=function(e,t){return function(n){"production"!==process.env.NODE_ENV&&(window.__LSM_NAME__=t?t.name:""),a.state=t(a.state,n),a.storageType.setItem(a.name,JSON.stringify(a.state)),a.middleWares.length&&(a.state=a.middleWares.reduce(function(e,t){return t(e)||e},a.state)),e(a.state)}}(_,t[1]),n))},{}):{},state:s}},[s,_,e])}export{s as StateMachineProvider,_ as createStore,d as useStateMachine}; | ||
import{createContext as e,useState as t,createElement as n,useContext as r,useRef as a}from"react";var o=new(function(){function e(e){void 0===e&&(e="__LSM__"),this.name=e,this.state={},this.middleWares=[];try{this.storageType="undefined"!=typeof sessionStorage?window.sessionStorage:{}}catch(e){this.storageType={}}}var t=e.prototype;return t.updateStore=function(e){this.state=function(e,t){try{return JSON.parse(e.getItem(t))}catch(e){return null}}(this.storageType,this.name)||e},t.updateMiddleWares=function(e){return this.middleWares=e},e}()),i=e(void 0),s=function(e){var r=e.children,a=t(o.state);return n(i.Provider,{value:{state:a[0],setState:a[1]}},r)};function _(e,t){var n,r,a;void 0===t&&(t={name:"__LSM__",middleWares:[]}),t.name&&(o.name=t.name),t.storageType&&(o.storageType=t.storageType),t.middleWares&&o.updateMiddleWares(t.middleWares),"production"!==process.env.NODE_ENV&&(n=o.storageType,r=o.name,a=o.state,"undefined"!=typeof window&&(window.__LSM__=r,window.__LSM_NAME__=r,window.__LSM_DEBUG__=function(e){return n.setItem("___LSM_DEBUG__",e)},window.__LSM_RESET__=function(){return n.removeItem(r)},window.__LSM_GET_STORE__=function(){return n.getItem(r)},window.__LSM_SAVE_TO__=function(e){return window.localStorage.setItem(e,JSON.stringify(a))},window.__LSM_LOAD__=function(e){return n.setItem(r||"___LSM_DEBUG__",e.data||window.localStorage.getItem(e.storeName)||"")})),o.updateStore(e)}function d(e){var t,n=(t=r(i),"production"!==process.env.NODE_ENV&&(t||console.error("StateMachine context is undefined, please verify you are calling useStateMachine() as child of a <StateMachineProvider> component.")),t),s=n.state,_=n.setState;return{actions:a(Object.entries(e||{}).reduce(function(e,t){var n;return Object.assign({},e,((n={})[t[0]]=function(e,t){return function(n){"production"!==process.env.NODE_ENV&&(window.__LSM_NAME__=t?t.name:""),o.state=t(o.state,n),o.middleWares.length&&(o.state=o.middleWares.reduce(function(e,r){return r(e,t.name,n)||e},o.state)),o.storageType.setItem(o.name,JSON.stringify(o.state)),e(o.state)}}(_,t[1]),n))},{})).current,state:s}}export{s as StateMachineProvider,_ as createStore,d as useStateMachine}; | ||
//# sourceMappingURL=little-state-machine.es.js.map |
@@ -1,2 +0,2 @@ | ||
var e=require("react"),t=new(function(){function e(e){void 0===e&&(e="__LSM__"),this.name=e,this.state={},this.middleWares=[],this.storageType="undefined"!=typeof sessionStorage?window.sessionStorage:{}}var t=e.prototype;return t.updateStore=function(e){this.state=function(e,t){try{return JSON.parse(e.getItem(t))}catch(e){return null}}(this.storageType,this.name)||e},t.updateMiddleWares=function(e){return this.middleWares=e},e}()),r=e.createContext(void 0);exports.StateMachineProvider=function(n){var a=n.children,s=e.useState(t.state);return e.createElement(r.Provider,{value:{state:s[0],setState:s[1]}},a)},exports.createStore=function(e,r){void 0===r&&(r={name:"__LSM__",middleWares:[]}),r.name&&(t.name=r.name),r.storageType&&(t.storageType=r.storageType),t.updateMiddleWares(r.middleWares),t.updateStore(e)},exports.useStateMachine=function(n){var a=e.useContext(r),s=a.state,i=a.setState;return e.useMemo(function(){return{actions:n?Object.entries(n).reduce(function(e,r){var n;return Object.assign({},e,((n={})[r[0]]=function(e,r){return function(n){t.state=r(t.state,n),t.storageType.setItem(t.name,JSON.stringify(t.state)),t.middleWares.length&&(t.state=t.middleWares.reduce(function(e,t){return t(e)||e},t.state)),e(t.state)}}(i,r[1]),n))},{}):{},state:s}},[s,i,n])}; | ||
var e=require("react"),t=new(function(){function e(e){void 0===e&&(e="__LSM__"),this.name=e,this.state={},this.middleWares=[];try{this.storageType="undefined"!=typeof sessionStorage?window.sessionStorage:{}}catch(e){this.storageType={}}}var t=e.prototype;return t.updateStore=function(e){this.state=function(e,t){try{return JSON.parse(e.getItem(t))}catch(e){return null}}(this.storageType,this.name)||e},t.updateMiddleWares=function(e){return this.middleWares=e},e}()),r=e.createContext(void 0);exports.StateMachineProvider=function(a){var n=a.children,s=e.useState(t.state);return e.createElement(r.Provider,{value:{state:s[0],setState:s[1]}},n)},exports.createStore=function(e,r){void 0===r&&(r={name:"__LSM__",middleWares:[]}),r.name&&(t.name=r.name),r.storageType&&(t.storageType=r.storageType),r.middleWares&&t.updateMiddleWares(r.middleWares),t.updateStore(e)},exports.useStateMachine=function(a){var n=e.useContext(r),s=n.state,i=n.setState;return{actions:e.useRef(Object.entries(a||{}).reduce(function(e,r){var a;return Object.assign({},e,((a={})[r[0]]=function(e,r){return function(a){t.state=r(t.state,a),t.middleWares.length&&(t.state=t.middleWares.reduce(function(e,t){return t(e,r.name,a)||e},t.state)),t.storageType.setItem(t.name,JSON.stringify(t.state)),e(t.state)}}(i,r[1]),a))},{})).current,state:s}}; | ||
//# sourceMappingURL=little-state-machine.js.map |
@@ -1,2 +0,2 @@ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("react")):"function"==typeof define&&define.amd?define(["exports","react"],t):t((e=e||self).littleStateMachine={},e.react)}(this,function(e,t){var n=new(function(){function e(e){void 0===e&&(e="__LSM__"),this.name=e,this.state={},this.middleWares=[],this.storageType="undefined"!=typeof sessionStorage?window.sessionStorage:{}}var t=e.prototype;return t.updateStore=function(e){this.state=function(e,t){try{return JSON.parse(e.getItem(t))}catch(e){return null}}(this.storageType,this.name)||e},t.updateMiddleWares=function(e){return this.middleWares=e},e}()),r=t.createContext(void 0);e.StateMachineProvider=function(e){var a=e.children,s=t.useState(n.state);return t.createElement(r.Provider,{value:{state:s[0],setState:s[1]}},a)},e.createStore=function(e,t){void 0===t&&(t={name:"__LSM__",middleWares:[]}),t.name&&(n.name=t.name),t.storageType&&(n.storageType=t.storageType),n.updateMiddleWares(t.middleWares),n.updateStore(e)},e.useStateMachine=function(e){var a=t.useContext(r),s=a.state,i=a.setState;return t.useMemo(function(){return{actions:e?Object.entries(e).reduce(function(e,t){var r;return Object.assign({},e,((r={})[t[0]]=function(e,t){return function(r){n.state=t(n.state,r),n.storageType.setItem(n.name,JSON.stringify(n.state)),n.middleWares.length&&(n.state=n.middleWares.reduce(function(e,t){return t(e)||e},n.state)),e(n.state)}}(i,t[1]),r))},{}):{},state:s}},[s,i,e])}}); | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("react")):"function"==typeof define&&define.amd?define(["exports","react"],t):t((e=e||self).littleStateMachine={},e.react)}(this,function(e,t){var n=new(function(){function e(e){void 0===e&&(e="__LSM__"),this.name=e,this.state={},this.middleWares=[];try{this.storageType="undefined"!=typeof sessionStorage?window.sessionStorage:{}}catch(e){this.storageType={}}}var t=e.prototype;return t.updateStore=function(e){this.state=function(e,t){try{return JSON.parse(e.getItem(t))}catch(e){return null}}(this.storageType,this.name)||e},t.updateMiddleWares=function(e){return this.middleWares=e},e}()),r=t.createContext(void 0);e.StateMachineProvider=function(e){var a=e.children,s=t.useState(n.state);return t.createElement(r.Provider,{value:{state:s[0],setState:s[1]}},a)},e.createStore=function(e,t){void 0===t&&(t={name:"__LSM__",middleWares:[]}),t.name&&(n.name=t.name),t.storageType&&(n.storageType=t.storageType),t.middleWares&&n.updateMiddleWares(t.middleWares),n.updateStore(e)},e.useStateMachine=function(e){var a=t.useContext(r),s=a.state,i=a.setState;return{actions:t.useRef(Object.entries(e||{}).reduce(function(e,t){var r;return Object.assign({},e,((r={})[t[0]]=function(e,t){return function(r){n.state=t(n.state,r),n.middleWares.length&&(n.state=n.middleWares.reduce(function(e,n){return n(e,t.name,r)||e},n.state)),n.storageType.setItem(n.name,JSON.stringify(n.state)),e(n.state)}}(i,t[1]),r))},{})).current,state:s}}}); | ||
//# sourceMappingURL=little-state-machine.umd.js.map |
@@ -13,3 +13,3 @@ import * as React from 'react'; | ||
}; | ||
export declare type MiddleWare = <T>(arg: T) => T; | ||
export declare type MiddleWare = (state: GlobalState, payload: any, callbackName: string) => GlobalState; | ||
export declare type StateMachineOptions = { | ||
@@ -16,0 +16,0 @@ name: string; |
@@ -8,3 +8,3 @@ { | ||
], | ||
"version": "4.0.0", | ||
"version": "4.0.1-beta.0", | ||
"main": "dist/little-state-machine.js", | ||
@@ -19,7 +19,4 @@ "module": "dist/little-state-machine.es.js", | ||
"build:es": "microbundle -f es --jsx React.createElement", | ||
"release": "npm version", | ||
"postrelease": "yarn publish && git push --follow-tags", | ||
"test": "jest", | ||
"test:watch": "yarn test -- --watchAll", | ||
"prepublish": "yarn run prebuild && yarn build" | ||
"test:watch": "yarn test --watchAll" | ||
}, | ||
@@ -42,2 +39,3 @@ "keywords": [ | ||
"rimraf": "^3.0.2", | ||
"semantic-release": "^17.3.7", | ||
"ts-jest": "^24.0.0", | ||
@@ -44,0 +42,0 @@ "typescript": "^4.0.0" |
@@ -17,3 +17,3 @@ <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> | ||
- Tiny with 0 dependency and simple (639B _gzip_) | ||
- Tiny with 0 dependency and simple (715B _gzip_) | ||
- Persist state by default (`sessionStorage` or `localStorage`) | ||
@@ -105,3 +105,3 @@ - Build with React Hooks | ||
function YourComponent() { | ||
const { actions, state } = useStateMachine(updateName); | ||
const { actions, state } = useStateMachine({ updateName }); | ||
@@ -126,2 +126,4 @@ return ( | ||
Checkout the [example](https://codesandbox.io/s/typescript-forked-xs30h). | ||
```ts | ||
@@ -163,3 +165,3 @@ import 'little-state-machine'; | ||
updateName | ||
})); | ||
}); | ||
@@ -166,0 +168,0 @@ return ( |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
41156
86
378
11
4