UNPKG

zoov

Version:

A React modular state-management solution, based on Zustand

286 lines (271 loc) 12.7 kB
'use strict'; var immer = require('immer'); var traditional = require('zustand/traditional'); var middleware = require('zustand/middleware'); var react = require('react'); var proxyCompare = require('proxy-compare'); const globalContext = new Map(); const scopeContext = react.createContext(globalContext); const defineProvider = (handler) => { const overrideScopeMap = new Map(); handler((module, options) => { overrideScopeMap.set(module, { buildOption: options }); }); return ({ children }) => { const scopeMap = react.useContext(scopeContext); const currentScopeMap = react.useMemo(() => new Map([...scopeMap, ...overrideScopeMap]), []); return react.createElement(scopeContext.Provider, { value: currentScopeMap }, children); }; }; const useScopeContext = () => { return react.useContext(scopeContext); }; const __buildScopeSymbol = Symbol('buildScope'); function simpleMemoizedFn(fn) { const affected = new WeakMap(); let cache = null; return function (state) { if (cache) { if (state === cache.lastState) { return cache.lastResult; } if (!proxyCompare.isChanged(cache.lastState, state, affected)) { return cache.lastResult; } } const proxy = proxyCompare.createProxy(state, affected); const lastResult = fn(proxy); cache = { lastResult, lastState: state }; return lastResult; }; } function extendActions(rawActions, rawModule) { const reducerKeys = Object.keys(rawActions); const reducers = reducerKeys.reduce((acc, key) => { acc[key] = (...args) => immer.produce((draft) => void rawActions[key](draft, ...args)); return acc; }, {}); return Object.assign(Object.assign({}, rawModule), { reducers }); } function extendComputed(computed, rawModule) { return Object.assign(Object.assign({}, rawModule), { computed }); } function extendMethods(builder, rawModule) { const builderFn = typeof builder === 'function' ? builder : (perform) => Object.keys(builder).reduce((acc, cur) => { acc[cur] = builder[cur].bind(perform); return acc; }, {}); return Object.assign(Object.assign({}, rawModule), { methodsBuilders: [...rawModule.methodsBuilders, builderFn] }); } function extendMiddleware(middleware, rawModule) { return Object.assign(Object.assign({}, rawModule), { middlewares: [...rawModule.middlewares, middleware] }); } function getScopeOrBuild(context, module) { var _a; let scopeRef = context.get(module); if (!scopeRef) { scopeRef = {}; context.set(module, scopeRef); } return ((_a = scopeRef.current) !== null && _a !== void 0 ? _a : (scopeRef.current = module[__buildScopeSymbol](scopeRef.buildOption))); } function buildModule(initialState, rawModule) { return () => { const buildScope = (props) => { const { defaultValue = {}, middleware: middleware$1 } = props || {}; const mergedState = Object.assign(Object.assign({}, initialState), defaultValue); const scopeReducer = (state, { type, payload }) => rawModule.reducers[type](...payload)(state); const stateCreator = middleware.redux(scopeReducer, mergedState); const middlewares = middleware$1 ? [middleware$1] : rawModule.middlewares; const computedHooksMap = {}; const computedRawMap = {}; const cachedActionsMap = new WeakMap(); const self = { store: traditional.createWithEqualityFn(middlewares.reduce((acc, middleware) => middleware(acc), stateCreator)), getComputed: () => computedRawMap, getComputedHooks: () => computedHooksMap, getActions: (context) => { const cachedAction = cachedActionsMap.get(context); if (cachedAction) return cachedAction; let actions = {}; actions.$reset = () => { self.store.setState(mergedState); }; actions.$setState = (...args) => { const newState = immer.produce((draft) => { const getters = args.slice(0, args.length - 1); const setter = args[args.length - 1]; for (let i = 0; i < getters.length; i++) { if (i === getters.length - 1) { if (typeof setter === 'function') { draft[getters[i]] = setter(draft[getters[i]]); } else { draft[getters[i]] = setter; } } else { draft = draft[getters[i]]; } } })(self.store.getState()); self.store.setState(newState); }; const dispatch = self.store.getState().dispatch; Object.keys(rawModule.reducers).forEach((key) => { actions[key] = (...args) => dispatch({ type: key, payload: args }); }); const getScope = (module) => { if (!module) return self; return getScopeOrBuild(context, module); }; let isBuildingMethods = false; const perform = { getState: (module) => getScope(module).getState(), getActions: (module) => { if (isBuildingMethods) throw new Error('should not call getActions in the method builder, call it inside a method.'); return getScope(module).getActions(context); }, getComputed: (module) => getScope(module).getComputed(), }; isBuildingMethods = true; rawModule.methodsBuilders.forEach((builder) => { actions = Object.assign(Object.assign({}, actions), builder(perform)); }); isBuildingMethods = false; cachedActionsMap.set(context, actions); return actions; }, getState: () => self.store.getState(), }; Object.keys(rawModule.computed).forEach((key) => { rawModule.computed[key] = simpleMemoizedFn(rawModule.computed[key]); Object.defineProperty(computedHooksMap, key, { get: () => self.store(rawModule.computed[key]), }); Object.defineProperty(computedRawMap, key, { get: () => rawModule.computed[key](self.getState()), }); }); rawModule.subscriptionBuilders.forEach((builder) => { self.store.subscribe(builder(mergedState)); }); return self; }; const useScope = () => { const context = useScopeContext(); return getScopeOrBuild(context, module); }; const module = { use: (selector, equalFn) => [module.useState(selector, equalFn), module.useActions(), module.useComputed()], useState: (selector, equalFn) => useScope().store(selector, equalFn), useActions: () => useScope().getActions(useScopeContext()), useComputed: () => useScope().getComputedHooks(), useStore: () => useScope().store, getStore: (context = globalContext) => getScopeOrBuild(context, module).store, getState: (context = globalContext) => getScopeOrBuild(context, module).getState(), getActions: (context = globalContext) => getScopeOrBuild(context, module).getActions(context), getComputed: (context = globalContext) => getScopeOrBuild(context, module).getComputed(), [__buildScopeSymbol]: buildScope, }; return module; }; } /****************************************************************************** Copyright (c) Microsoft Corporation. Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ***************************************************************************** */ /* global Reflect, Promise, SuppressedError, Symbol, Iterator */ function __rest(s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; } typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) { var e = new Error(message); return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e; }; function generateSubscribeBuilder(subscriber) { const subscribeOption = typeof subscriber === 'object' ? subscriber : { listener: subscriber }; const { selector = (i) => i } = subscribeOption, options = __rest(subscribeOption, ["selector"]); return (initialState) => { var _a; const equalityFn = (options === null || options === void 0 ? void 0 : options.equalityFn) || Object.is; let currentSlice = selector(initialState); let _cleanup; const addCleanup = (cleanup) => { _cleanup = cleanup; }; const listener = (state) => { const nextSlice = selector(state); if (!equalityFn(currentSlice, nextSlice)) { const previousSlice = currentSlice; _cleanup === null || _cleanup === void 0 ? void 0 : _cleanup(); options.listener((currentSlice = nextSlice), previousSlice, { addCleanup }); } }; if ((_a = options.fireImmediately) !== null && _a !== void 0 ? _a : true) { options.listener(currentSlice, currentSlice, { addCleanup }); } return listener; }; } function extendSubscribe(subscriber, rawModule) { rawModule.subscriptionBuilders = [...rawModule.subscriptionBuilders, generateSubscribeBuilder(subscriber)]; return rawModule; } function factory(state, rawModule) { return { actions: (actions) => factory(state, extendActions(actions, rawModule)), computed: (computed) => factory(state, extendComputed(computed, rawModule)), methods: (methods) => factory(state, extendMethods(methods, rawModule)), middleware: (middleware) => factory(state, extendMiddleware(middleware, rawModule)), subscribe: (subscriber) => factory(state, extendSubscribe(subscriber, rawModule)), build: buildModule(state, rawModule), }; } function defineModule(defaultState) { return factory(defaultState, { reducers: {}, computed: {}, methodsBuilders: [], middlewares: [], subscriptionBuilders: [], }); } function useModule(module, selector, equalityFn) { return module.use(selector, equalityFn); } function useModuleActions(module) { return module.useActions(); } function useModuleComputed(module) { return module.useComputed(); } const VERSION = '0.5.8'; exports.VERSION = VERSION; exports.defineModule = defineModule; exports.defineProvider = defineProvider; exports.useModule = useModule; exports.useModuleActions = useModuleActions; exports.useModuleComputed = useModuleComputed; exports.useScopeContext = useScopeContext;