@mapoio/react-state-manage
Advanced tools
Comparing version 1.13.22 to 1.14.0
@@ -5,2 +5,11 @@ # Change Log | ||
# [1.14.0](https://github.com/mapoio/react-state-manage/compare/v1.13.22...v1.14.0) (2020-01-20) | ||
### Features | ||
* Add subscribe ([f86245f](https://github.com/mapoio/react-state-manage/commit/f86245f)) | ||
## [1.13.22](https://github.com/mapoio/react-state-manage/compare/v1.13.21...v1.13.22) (2019-10-11) | ||
@@ -7,0 +16,0 @@ |
@@ -6,7 +6,19 @@ import { Opt, Reducers, Effects, Selector, ActionSelector, Config } from './typings'; | ||
declare const createStore: <S, R extends Reducers<S>, E extends Effects>(opt: Opt<S, R, E>) => { | ||
[x: number]: () => { | ||
[x: number]: () => { | ||
[x: number]: any; | ||
subscribe(observer: unknown): { | ||
unsubscribe: () => void; | ||
}; | ||
}; | ||
subscribe(observer: unknown): { | ||
unsubscribe: () => void; | ||
}; | ||
}; | ||
useStore: <P extends any>(selector: Selector<S, P>) => P; | ||
dispatch: <K extends any>(action: ActionSelector<R, E> | keyof R | keyof E, payload?: K | undefined) => any; | ||
getState: () => S; | ||
subscribe: (listener: () => void) => () => void; | ||
}; | ||
export default store; | ||
export { createStore }; |
@@ -39,2 +39,3 @@ import { useEffect, useState } from 'react'; | ||
var createStore = function (opt) { | ||
var _a; | ||
var updaters = []; | ||
@@ -62,2 +63,20 @@ var beforeDispatchs = config.beforeDispatchs; | ||
}; | ||
var subscribe = function (listener) { | ||
if (typeof listener !== 'function') { | ||
throw new Error('Expected the listener to be a function.'); | ||
} | ||
var update = function (set, nextState) { | ||
set(function () { return nextState; }); | ||
}; | ||
var isSubscribed = true; | ||
var updater = { update: update, set: listener }; | ||
updaters.push(updater); | ||
return function () { | ||
if (!isSubscribed) | ||
return; | ||
isSubscribed = false; | ||
var index = updaters.indexOf(updater); | ||
updaters.splice(index, 1); | ||
}; | ||
}; | ||
var dispatch = function (action, payload) { | ||
@@ -91,3 +110,38 @@ var actionName = getActionName(action); | ||
}; | ||
return { useStore: useStore, dispatch: dispatch, getState: getState }; | ||
function observable() { | ||
var _a; | ||
var outerSubscribe = subscribe; | ||
return _a = { | ||
subscribe: function (observer) { | ||
if (typeof observer !== 'object' || observer === null) { | ||
throw new TypeError('Expected the observer to be an object.'); | ||
} | ||
function observeState() { | ||
var observerAsObserver = observer; | ||
// @ts-ignore | ||
if (observerAsObserver.next) { | ||
// @ts-ignore | ||
observerAsObserver.next(getState()); | ||
} | ||
} | ||
observeState(); | ||
var unsubscribe = outerSubscribe(observeState); | ||
return { unsubscribe: unsubscribe }; | ||
} | ||
}, | ||
// @ts-ignore | ||
_a[$$observable] = function () { | ||
return this; | ||
}, | ||
_a; | ||
} | ||
return _a = { | ||
useStore: useStore, | ||
dispatch: dispatch, | ||
getState: getState, | ||
subscribe: subscribe | ||
}, | ||
// @ts-ignore | ||
_a[$$observable] = observable, | ||
_a; | ||
}; | ||
@@ -94,0 +148,0 @@ |
@@ -1,1 +0,1 @@ | ||
import{useEffect,useState}from"react";import produce from"immer";import cloneDeep from"clone-deep";function useMount(mount){useEffect(mount,[])}function useUnmount(unmount){useEffect(function(){return function(){if(unmount)unmount()}},[])}function getActionName(action){if(typeof action==="string")return action;try{var str=action.toString();var regAction=/return.*\.(.*)[;,}]/;var arr=str.match(regAction)||[];return arr[1]}catch(_a){throw new Error("action type or selector invalid")}}var config={beforeDispatchs:[],afterDispatchs:[],beforeUpdates:[]};var store={init:function(initConfig){Object.assign(config,initConfig)}};var createStore=function(opt){var updaters=[];var beforeDispatchs=config.beforeDispatchs;var afterDispatchs=config.afterDispatchs;var beforeUpdates=config.beforeUpdates;var prevState=cloneDeep(opt.state);var useStore=function(selector){var _a=useState(opt.state),state=_a[0],setState=_a[1];var update=function(set,nextState){set(function(){return nextState})};var updater={update:update,set:setState};useMount(function(){updaters.push(updater)});useUnmount(function(){updaters.splice(updaters.indexOf(updater),1)});return selector(state)};var dispatch=function(action,payload){var actionName=getActionName(action);beforeDispatchs.forEach(function(func){return func(cloneDeep(opt.state),actionName,payload)});if(opt.effects&&opt.effects[actionName]){return opt.effects[actionName](payload)}if(!updaters.length)return;var runAction=opt.reducers&&opt.reducers[actionName];if(runAction){var nextState_1=produce(opt.state,function(draft){runAction(draft,payload)});prevState=cloneDeep(opt.state);beforeUpdates.forEach(function(func){func(cloneDeep(prevState),cloneDeep(nextState_1),actionName,payload)});opt.state=nextState_1;updaters.forEach(function(updater){updater.update(updater.set,nextState_1)})}afterDispatchs.forEach(function(func){return func(cloneDeep(opt.state),actionName,payload)})};var getState=function(){return opt.state};return{useStore:useStore,dispatch:dispatch,getState:getState}};export default store;export{createStore}; | ||
import{useEffect,useState}from"react";import produce from"immer";import cloneDeep from"clone-deep";function useMount(mount){useEffect(mount,[])}function useUnmount(unmount){useEffect(function(){return function(){if(unmount)unmount()}},[])}function getActionName(action){if(typeof action==="string")return action;try{var str=action.toString();var regAction=/return.*\.(.*)[;,}]/;var arr=str.match(regAction)||[];return arr[1]}catch(_a){throw new Error("action type or selector invalid")}}var config={beforeDispatchs:[],afterDispatchs:[],beforeUpdates:[]};var store={init:function(initConfig){Object.assign(config,initConfig)}};var createStore=function(opt){var _a;var updaters=[];var beforeDispatchs=config.beforeDispatchs;var afterDispatchs=config.afterDispatchs;var beforeUpdates=config.beforeUpdates;var prevState=cloneDeep(opt.state);var useStore=function(selector){var _a=useState(opt.state),state=_a[0],setState=_a[1];var update=function(set,nextState){set(function(){return nextState})};var updater={update:update,set:setState};useMount(function(){updaters.push(updater)});useUnmount(function(){updaters.splice(updaters.indexOf(updater),1)});return selector(state)};var subscribe=function(listener){if(typeof listener!=="function"){throw new Error("Expected the listener to be a function.")}var update=function(set,nextState){set(function(){return nextState})};var isSubscribed=true;var updater={update:update,set:listener};updaters.push(updater);return function(){if(!isSubscribed)return;isSubscribed=false;var index=updaters.indexOf(updater);updaters.splice(index,1)}};var dispatch=function(action,payload){var actionName=getActionName(action);beforeDispatchs.forEach(function(func){return func(cloneDeep(opt.state),actionName,payload)});if(opt.effects&&opt.effects[actionName]){return opt.effects[actionName](payload)}if(!updaters.length)return;var runAction=opt.reducers&&opt.reducers[actionName];if(runAction){var nextState_1=produce(opt.state,function(draft){runAction(draft,payload)});prevState=cloneDeep(opt.state);beforeUpdates.forEach(function(func){func(cloneDeep(prevState),cloneDeep(nextState_1),actionName,payload)});opt.state=nextState_1;updaters.forEach(function(updater){updater.update(updater.set,nextState_1)})}afterDispatchs.forEach(function(func){return func(cloneDeep(opt.state),actionName,payload)})};var getState=function(){return opt.state};function observable(){var _a;var outerSubscribe=subscribe;return _a={subscribe:function(observer){if(typeof observer!=="object"||observer===null){throw new TypeError("Expected the observer to be an object.")}function observeState(){var observerAsObserver=observer;if(observerAsObserver.next){observerAsObserver.next(getState())}}observeState();var unsubscribe=outerSubscribe(observeState);return{unsubscribe:unsubscribe}}},_a[$$observable]=function(){return this},_a}return _a={useStore:useStore,dispatch:dispatch,getState:getState,subscribe:subscribe},_a[$$observable]=observable,_a};export default store;export{createStore}; |
@@ -0,1 +1,3 @@ | ||
import 'react'; | ||
interface Opt<S, R, E> { | ||
@@ -30,5 +32,17 @@ name?: string; | ||
declare const createStore: <S, R extends Reducers<S>, E extends Effects>(opt: Opt<S, R, E>) => { | ||
[x: number]: () => { | ||
[x: number]: () => { | ||
[x: number]: any; | ||
subscribe(observer: unknown): { | ||
unsubscribe: () => void; | ||
}; | ||
}; | ||
subscribe(observer: unknown): { | ||
unsubscribe: () => void; | ||
}; | ||
}; | ||
useStore: <P extends any>(selector: Selector<S, P>) => P; | ||
dispatch: <K extends any>(action: ActionSelector<R, E> | keyof R | keyof E, payload?: K | undefined) => any; | ||
getState: () => S; | ||
subscribe: (listener: () => void) => () => void; | ||
}; | ||
@@ -35,0 +49,0 @@ |
{ | ||
"name": "@mapoio/react-state-manage", | ||
"version": "1.13.22", | ||
"version": "1.14.0", | ||
"description": "", | ||
@@ -31,4 +31,3 @@ "main": "dist/index.js", | ||
"hooks": { | ||
"pre-commit": "lint-staged", | ||
"commit-msg": "commitlint -E HUSKY_GIT_PARAMS" | ||
"pre-commit": "lint-staged" | ||
} | ||
@@ -35,0 +34,0 @@ }, |
Sorry, the diff of this file is not supported yet
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
35139
257