universal-model-angular
Version:
Universal model for Angular
213 lines (163 loc) • 9.55 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _reactiveJs = require("@pksilen/reactive-js");
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance"); }
function _iterableToArrayLimit(arr, i) { if (!(Symbol.iterator in Object(arr) || Object.prototype.toString.call(arr) === "[object Arguments]")) { return; } var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
var Store =
/*#__PURE__*/
function () {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function Store(initialState, selectors) {
var _this = this;
_classCallCheck(this, Store);
_defineProperty(this, "reactiveState", void 0);
_defineProperty(this, "reactiveSelectors", void 0);
_defineProperty(this, "stateStopWatches", new Map());
_defineProperty(this, "selectorStopWatches", new Map());
_defineProperty(this, "componentInstanceToUpdatesMap", new Map());
_defineProperty(this, "updateCount", 0);
this.reactiveState = (0, _reactiveJs.reactive)(initialState);
this.reactiveSelectors = {};
if (selectors) {
Object.keys(selectors).forEach(function (key) {
return _this.reactiveSelectors[key] = (0, _reactiveJs.computed)(function () {
return (// eslint-disable-next-line @typescript-eslint/ban-ts-ignore
// @ts-ignore
selectors[key](_this.reactiveState)
);
});
});
}
} // eslint-disable-next-line @typescript-eslint/no-explicit-any
_createClass(Store, [{
key: "getStateStopWatches",
value: function getStateStopWatches(componentInstace) {
return this.stateStopWatches.get(componentInstace);
} // eslint-disable-next-line @typescript-eslint/no-explicit-any
}, {
key: "getSelectorStopWatches",
value: function getSelectorStopWatches(componentInstace) {
return this.selectorStopWatches.get(componentInstace);
}
}, {
key: "getUpdateCount",
value: function getUpdateCount() {
return this.updateCount;
}
}, {
key: "getState",
value: function getState() {
return this.reactiveState;
}
}, {
key: "getSelectors",
value: function getSelectors() {
return this.reactiveSelectors;
}
}, {
key: "getStateAndSelectors",
value: function getStateAndSelectors() {
return [this.reactiveState, this.reactiveSelectors];
} // eslint-disable-next-line @typescript-eslint/no-explicit-any
}, {
key: "useStateAndSelectors",
value: function useStateAndSelectors(componentInstance, subStateOrStateGetterMap, selectorMap) {
this.useState(componentInstance, subStateOrStateGetterMap);
this.useSelectors(componentInstance, selectorMap);
} // eslint-disable-next-line @typescript-eslint/no-explicit-any
}, {
key: "useState",
value: function useState(componentInstance, subStateOrStateGetterMap) {
var _this2 = this;
this.stateStopWatches.set(componentInstance, []);
Object.entries(subStateOrStateGetterMap).forEach(function (_ref) {
var _this2$stateStopWatch;
var _ref2 = _slicedToArray(_ref, 2),
stateName = _ref2[0],
subStateOrStateGetter = _ref2[1];
if (typeof subStateOrStateGetter !== 'function' && !subStateOrStateGetter.__isSubState__) {
throw new Error('useState: One of given subStates is not subState');
}
componentInstance[stateName] = typeof subStateOrStateGetter === 'function' ? (0, _reactiveJs.computed)(subStateOrStateGetter).value : subStateOrStateGetter;
(_this2$stateStopWatch = _this2.stateStopWatches.get(componentInstance)) === null || _this2$stateStopWatch === void 0 ? void 0 : _this2$stateStopWatch.push(_this2.watch(componentInstance, stateName, typeof subStateOrStateGetter === 'function' ? (0, _reactiveJs.computed)(subStateOrStateGetter) : subStateOrStateGetter));
});
var originalOnDestroy = componentInstance.ngOnDestroy;
componentInstance.ngOnDestroy = function () {
var _this2$stateStopWatch2;
(_this2$stateStopWatch2 = _this2.stateStopWatches.get(componentInstance)) === null || _this2$stateStopWatch2 === void 0 ? void 0 : _this2$stateStopWatch2.forEach(function (stopWatch) {
return stopWatch();
});
_this2.stateStopWatches.delete(componentInstance);
if (originalOnDestroy) {
originalOnDestroy();
}
};
} // eslint-disable-next-line @typescript-eslint/no-explicit-any
}, {
key: "useSelectors",
value: function useSelectors(componentInstance, selectorMap) {
var _this3 = this;
this.selectorStopWatches.set(componentInstance, []);
Object.entries(selectorMap).forEach(function (_ref3) {
var _this3$selectorStopWa;
var _ref4 = _slicedToArray(_ref3, 2),
selectorName = _ref4[0],
selector = _ref4[1];
componentInstance[selectorName] = selector.value;
(_this3$selectorStopWa = _this3.selectorStopWatches.get(componentInstance)) === null || _this3$selectorStopWa === void 0 ? void 0 : _this3$selectorStopWa.push(_this3.watch(componentInstance, selectorName, selector));
});
var originalOnDestroy = componentInstance.ngOnDestroy;
componentInstance.ngOnDestroy = function () {
var _this3$selectorStopWa2;
(_this3$selectorStopWa2 = _this3.selectorStopWatches.get(componentInstance)) === null || _this3$selectorStopWa2 === void 0 ? void 0 : _this3$selectorStopWa2.forEach(function (stopWatch) {
return stopWatch();
});
_this3.selectorStopWatches.delete(componentInstance);
if (originalOnDestroy) {
originalOnDestroy();
}
};
} // eslint-disable-next-line @typescript-eslint/no-explicit-any
}, {
key: "watch",
value: function watch(componentInstance, name, subStateOrStateGetterOrSelector) {
var _this4 = this;
return (0, _reactiveJs.watch)(function () {
return subStateOrStateGetterOrSelector;
}, function () {
if (!_this4.componentInstanceToUpdatesMap.get(componentInstance)) {
setTimeout(function () {
_this4.updateCount++;
Object.entries(_this4.componentInstanceToUpdatesMap.get(componentInstance)).forEach( // eslint-disable-next-line @typescript-eslint/no-explicit-any
function (_ref5) {
var _ref6 = _slicedToArray(_ref5, 2),
key = _ref6[0],
value = _ref6[1];
componentInstance[key] = value;
});
_this4.componentInstanceToUpdatesMap.delete(componentInstance);
}, 0);
}
_this4.componentInstanceToUpdatesMap.set(componentInstance, _objectSpread({}, _this4.componentInstanceToUpdatesMap.get(componentInstance), _defineProperty({}, name, 'effect' in subStateOrStateGetterOrSelector ? subStateOrStateGetterOrSelector.value : subStateOrStateGetterOrSelector)));
}, {
deep: true,
flush: 'sync'
});
}
}]);
return Store;
}();
exports.default = Store;
//# sourceMappingURL=Store.js.map