UNPKG

typedux

Version:

Slightly adjusted Redux (awesome by default) for TS

47 lines 1.69 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.selectorChain = void 0; const guard_1 = require("@3fv/guard"); function continueSelectorChain(store, state, data, keyPath = []) { keyPath = keyPath || []; // noinspection DuplicatedCode const nextSelector = (() => { // TRACK FIRST PROP ACCESS const firstGet = keyPath.map(() => true); // CHECK IF KEY SHOULD BE NUMBER function resolveKey(value, key, index) { if (firstGet[index]) { if (Array.isArray(value)) { const keyNum = guard_1.toNumber(key); if (guard_1.isNumber(keyNum)) { key = keyPath[index] = keyNum; } } firstGet[index] = false; } return key; } function getterFn(state) { return keyPath.reduce((value, key, index) => { return value[resolveKey(value, key, index)]; }, state); } const getter = Object.assign(getterFn, { subscribe(listener) { return store.observe(getterFn, listener); } }); return getter; // overrideCallback(getter, keyPath) }); return (new Proxy(nextSelector, { get: (target, key) => { return continueSelectorChain(store, state, undefined, [...keyPath, key]); } })); } //PropChainType<S, S,SelectorChainDataAccessor<S,S>> function selectorChain(store, state) { return continueSelectorChain(store, state, state); } exports.selectorChain = selectorChain; //# sourceMappingURL=SelectorChain.js.map