UNPKG

@sigi/core

Version:
304 lines 13.4 kB
import { __read, __spreadArray, __values } from "tslib"; import { produce } from 'immer'; import { merge } from 'rxjs'; import { map, filter, ignoreElements } from 'rxjs/operators'; import { hmrEnabled, hmrInstanceCache } from './hmr'; import { getDecoratedActions, getActionsToSkip } from './metadata'; import { Store } from './store'; import { NOOP_ACTION_TYPE_SYMBOL, GLOBAL_KEY_SYMBOL, TERMINATE_ACTION_TYPE_SYMBOL, RESET_ACTION_TYPE_SYMBOL, RETRY_ACTION_TYPE_SYMBOL, RETRY_KEY_SYMBOL, } from './symbols'; var _globalThis = typeof globalThis === 'undefined' ? typeof window === 'undefined' ? global : window : globalThis; var DEFAULT_STATE_KEY = 'defaultState'; var EffectModule = (function () { function EffectModule() { var e_1, _a, e_2, _b; var _this = this; this.actionStreams = {}; this.retryActionsCreator = {}; this.actionNames = []; this.restoredFromSSR = false; this.createNoopAction = function () { return _this.noop(); }; this.terminate = function () { return { type: TERMINATE_ACTION_TYPE_SYMBOL, payload: null, store: _this.store }; }; this.reset = function () { return { type: RESET_ACTION_TYPE_SYMBOL, payload: null, store: _this.store }; }; this.moduleName = Object.getPrototypeOf(this).moduleName; var reducer = this.combineReducers(); var definedActions = this.combineDefineActions(); var epic = this.combineEffects(); this.store = new Store(this.moduleName, reducer, epic); var _loop_1 = function (name_1) { ; this_1[name_1] = this_1.store.action$.pipe(filter(function (_a) { var type = _a.type; return type === name_1; }), map(function (_a) { var payload = _a.payload; return payload; })); }; var this_1 = this; try { for (var definedActions_1 = __values(definedActions), definedActions_1_1 = definedActions_1.next(); !definedActions_1_1.done; definedActions_1_1 = definedActions_1.next()) { var name_1 = definedActions_1_1.value; _loop_1(name_1); } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (definedActions_1_1 && !definedActions_1_1.done && (_a = definedActions_1.return)) _a.call(definedActions_1); } finally { if (e_1) throw e_1.error; } } this.actions = { reset: this.reset, terminate: this.terminate, noop: this.noop, }; this.dispatchers = { reset: function () { _this.store.dispatch(_this.reset()); }, terminate: function () { _this.store.dispatch(_this.terminate()); }, noop: function () { _this.store.dispatch(_this.noop()); }, }; var _loop_2 = function (name_2) { var actionCreator = function (payload) { return ({ type: name_2, payload: payload, store: _this.store }); }; this_2.actions[name_2] = actionCreator; this_2.dispatchers[name_2] = function (payload) { _this.store.dispatch(actionCreator(payload)); }; this_2.actionStreams[name_2] = this_2.store.action$.pipe(filter(function (_a) { var type = _a.type; return type === name_2; }), map(function (_a) { var payload = _a.payload; return payload; })); }; var this_2 = this; try { for (var _c = __values(this.actionNames), _d = _c.next(); !_d.done; _d = _c.next()) { var name_2 = _d.value; _loop_2(name_2); } } catch (e_2_1) { e_2 = { error: e_2_1 }; } finally { try { if (_d && !_d.done && (_b = _c.return)) _b.call(_c); } finally { if (e_2) throw e_2.error; } } if (typeof Proxy !== 'undefined') { var context_1 = this; return new Proxy(this, { defineProperty: function (target, p, attr) { var _a; if (p === DEFAULT_STATE_KEY) { if (attr.set) { var rawSetter_1 = attr.set; attr.set = function (value) { var _a; context_1.internalDefaultState = value; if (!context_1.store.ready) { context_1.store.setup(context_1.getDefaultState()); context_1.actionsToRetry = new Set(((_a = _globalThis[RETRY_KEY_SYMBOL]) === null || _a === void 0 ? void 0 : _a[this.moduleName]) || []); context_1.actionsToSkip = new Set(context_1.restoredFromSSR ? getActionsToSkip(context_1.constructor.prototype) || [] : []); } return rawSetter_1.call(this, value); }; } else if ('value' in attr) { context_1.internalDefaultState = attr.value; if (!context_1.store.ready) { context_1.store.setup(context_1.getDefaultState()); context_1.actionsToRetry = new Set(((_a = _globalThis[RETRY_KEY_SYMBOL]) === null || _a === void 0 ? void 0 : _a[context_1.moduleName]) || []); context_1.actionsToSkip = new Set(context_1.restoredFromSSR ? getActionsToSkip(context_1.constructor.prototype) || [] : []); } } } return Reflect.defineProperty(target, p, attr); }, set: function (target, p, value, receiver) { var _a; if (p === DEFAULT_STATE_KEY) { context_1.internalDefaultState = value; if (!context_1.store.ready) { context_1.store.setup(context_1.getDefaultState()); context_1.actionsToRetry = new Set(((_a = _globalThis[RETRY_KEY_SYMBOL]) === null || _a === void 0 ? void 0 : _a[context_1.moduleName]) || []); context_1.actionsToSkip = new Set(context_1.restoredFromSSR ? getActionsToSkip(context_1.constructor.prototype) || [] : []); } } return Reflect.set(target, p, value, receiver); }, }); } else { Object.defineProperty(this, DEFAULT_STATE_KEY, { set: function (value) { var _a; _this.internalDefaultState = value; if (!_this.store.ready) { _this.store.setup(_this.getDefaultState()); _this.actionsToRetry = new Set(((_a = _globalThis[RETRY_KEY_SYMBOL]) === null || _a === void 0 ? void 0 : _a[_this.moduleName]) || []); _this.actionsToSkip = new Set(_this.restoredFromSSR ? getActionsToSkip(_this.constructor.prototype) || [] : []); } }, get: function () { return _this.getDefaultState(); }, }); } } Object.defineProperty(EffectModule.prototype, "state$", { get: function () { return this.store.state$; }, enumerable: false, configurable: true }); Object.defineProperty(EffectModule.prototype, "action$", { get: function () { return this.store.action$; }, enumerable: false, configurable: true }); Object.defineProperty(EffectModule.prototype, "state", { get: function () { return this.store.state; }, enumerable: false, configurable: true }); EffectModule.prototype.getActions = function () { return this.actions; }; EffectModule.prototype.getAction$ = function () { return this.actionStreams; }; EffectModule.prototype.retryOnClient = function () { return this.retryActionsCreator; }; EffectModule.prototype.noop = function () { return { type: NOOP_ACTION_TYPE_SYMBOL, payload: null, store: this.store }; }; EffectModule.prototype.getDefaultState = function () { var _a, _b; return (_b = (_a = this.tryReadHmrState()) !== null && _a !== void 0 ? _a : this.tryReadSSRState()) !== null && _b !== void 0 ? _b : this.internalDefaultState; }; EffectModule.prototype.tryReadSSRState = function () { var ssrCache = _globalThis[GLOBAL_KEY_SYMBOL]; if (ssrCache === null || ssrCache === void 0 ? void 0 : ssrCache[this.moduleName]) { this.restoredFromSSR = true; return ssrCache[this.moduleName]; } }; EffectModule.prototype.tryReadHmrState = function () { if (hmrEnabled) { var hmrCache = hmrInstanceCache.get(this.moduleName); if (hmrCache) { var cachedState = hmrCache.state; hmrCache.dispose(); return cachedState; } } }; EffectModule.prototype.combineEffects = function () { var _a; var _this = this; var effectKeys = getDecoratedActions(this.constructor.prototype, 'Effect'); if (!effectKeys || effectKeys.length === 0) { return function (action$) { return action$.pipe(ignoreElements()); }; } (_a = this.actionNames).push.apply(_a, __spreadArray([], __read(effectKeys), false)); return function (action$) { return merge.apply(void 0, __spreadArray([], __read(effectKeys.map(function (name) { var effect = _this[name]; var payload$ = action$.pipe(filter(function (_a) { var type = _a.type; return type === name; }), filter(function (_, index) { var _a; var skipCount = !_this.actionsToRetry.has(name) && ((_a = _this.actionsToSkip) === null || _a === void 0 ? void 0 : _a.has(name)) ? 1 : 0; return skipCount <= index; }), map(function (_a) { var payload = _a.payload; return payload; })); _this.retryActionsCreator[name] = function () { return ({ type: RETRY_ACTION_TYPE_SYMBOL, payload: { module: _this, name: name, }, store: _this.store, }); }; return effect.call(_this, payload$); })), false)); }; }; EffectModule.prototype.combineReducers = function () { var _a; var _this = this; var reducerKeys = getDecoratedActions(this.constructor.prototype, 'Reducer', []); var immerReducerKeys = getDecoratedActions(this.constructor.prototype, 'ImmerReducer', []); (_a = this.actionNames).push.apply(_a, __spreadArray(__spreadArray([], __read(reducerKeys), false), __read(immerReducerKeys), false)); var immerReducers = immerReducerKeys.reduce(function (acc, property) { acc[property] = _this[property].bind(_this); return acc; }, {}); var reducers = reducerKeys.reduce(function (acc, property) { acc[property] = _this[property].bind(_this); return acc; }, {}); return function (prevState, action) { var type = action.type; if (type === RESET_ACTION_TYPE_SYMBOL) { return _this.getDefaultState(); } else { if (reducers[type]) { return reducers[type](prevState, action.payload); } else if (immerReducers[type]) { return produce(prevState, function (draft) { return immerReducers[type](draft, action.payload); }); } } return prevState; }; }; EffectModule.prototype.combineDefineActions = function () { var _a; var defineActionKeys = getDecoratedActions(this.constructor.prototype, 'DefineAction', []); (_a = this.actionNames).push.apply(_a, __spreadArray([], __read(defineActionKeys), false)); return defineActionKeys; }; return EffectModule; }()); export { EffectModule }; //# sourceMappingURL=module.js.map