UNPKG

jorum

Version:

Model layer with rx.js for React applications.

114 lines (113 loc) 4.25 kB
"use strict"; var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; Object.defineProperty(exports, "__esModule", { value: true }); var React = require("react"); var bloc_1 = require("../bloc"); var react_1 = require("react"); var inject_1 = require("../inject"); var effect_1 = require("../effect"); var BlocContainer = /** @class */ (function () { function BlocContainer() { this.blocType = null; this._bloc = undefined; this.effectSubscriptions = []; } Object.defineProperty(BlocContainer.prototype, "bloc", { get: function () { return this._bloc; }, set: function (newBloc) { if (newBloc === undefined) return; if (newBloc === this._bloc) return; this.cleanUp(); this._bloc = newBloc; this.initialize(); }, enumerable: true, configurable: true }); BlocContainer.prototype.initialize = function () { if (!this._bloc) return; var effects = Reflect.getMetadata(effect_1.effectsMetadataKey, this.blocType.prototype) || []; for (var _i = 0, effects_1 = effects; _i < effects_1.length; _i++) { var effect = effects_1[_i]; var stream$ = this._bloc[effect]; var subscription = stream$.subscribe(doNothing); this.effectSubscriptions.push(subscription); } }; BlocContainer.prototype.cleanUp = function () { if (typeof this._bloc !== 'object') return; for (var _i = 0, _a = this.effectSubscriptions; _i < _a.length; _i++) { var subscription = _a[_i]; subscription.unsubscribe(); } this.effectSubscriptions = []; if (this._bloc.blocWillDestroy) { this._bloc.blocWillDestroy(); } }; BlocContainer.prototype.hasBloc = function () { return this._bloc !== undefined; }; return BlocContainer; }()); exports.Provider = function Provider(props) { var _a, _b; function useInjections(Bloc, args) { var injects = Reflect.getMetadata(inject_1.injectMetadataKey, Bloc) || []; var paramTypes = Reflect.getMetadata('design:paramtypes', Bloc) || []; for (var _i = 0, injects_1 = injects; _i < injects_1.length; _i++) { var inject = injects_1[_i]; var Context_1 = Reflect.getMetadata(bloc_1.contextSymbol, paramTypes[inject]); var injection = react_1.useContext(Context_1); if (args) { args[inject] = injection; } } } var containerRef = react_1.useRef(new BlocContainer()); useInjections(props.of, props.args); if (containerRef.current.blocType !== props.of) { containerRef.current.blocType = props.of; containerRef.current.bloc = new ((_a = props.of).bind.apply(_a, [void 0].concat(props.args)))(); } if (!containerRef.current.hasBloc()) { containerRef.current.bloc = new ((_b = props.of).bind.apply(_b, [void 0].concat(props.args)))(); } react_1.useEffect(function () { return function () { containerRef.current.bloc = null; }; }, []); var Context = Reflect.getMetadata(bloc_1.contextSymbol, props.of); return (React.createElement(Context.Provider, { value: containerRef.current.bloc }, props.children)); }; exports.Provider.defaultProps = { args: [] }; function withProvider(providerProps) { return function (C) { return function WithProvider(props) { var finalProviderProps = typeof providerProps === 'function' ? providerProps(props) : providerProps; return (React.createElement(exports.Provider, __assign({}, finalProviderProps), React.createElement(C, __assign({}, props)))); }; }; } exports.withProvider = withProvider; function doNothing() { }