little-state-machine
Advanced tools
Comparing version 4.0.0-beta.1 to 4.0.0-beta.2
@@ -1,2 +0,2 @@ | ||
import { setStorageType, createStore, StateMachineProvider, useStateMachine } from './stateMachine'; | ||
export { setStorageType, createStore, StateMachineProvider, useStateMachine, }; | ||
import { createStore, StateMachineProvider, useStateMachine } from './stateMachine'; | ||
export { createStore, StateMachineProvider, useStateMachine }; |
@@ -7,8 +7,7 @@ import { createContext, useState, useMemo, createElement, useContext, useCallback } from 'react'; | ||
var getStoreData = (storageType, storeName) => { | ||
const sessionStorageData = storageType.getItem(storeName); | ||
try { | ||
return sessionStorageData ? JSON.parse(sessionStorageData) : undefined; | ||
return JSON.parse(storageType.getItem(storeName)); | ||
} | ||
catch (_a) { | ||
return undefined; | ||
return ''; | ||
} | ||
@@ -18,49 +17,41 @@ }; | ||
class StoreFactory { | ||
constructor(storageType, name) { | ||
this._name = STORE_DEFAULT_NAME; | ||
this.storageType = storageType; | ||
this._name = name; | ||
this._store = getStoreData(storageType, name); | ||
constructor(name, isClient) { | ||
this.name = STORE_DEFAULT_NAME; | ||
this.store = undefined; | ||
this.middleWares = []; | ||
this.storageType = | ||
isClient && typeof sessionStorage !== 'undefined' | ||
? window.sessionStorage | ||
: { | ||
getItem: (payload) => payload, | ||
setItem: (payload) => payload, | ||
clear: () => { }, | ||
length: 0, | ||
key: (payload) => payload.toString(), | ||
removeItem: () => { }, | ||
}; | ||
this.name = name; | ||
} | ||
get name() { | ||
return this._name; | ||
updateStore(defaultValues) { | ||
this.store = getStoreData(this.storageType, this.name) || defaultValues; | ||
} | ||
set name(value) { | ||
this._name = value; | ||
updateMiddleWares(middleWares) { | ||
return (this.middleWares = middleWares); | ||
} | ||
get store() { | ||
return getStoreData(this.storageType, name) || this._store; | ||
} | ||
set store(value) { | ||
this._store = value; | ||
} | ||
} | ||
var isUndefined = (val) => val === undefined; | ||
function setUpDevTools(storageType, name, store) { | ||
if (typeof window === 'undefined') | ||
return; | ||
window.STATE_MACHINE_DEBUG = (value) => storageType.setItem('___STATE_MACHINE_DEBUG__', value); | ||
window.STATE_MACHINE_RESET = () => storageType.clear(); | ||
window.STATE_MACHINE_GET_STORE = () => storageType.getItem(name); | ||
window.STATE_MACHINE_SAVE_TO = (name) => window.localStorage.setItem(name, JSON.stringify(store)); | ||
window.STATE_MACHINE_LOAD = ({ storeName, data, }) => storageType.setItem(name || '___STATE_MACHINE_DEBUG__', data || window.localStorage.getItem(storeName) || ''); | ||
window.__LSM_DEBUG__ = (value) => storageType.setItem('___LSM_DEBUG__', value); | ||
window.__LSM_RESET__ = () => storageType.clear(); | ||
window.__LSM_GET_STORE__ = () => storageType.getItem(name); | ||
window.__LSM_SAVE_TO__ = (name) => window.localStorage.setItem(name, JSON.stringify(store)); | ||
window.__LSM_LOAD__ = ({ storeName, data, }) => storageType.setItem(name || '___LSM_DEBUG__', data || window.localStorage.getItem(storeName) || ''); | ||
} | ||
const isClient = typeof window !== 'undefined'; | ||
let storageType = isClient && typeof sessionStorage !== 'undefined' | ||
? window.sessionStorage | ||
: { | ||
getItem: (payload) => payload, | ||
setItem: (payload) => payload, | ||
clear: () => { }, | ||
length: 0, | ||
key: (payload) => payload.toString(), | ||
removeItem: () => { }, | ||
}; | ||
let middleWaresArray = []; | ||
const storeFactory = new StoreFactory(storageType, STORE_DEFAULT_NAME); | ||
const middleWare = (data = '') => { | ||
if (data && isClient) { | ||
const storeFactory = new StoreFactory(STORE_DEFAULT_NAME, isClient); | ||
const middleWare = (data) => { | ||
if (data) { | ||
window[STORE_ACTION_NAME] = data; | ||
@@ -70,5 +61,2 @@ } | ||
}; | ||
function setStorageType(type) { | ||
storageType = type; | ||
} | ||
function createStore(defaultStoreData, options = { | ||
@@ -79,10 +67,11 @@ name: STORE_DEFAULT_NAME, | ||
options.name && (storeFactory.name = options.name); | ||
options.storageType && (storeFactory.storageType = options.storageType); | ||
if (process.env.NODE_ENV !== 'production' && isClient) { | ||
window[STORE_DEFAULT_NAME] = storeFactory.name; | ||
} | ||
middleWaresArray = options.middleWares; | ||
storeFactory.updateMiddleWares(options.middleWares); | ||
if (process.env.NODE_ENV !== 'production') { | ||
setUpDevTools(storageType, storeFactory.name, storeFactory.store); | ||
setUpDevTools(storeFactory.storageType, storeFactory.name, storeFactory.store); | ||
} | ||
storeFactory.store = storeFactory.store || defaultStoreData; | ||
storeFactory.updateStore(defaultStoreData); | ||
} | ||
@@ -102,43 +91,25 @@ const StateMachineContext = createContext({ | ||
const actionTemplate = ({ options, callback, updateStore, }) => (payload) => { | ||
let result; | ||
const debugName = callback ? callback.name : ''; | ||
if (process.env.NODE_ENV !== 'production') { | ||
const debugName = callback ? callback.name : ''; | ||
middleWare(debugName); | ||
} | ||
if (callback) { | ||
result = callback(storeFactory.store, payload); | ||
storeFactory.store = callback(storeFactory.store, payload); | ||
} | ||
storeFactory.store = isUndefined(result) ? storeFactory.store : result; | ||
storageType.setItem(storeFactory.name, JSON.stringify(storeFactory.store)); | ||
if (isUndefined(options) || | ||
(options && options.shouldReRenderApp !== false)) { | ||
let pipeData = storeFactory.store; | ||
if (Array.isArray(middleWaresArray) && middleWaresArray.length) { | ||
pipeData = middleWaresArray.reduce((currentValue, currentFunction) => currentFunction(currentValue) || currentValue, pipeData); | ||
} | ||
updateStore(pipeData); | ||
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); | ||
if (updateStoreFunction && Object.keys(updateStoreFunction).length) { | ||
return { | ||
actions: Object.entries(updateStoreFunction).reduce((previous, [key, callback]) => (Object.assign(Object.assign({}, previous), { [key]: useCallback(actionTemplate({ | ||
return { | ||
actions: updateStoreFunction | ||
? Object.entries(updateStoreFunction).reduce((previous, [key, callback]) => (Object.assign(Object.assign({}, previous), { [key]: useCallback(actionTemplate({ | ||
options, | ||
callback, | ||
updateStore, | ||
}), []) })), {}), | ||
action: (p) => p, | ||
state: globalState, | ||
}; | ||
} | ||
return { | ||
actions: {}, | ||
action: useCallback(updateStoreFunction | ||
? actionTemplate({ | ||
options, | ||
callback: updateStoreFunction, | ||
updateStore, | ||
}) | ||
: () => { }, []), | ||
}), []) })), {}) | ||
: {}, | ||
state: globalState, | ||
@@ -148,2 +119,2 @@ }; | ||
export { StateMachineProvider, createStore, setStorageType, useStateMachine }; | ||
export { StateMachineProvider, createStore, useStateMachine }; |
@@ -5,8 +5,4 @@ 'use strict'; | ||
var _Object$keys = require("@babel/runtime-corejs3/core-js-stable/object/keys"); | ||
var _reduceInstanceProperty = require("@babel/runtime-corejs3/core-js-stable/instance/reduce"); | ||
var _Array$isArray = require("@babel/runtime-corejs3/core-js-stable/array/is-array"); | ||
var _JSON$stringify = require("@babel/runtime-corejs3/core-js-stable/json/stringify"); | ||
@@ -87,8 +83,6 @@ | ||
var getStoreData = function (storageType, storeName) { | ||
var sessionStorageData = storageType.getItem(storeName); | ||
try { | ||
return sessionStorageData ? JSON.parse(sessionStorageData) : undefined; | ||
return JSON.parse(storageType.getItem(storeName)); | ||
} catch (_a) { | ||
return undefined; | ||
return ''; | ||
} | ||
@@ -100,30 +94,30 @@ }; | ||
function () { | ||
function StoreFactory(storageType, name) { | ||
this._name = STORE_DEFAULT_NAME; | ||
this.storageType = storageType; | ||
this._name = name; | ||
this._store = getStoreData(storageType, name); | ||
function StoreFactory(name, isClient) { | ||
this.name = STORE_DEFAULT_NAME; | ||
this.store = undefined; | ||
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.name = name; | ||
} | ||
_Object$defineProperty(StoreFactory.prototype, "name", { | ||
get: function () { | ||
return this._name; | ||
}, | ||
set: function (value) { | ||
this._name = value; | ||
}, | ||
enumerable: false, | ||
configurable: true | ||
}); | ||
StoreFactory.prototype.updateStore = function (defaultValues) { | ||
this.store = getStoreData(this.storageType, this.name) || defaultValues; | ||
}; | ||
_Object$defineProperty(StoreFactory.prototype, "store", { | ||
get: function () { | ||
return getStoreData(this.storageType, name) || this._store; | ||
}, | ||
set: function (value) { | ||
this._store = value; | ||
}, | ||
enumerable: false, | ||
configurable: true | ||
}); | ||
StoreFactory.prototype.updateMiddleWares = function (middleWares) { | ||
return this.middleWares = middleWares; | ||
}; | ||
@@ -133,29 +127,25 @@ return StoreFactory; | ||
var isUndefined = function (val) { | ||
return val === undefined; | ||
}; | ||
function setUpDevTools(storageType, name, store) { | ||
if (typeof window === 'undefined') return; | ||
window.STATE_MACHINE_DEBUG = function (value) { | ||
return storageType.setItem('___STATE_MACHINE_DEBUG__', value); | ||
window.__LSM_DEBUG__ = function (value) { | ||
return storageType.setItem('___LSM_DEBUG__', value); | ||
}; | ||
window.STATE_MACHINE_RESET = function () { | ||
window.__LSM_RESET__ = function () { | ||
return storageType.clear(); | ||
}; | ||
window.STATE_MACHINE_GET_STORE = function () { | ||
window.__LSM_GET_STORE__ = function () { | ||
return storageType.getItem(name); | ||
}; | ||
window.STATE_MACHINE_SAVE_TO = function (name) { | ||
window.__LSM_SAVE_TO__ = function (name) { | ||
return window.localStorage.setItem(name, _JSON$stringify(store)); | ||
}; | ||
window.STATE_MACHINE_LOAD = function (_a) { | ||
window.__LSM_LOAD__ = function (_a) { | ||
var storeName = _a.storeName, | ||
data = _a.data; | ||
return storageType.setItem(name || '___STATE_MACHINE_DEBUG__', data || window.localStorage.getItem(storeName) || ''); | ||
return storageType.setItem(name || '___LSM_DEBUG__', data || window.localStorage.getItem(storeName) || ''); | ||
}; | ||
@@ -165,25 +155,6 @@ } | ||
var isClient = typeof window !== 'undefined'; | ||
var 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 () {} | ||
}; | ||
var middleWaresArray = []; | ||
var storeFactory = new StoreFactory(storageType, STORE_DEFAULT_NAME); | ||
var storeFactory = new StoreFactory(STORE_DEFAULT_NAME, isClient); | ||
var middleWare = function (data) { | ||
if (data === void 0) { | ||
data = ''; | ||
} | ||
if (data && isClient) { | ||
if (data) { | ||
window[STORE_ACTION_NAME] = data; | ||
@@ -195,6 +166,2 @@ } | ||
function setStorageType(type) { | ||
storageType = type; | ||
} | ||
function createStore(defaultStoreData, options) { | ||
@@ -209,2 +176,3 @@ if (options === void 0) { | ||
options.name && (storeFactory.name = options.name); | ||
options.storageType && (storeFactory.storageType = options.storageType); | ||
@@ -215,9 +183,9 @@ if (process.env.NODE_ENV !== 'production' && isClient) { | ||
middleWaresArray = options.middleWares; | ||
storeFactory.updateMiddleWares(options.middleWares); | ||
if (process.env.NODE_ENV !== 'production') { | ||
setUpDevTools(storageType, storeFactory.name, storeFactory.store); | ||
setUpDevTools(storeFactory.storageType, storeFactory.name, storeFactory.store); | ||
} | ||
storeFactory.store = storeFactory.store || defaultStoreData; | ||
storeFactory.updateStore(defaultStoreData); | ||
} | ||
@@ -253,6 +221,4 @@ | ||
return function (payload) { | ||
var result; | ||
var debugName = callback ? callback.name : ''; | ||
if (process.env.NODE_ENV !== 'production') { | ||
var debugName = callback ? callback.name : ''; | ||
middleWare(debugName); | ||
@@ -262,19 +228,16 @@ } | ||
if (callback) { | ||
result = callback(storeFactory.store, payload); | ||
storeFactory.store = callback(storeFactory.store, payload); | ||
} | ||
storeFactory.store = isUndefined(result) ? storeFactory.store : result; | ||
storageType.setItem(storeFactory.name, _JSON$stringify(storeFactory.store)); | ||
storeFactory.storageType.setItem(storeFactory.name, _JSON$stringify(storeFactory.store)); | ||
if (isUndefined(options) || options && options.shouldReRenderApp !== false) { | ||
var pipeData = storeFactory.store; | ||
if (storeFactory.middleWares.length) { | ||
var _context; | ||
if (_Array$isArray(middleWaresArray) && middleWaresArray.length) { | ||
pipeData = _reduceInstanceProperty(middleWaresArray).call(middleWaresArray, function (currentValue, currentFunction) { | ||
return currentFunction(currentValue) || currentValue; | ||
}, pipeData); | ||
} | ||
storeFactory.store = _reduceInstanceProperty(_context = storeFactory.middleWares).call(_context, function (currentValue, currentFunction) { | ||
return currentFunction(currentValue) || currentValue; | ||
}, storeFactory.store); | ||
} | ||
updateStore(pipeData); | ||
} | ||
!options && updateStore(storeFactory.store); | ||
}; | ||
@@ -284,2 +247,4 @@ }; | ||
function useStateMachine(updateStoreFunction, options) { | ||
var _context2; | ||
var _a = React.useContext(StateMachineContext), | ||
@@ -289,33 +254,16 @@ globalState = _a.store, | ||
if (updateStoreFunction && _Object$keys(updateStoreFunction).length) { | ||
var _context; | ||
return { | ||
actions: updateStoreFunction ? _reduceInstanceProperty(_context2 = _Object$entries(updateStoreFunction)).call(_context2, function (previous, _a) { | ||
var _b; | ||
return { | ||
actions: _reduceInstanceProperty(_context = _Object$entries(updateStoreFunction)).call(_context, function (previous, _a) { | ||
var _b; | ||
var _c = __read(_a, 2), | ||
key = _c[0], | ||
callback = _c[1]; | ||
var _c = __read(_a, 2), | ||
key = _c[0], | ||
callback = _c[1]; | ||
return __assign(__assign({}, previous), (_b = {}, _b[key] = React.useCallback(actionTemplate({ | ||
options: options, | ||
callback: callback, | ||
updateStore: updateStore | ||
}), []), _b)); | ||
}, {}), | ||
action: function (p) { | ||
return p; | ||
}, | ||
state: globalState | ||
}; | ||
} | ||
return { | ||
actions: {}, | ||
action: React.useCallback(updateStoreFunction ? actionTemplate({ | ||
options: options, | ||
callback: updateStoreFunction, | ||
updateStore: updateStore | ||
}) : function () {}, []), | ||
return __assign(__assign({}, previous), (_b = {}, _b[key] = React.useCallback(actionTemplate({ | ||
options: options, | ||
callback: callback, | ||
updateStore: updateStore | ||
}), []), _b)); | ||
}, {}) : {}, | ||
state: globalState | ||
@@ -327,3 +275,2 @@ }; | ||
exports.createStore = createStore; | ||
exports.setStorageType = setStorageType; | ||
exports.useStateMachine = useStateMachine; |
@@ -11,8 +11,7 @@ 'use strict'; | ||
var getStoreData = (storageType, storeName) => { | ||
const sessionStorageData = storageType.getItem(storeName); | ||
try { | ||
return sessionStorageData ? JSON.parse(sessionStorageData) : undefined; | ||
return JSON.parse(storageType.getItem(storeName)); | ||
} | ||
catch (_a) { | ||
return undefined; | ||
return ''; | ||
} | ||
@@ -22,49 +21,41 @@ }; | ||
class StoreFactory { | ||
constructor(storageType, name) { | ||
this._name = STORE_DEFAULT_NAME; | ||
this.storageType = storageType; | ||
this._name = name; | ||
this._store = getStoreData(storageType, name); | ||
constructor(name, isClient) { | ||
this.name = STORE_DEFAULT_NAME; | ||
this.store = undefined; | ||
this.middleWares = []; | ||
this.storageType = | ||
isClient && typeof sessionStorage !== 'undefined' | ||
? window.sessionStorage | ||
: { | ||
getItem: (payload) => payload, | ||
setItem: (payload) => payload, | ||
clear: () => { }, | ||
length: 0, | ||
key: (payload) => payload.toString(), | ||
removeItem: () => { }, | ||
}; | ||
this.name = name; | ||
} | ||
get name() { | ||
return this._name; | ||
updateStore(defaultValues) { | ||
this.store = getStoreData(this.storageType, this.name) || defaultValues; | ||
} | ||
set name(value) { | ||
this._name = value; | ||
updateMiddleWares(middleWares) { | ||
return (this.middleWares = middleWares); | ||
} | ||
get store() { | ||
return getStoreData(this.storageType, name) || this._store; | ||
} | ||
set store(value) { | ||
this._store = value; | ||
} | ||
} | ||
var isUndefined = (val) => val === undefined; | ||
function setUpDevTools(storageType, name, store) { | ||
if (typeof window === 'undefined') | ||
return; | ||
window.STATE_MACHINE_DEBUG = (value) => storageType.setItem('___STATE_MACHINE_DEBUG__', value); | ||
window.STATE_MACHINE_RESET = () => storageType.clear(); | ||
window.STATE_MACHINE_GET_STORE = () => storageType.getItem(name); | ||
window.STATE_MACHINE_SAVE_TO = (name) => window.localStorage.setItem(name, JSON.stringify(store)); | ||
window.STATE_MACHINE_LOAD = ({ storeName, data, }) => storageType.setItem(name || '___STATE_MACHINE_DEBUG__', data || window.localStorage.getItem(storeName) || ''); | ||
window.__LSM_DEBUG__ = (value) => storageType.setItem('___LSM_DEBUG__', value); | ||
window.__LSM_RESET__ = () => storageType.clear(); | ||
window.__LSM_GET_STORE__ = () => storageType.getItem(name); | ||
window.__LSM_SAVE_TO__ = (name) => window.localStorage.setItem(name, JSON.stringify(store)); | ||
window.__LSM_LOAD__ = ({ storeName, data, }) => storageType.setItem(name || '___LSM_DEBUG__', data || window.localStorage.getItem(storeName) || ''); | ||
} | ||
const isClient = typeof window !== 'undefined'; | ||
let storageType = isClient && typeof sessionStorage !== 'undefined' | ||
? window.sessionStorage | ||
: { | ||
getItem: (payload) => payload, | ||
setItem: (payload) => payload, | ||
clear: () => { }, | ||
length: 0, | ||
key: (payload) => payload.toString(), | ||
removeItem: () => { }, | ||
}; | ||
let middleWaresArray = []; | ||
const storeFactory = new StoreFactory(storageType, STORE_DEFAULT_NAME); | ||
const middleWare = (data = '') => { | ||
if (data && isClient) { | ||
const storeFactory = new StoreFactory(STORE_DEFAULT_NAME, isClient); | ||
const middleWare = (data) => { | ||
if (data) { | ||
window[STORE_ACTION_NAME] = data; | ||
@@ -74,5 +65,2 @@ } | ||
}; | ||
function setStorageType(type) { | ||
storageType = type; | ||
} | ||
function createStore(defaultStoreData, options = { | ||
@@ -83,10 +71,11 @@ name: STORE_DEFAULT_NAME, | ||
options.name && (storeFactory.name = options.name); | ||
options.storageType && (storeFactory.storageType = options.storageType); | ||
if (process.env.NODE_ENV !== 'production' && isClient) { | ||
window[STORE_DEFAULT_NAME] = storeFactory.name; | ||
} | ||
middleWaresArray = options.middleWares; | ||
storeFactory.updateMiddleWares(options.middleWares); | ||
if (process.env.NODE_ENV !== 'production') { | ||
setUpDevTools(storageType, storeFactory.name, storeFactory.store); | ||
setUpDevTools(storeFactory.storageType, storeFactory.name, storeFactory.store); | ||
} | ||
storeFactory.store = storeFactory.store || defaultStoreData; | ||
storeFactory.updateStore(defaultStoreData); | ||
} | ||
@@ -106,43 +95,25 @@ const StateMachineContext = React.createContext({ | ||
const actionTemplate = ({ options, callback, updateStore, }) => (payload) => { | ||
let result; | ||
const debugName = callback ? callback.name : ''; | ||
if (process.env.NODE_ENV !== 'production') { | ||
const debugName = callback ? callback.name : ''; | ||
middleWare(debugName); | ||
} | ||
if (callback) { | ||
result = callback(storeFactory.store, payload); | ||
storeFactory.store = callback(storeFactory.store, payload); | ||
} | ||
storeFactory.store = isUndefined(result) ? storeFactory.store : result; | ||
storageType.setItem(storeFactory.name, JSON.stringify(storeFactory.store)); | ||
if (isUndefined(options) || | ||
(options && options.shouldReRenderApp !== false)) { | ||
let pipeData = storeFactory.store; | ||
if (Array.isArray(middleWaresArray) && middleWaresArray.length) { | ||
pipeData = middleWaresArray.reduce((currentValue, currentFunction) => currentFunction(currentValue) || currentValue, pipeData); | ||
} | ||
updateStore(pipeData); | ||
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); | ||
if (updateStoreFunction && Object.keys(updateStoreFunction).length) { | ||
return { | ||
actions: Object.entries(updateStoreFunction).reduce((previous, [key, callback]) => (Object.assign(Object.assign({}, previous), { [key]: React.useCallback(actionTemplate({ | ||
return { | ||
actions: updateStoreFunction | ||
? Object.entries(updateStoreFunction).reduce((previous, [key, callback]) => (Object.assign(Object.assign({}, previous), { [key]: React.useCallback(actionTemplate({ | ||
options, | ||
callback, | ||
updateStore, | ||
}), []) })), {}), | ||
action: (p) => p, | ||
state: globalState, | ||
}; | ||
} | ||
return { | ||
actions: {}, | ||
action: React.useCallback(updateStoreFunction | ||
? actionTemplate({ | ||
options, | ||
callback: updateStoreFunction, | ||
updateStore, | ||
}) | ||
: () => { }, []), | ||
}), []) })), {}) | ||
: {}, | ||
state: globalState, | ||
@@ -154,3 +125,2 @@ }; | ||
exports.createStore = createStore; | ||
exports.setStorageType = setStorageType; | ||
exports.useStateMachine = useStateMachine; |
@@ -1,1 +0,1 @@ | ||
import{createContext as e,useState as t,useMemo as o,createElement as n,useContext as s,useCallback as r}from"react";var a=(e,t)=>{const o=e.getItem(t);try{return o?JSON.parse(o):void 0}catch(e){return}};var i=e=>void 0===e;const _="undefined"!=typeof window;let c=_&&"undefined"!=typeof sessionStorage?window.sessionStorage:{getItem:e=>e,setItem:e=>e,clear:()=>{},length:0,key:e=>e.toString(),removeItem:()=>{}},d=[];const m=new class{constructor(e,t){this._name="__LSM__",this.storageType=e,this._name=t,this._store=a(e,t)}get name(){return this._name}set name(e){this._name=e}get store(){return a(this.storageType,name)||this._store}set store(e){this._store=e}}(c,"__LSM__");function E(e){c=e}function S(e,t={name:"__LSM__",middleWares:[]}){t.name&&(m.name=t.name),"production"!==process.env.NODE_ENV&&_&&(window.__LSM__=m.name),d=t.middleWares,"production"!==process.env.NODE_ENV&&function(e,t,o){"undefined"!=typeof window&&(window.STATE_MACHINE_DEBUG=t=>e.setItem("___STATE_MACHINE_DEBUG__",t),window.STATE_MACHINE_RESET=()=>e.clear(),window.STATE_MACHINE_GET_STORE=()=>e.getItem(t),window.STATE_MACHINE_SAVE_TO=e=>window.localStorage.setItem(e,JSON.stringify(o)),window.STATE_MACHINE_LOAD=({storeName:o,data:n})=>e.setItem(t||"___STATE_MACHINE_DEBUG__",n||window.localStorage.getItem(o)||""))}(c,m.name,m.store),m.store=m.store||e}const u=e({store:m.store,updateStore:e=>e});function p(e){const[s,r]=t(m.store),a=o(()=>({store:s,updateStore:r}),[s]);return n(u.Provider,Object.assign({value:a},e))}const w=({options:e,callback:t,updateStore:o})=>n=>{let s;const r=t?t.name:"";if("production"!==process.env.NODE_ENV&&((e="")=>{e&&_&&(window.__LSM_NAME__=e)})(r),t&&(s=t(m.store,n)),m.store=i(s)?m.store:s,c.setItem(m.name,JSON.stringify(m.store)),i(e)||e&&!1!==e.shouldReRenderApp){let e=m.store;Array.isArray(d)&&d.length&&(e=d.reduce((e,t)=>t(e)||e,e)),o(e)}};function l(e,t){const{store:o,updateStore:n}=s(u);return e&&Object.keys(e).length?{actions:Object.entries(e).reduce((e,[o,s])=>Object.assign(Object.assign({},e),{[o]:r(w({options:t,callback:s,updateStore:n}),[])}),{}),action:e=>e,state:o}:{actions:{},action:r(e?w({options:t,callback:e,updateStore:n}):()=>{},[]),state:o}}export{p as StateMachineProvider,S as createStore,E as setStorageType,l as useStateMachine}; | ||
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}; |
@@ -10,8 +10,7 @@ (function (global, factory) { | ||
var getStoreData = (storageType, storeName) => { | ||
const sessionStorageData = storageType.getItem(storeName); | ||
try { | ||
return sessionStorageData ? JSON.parse(sessionStorageData) : undefined; | ||
return JSON.parse(storageType.getItem(storeName)); | ||
} | ||
catch (_a) { | ||
return undefined; | ||
return ''; | ||
} | ||
@@ -21,40 +20,29 @@ }; | ||
class StoreFactory { | ||
constructor(storageType, name) { | ||
this._name = STORE_DEFAULT_NAME; | ||
this.storageType = storageType; | ||
this._name = name; | ||
this._store = getStoreData(storageType, name); | ||
constructor(name, isClient) { | ||
this.name = STORE_DEFAULT_NAME; | ||
this.store = undefined; | ||
this.middleWares = []; | ||
this.storageType = | ||
isClient && typeof sessionStorage !== 'undefined' | ||
? window.sessionStorage | ||
: { | ||
getItem: (payload) => payload, | ||
setItem: (payload) => payload, | ||
clear: () => { }, | ||
length: 0, | ||
key: (payload) => payload.toString(), | ||
removeItem: () => { }, | ||
}; | ||
this.name = name; | ||
} | ||
get name() { | ||
return this._name; | ||
updateStore(defaultValues) { | ||
this.store = getStoreData(this.storageType, this.name) || defaultValues; | ||
} | ||
set name(value) { | ||
this._name = value; | ||
updateMiddleWares(middleWares) { | ||
return (this.middleWares = middleWares); | ||
} | ||
get store() { | ||
return getStoreData(this.storageType, name) || this._store; | ||
} | ||
set store(value) { | ||
this._store = value; | ||
} | ||
} | ||
var isUndefined = (val) => val === undefined; | ||
const isClient = typeof window !== 'undefined'; | ||
let storageType = isClient && typeof sessionStorage !== 'undefined' | ||
? window.sessionStorage | ||
: { | ||
getItem: (payload) => payload, | ||
setItem: (payload) => payload, | ||
clear: () => { }, | ||
length: 0, | ||
key: (payload) => payload.toString(), | ||
removeItem: () => { }, | ||
}; | ||
let middleWaresArray = []; | ||
const storeFactory = new StoreFactory(storageType, STORE_DEFAULT_NAME); | ||
function setStorageType(type) { | ||
storageType = type; | ||
} | ||
const storeFactory = new StoreFactory(STORE_DEFAULT_NAME, isClient); | ||
function createStore(defaultStoreData, options = { | ||
@@ -65,4 +53,5 @@ name: STORE_DEFAULT_NAME, | ||
options.name && (storeFactory.name = options.name); | ||
middleWaresArray = options.middleWares; | ||
storeFactory.store = storeFactory.store || defaultStoreData; | ||
options.storageType && (storeFactory.storageType = options.storageType); | ||
storeFactory.updateMiddleWares(options.middleWares); | ||
storeFactory.updateStore(defaultStoreData); | ||
} | ||
@@ -82,40 +71,21 @@ const StateMachineContext = React.createContext({ | ||
const actionTemplate = ({ options, callback, updateStore, }) => (payload) => { | ||
let result; | ||
const debugName = callback ? callback.name : ''; | ||
if (callback) { | ||
result = callback(storeFactory.store, payload); | ||
storeFactory.store = callback(storeFactory.store, payload); | ||
} | ||
storeFactory.store = isUndefined(result) ? storeFactory.store : result; | ||
storageType.setItem(storeFactory.name, JSON.stringify(storeFactory.store)); | ||
if (isUndefined(options) || | ||
(options && options.shouldReRenderApp !== false)) { | ||
let pipeData = storeFactory.store; | ||
if (Array.isArray(middleWaresArray) && middleWaresArray.length) { | ||
pipeData = middleWaresArray.reduce((currentValue, currentFunction) => currentFunction(currentValue) || currentValue, pipeData); | ||
} | ||
updateStore(pipeData); | ||
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); | ||
if (updateStoreFunction && Object.keys(updateStoreFunction).length) { | ||
return { | ||
actions: Object.entries(updateStoreFunction).reduce((previous, [key, callback]) => (Object.assign(Object.assign({}, previous), { [key]: React.useCallback(actionTemplate({ | ||
return { | ||
actions: updateStoreFunction | ||
? Object.entries(updateStoreFunction).reduce((previous, [key, callback]) => (Object.assign(Object.assign({}, previous), { [key]: React.useCallback(actionTemplate({ | ||
options, | ||
callback, | ||
updateStore, | ||
}), []) })), {}), | ||
action: (p) => p, | ||
state: globalState, | ||
}; | ||
} | ||
return { | ||
actions: {}, | ||
action: React.useCallback(updateStoreFunction | ||
? actionTemplate({ | ||
options, | ||
callback: updateStoreFunction, | ||
updateStore, | ||
}) | ||
: () => { }, []), | ||
}), []) })), {}) | ||
: {}, | ||
state: globalState, | ||
@@ -127,3 +97,2 @@ }; | ||
exports.createStore = createStore; | ||
exports.setStorageType = setStorageType; | ||
exports.useStateMachine = useStateMachine; | ||
@@ -130,0 +99,0 @@ |
@@ -1,10 +0,10 @@ | ||
export default class StoreFactory<T> { | ||
private _name; | ||
private _store; | ||
import { MiddleWare } from '../types'; | ||
export default class StoreFactory { | ||
name: string; | ||
storageType: Storage; | ||
constructor(storageType: Storage, name: string); | ||
get name(): string; | ||
set name(value: string); | ||
get store(): T; | ||
set store(value: T); | ||
store: unknown; | ||
middleWares: MiddleWare[]; | ||
constructor(name: string, isClient: boolean); | ||
updateStore<T>(defaultValues: T): void; | ||
updateMiddleWares(middleWares: MiddleWare[]): MiddleWare[]; | ||
} |
/// <reference types="react" /> | ||
import { UpdateStore, Options, Action, Actions, StateMachineOptions } from './types'; | ||
export declare const middleWare: (data?: string) => string; | ||
export declare function setStorageType(type: Storage): void; | ||
import { UpdateStore, Options, Actions, StateMachineOptions } from './types'; | ||
export declare const middleWare: <T extends unknown>(data: T) => T; | ||
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): { | ||
action: Action; | ||
actions: Actions; | ||
state: T; | ||
}; |
export declare type Store = Record<string, any>; | ||
export declare type StoreUpdateFunction<T> = (store: T, payload: any) => T; | ||
export declare type UpdateStore<T> = StoreUpdateFunction<T> | { | ||
export declare type UpdateStore<T> = { | ||
[key: string]: StoreUpdateFunction<T>; | ||
@@ -13,5 +13,7 @@ }; | ||
}; | ||
export declare type MiddleWare = <T>(arg: T) => T; | ||
export declare type StateMachineOptions = { | ||
name: string; | ||
middleWares?: Function[]; | ||
middleWares: MiddleWare[]; | ||
storageType?: Storage; | ||
}; | ||
@@ -22,9 +24,9 @@ declare global { | ||
__LSM__: any; | ||
STATE_MACHINE_DEBUG: any; | ||
STATE_MACHINE_RESET: any; | ||
STATE_MACHINE_GET_STORE: any; | ||
STATE_MACHINE_SAVE_TO: any; | ||
STATE_MACHINE_LOAD: any; | ||
STATE_MACHINE_DEBUG_NAME: any; | ||
__LSM_DEBUG__: any; | ||
__LSM_RESET__: any; | ||
__LSM_GET_STORE__: any; | ||
__LSM_SAVE_TO__: any; | ||
__LSM_LOAD__: any; | ||
__LSM_DEBUG_NAME__: any; | ||
} | ||
} |
{ | ||
"name": "little-state-machine", | ||
"sideEffects": false, | ||
"version": "4.0.0-beta.1", | ||
"version": "4.0.0-beta.2", | ||
"main": "dist/little-state-machine.js", | ||
@@ -6,0 +6,0 @@ "module": "dist/little-state-machine.es.js", |
111
README.md
@@ -17,6 +17,5 @@ <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 (less than 1.5kb) | ||
- Tiny with 0 dependency and simple (less than 1kb) | ||
- Persist state by default (`sessionStorage` or `localStorage`) | ||
- Build with React Hooks | ||
- Compatible with React Native | ||
@@ -27,6 +26,2 @@ <h2>📦 Installation</h2> | ||
<h2>🖥 <a href="https://codesandbox.io/s/lrz5wloklm">Demo</a></h2> | ||
Check out the <a href="https://codesandbox.io/s/lrz5wloklm">Demo</a>. | ||
<br /> | ||
<h2>🕹 API</h2> | ||
@@ -40,3 +35,3 @@ | ||
```ts | ||
```tsx | ||
createStore(store, options?: { | ||
@@ -50,3 +45,3 @@ name: string; // rename the store | ||
```ts | ||
```tsx | ||
import yourDetail from './state/yourDetail'; | ||
@@ -56,2 +51,3 @@ | ||
console.log(store); | ||
return store; | ||
} | ||
@@ -73,15 +69,12 @@ | ||
```typescript | ||
import { updateUserNameAction, removeNameAction } from './actions/yourDetails'; | ||
const { action, state } = useStateMachine<T>(updateUserNameAction); | ||
const { actions, state } = useStateMachine<T>({ | ||
removeNameAction, | ||
updateUserNameAction, | ||
}); | ||
// The following examples are for optional argument | ||
const { action, state } = useStateMachine<T>(updateUserNameAction, { | ||
shouldReRenderApp: false, // This will prevent App from re-render and only update the store | ||
}); | ||
```tsx | ||
const { actions, state } = useStateMachine<T>( | ||
{ | ||
removeNameAction, | ||
updateUserNameAction, | ||
}, | ||
{ | ||
shouldReRenderApp: false, // This will prevent App from re-render and only update the store | ||
}, | ||
); | ||
``` | ||
@@ -91,56 +84,26 @@ | ||
Check out the <a href="https://codesandbox.io/s/little-state-machine-demo-v4-u4njf">Demo</a>. | ||
📋 `app.js` | ||
```jsx | ||
```tsx | ||
import React from 'react'; | ||
import yourDetail from './yourDetail'; | ||
import YourComponent from './yourComponent'; | ||
import { StateMachineProvider, createStore } from 'little-state-machine'; | ||
import { DevTool } from 'little-state-machine-devtools'; | ||
// create your store | ||
createStore({ | ||
yourDetail, | ||
yourDetail: {}, | ||
}); | ||
export default () => { | ||
return ( | ||
<StateMachineProvider> | ||
<DevTool /> | ||
<YourComponent /> | ||
</StateMachineProvider> | ||
); | ||
}; | ||
export default () => ( | ||
<StateMachineProvider> | ||
<YourComponent /> | ||
</StateMachineProvider> | ||
); | ||
``` | ||
📋 `yourComponent.js` | ||
```jsx | ||
import React from 'react'; | ||
import { updateName } from './action.js'; | ||
import { useStateMachine } from 'little-state-machine'; | ||
export default function YourComponent() { | ||
const { | ||
action, | ||
state: { | ||
yourDetail: { name }, | ||
}, | ||
} = useStateMachine(updateName); | ||
return <div onClick={() => action({ name: 'bill' })}>{name}</div>; | ||
} | ||
``` | ||
📋 `yourDetail.js` | ||
```js | ||
export default { | ||
name: 'test', | ||
}; | ||
``` | ||
📋 `action.js` | ||
```js | ||
```tsx | ||
export function updateName(state, payload) { | ||
@@ -157,7 +120,25 @@ return { | ||
<h2>⚒ Little State Machine DevTool</h2> | ||
📋 `yourComponent.js` | ||
```tsx | ||
import React from 'react'; | ||
import { updateName } from './action.js'; | ||
import { useStateMachine } from 'little-state-machine'; | ||
export default function YourComponent() { | ||
const { actions, state } = useStateMachine(updateName); | ||
return ( | ||
<div onClick={() => actions.updateName({ name: 'bill' })}> | ||
{state.yourDetail.name} | ||
</div> | ||
); | ||
} | ||
``` | ||
<h2>⚒ DevTool</h2> | ||
[DevTool](https://github.com/bluebill1049/little-state-machine-dev-tools) component to track your state change and action. | ||
```jsx | ||
```tsx | ||
import { DevTool } from 'little-state-machine-devtools'; | ||
@@ -181,3 +162,3 @@ | ||
```js | ||
```tsx | ||
import { createStore } from 'little-state-machine/dist/little-state-machine.ie11'; | ||
@@ -193,3 +174,3 @@ ``` | ||
```js | ||
```tsx | ||
if (!Object.entries) { | ||
@@ -196,0 +177,0 @@ Object.entries = function (obj) { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
37503
19
646
311