UNPKG

@rxx/worker

Version:

React MVI micro framework.

111 lines (110 loc) 4.04 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var rxjs_1 = require("rxjs"); var operators_1 = require("rxjs/operators"); function isObject(o) { return (Object.prototype.toString.call(o) === '[object Object]' && o.constructor === Object); } var isArray = Array.isArray; var Collector = (function () { function Collector() { this.code = 'const template = '; this.observables = []; this.values = []; this.id = 0; } Collector.prototype.collect = function (object, startWithNull) { var _this = this; if (startWithNull === void 0) { startWithNull = false; } if (object instanceof rxjs_1.Observable) { return object; } if (!isArray(Object) && !isObject(object)) { throw new Error('Invalid object passed to combineTemplate'); } this.doCollect(object); this.initInjector(); if (!this.observables.length) { return rxjs_1.of(object); } return rxjs_1.combineLatest.apply(void 0, this.observables.map(function (_a) { var observable = _a.observable; var NULL_VALUE = {}; var firstValue = NULL_VALUE; var sub = observable.subscribe(function (v) { return (firstValue = v); }); sub.unsubscribe(); if (firstValue === NULL_VALUE && startWithNull) { return observable.pipe(operators_1.startWith(null)); } return observable; })).pipe(operators_1.map(function (results) { var values = {}; results.forEach(function (value, i) { values[_this.observables[i].key] = value; }); _this.values.forEach(function (_a) { var key = _a.key, value = _a.value; return (values[key] = value); }); return _this.injector(values); })); }; Collector.prototype.doCollect = function (object) { var _this = this; if (isObject(object)) { this.code += '{'; for (var key in object) { var k = JSON.stringify(key); if (object[key] instanceof rxjs_1.Observable) { this.code += k + ": values[" + ++this.id + "]"; this.observables.push({ observable: object[key], key: String(this.id), }); } else { this.code += k + ": "; this.doCollect(object[key]); } this.code += ','; } this.sliceComma(); this.code += '}'; } else if (isArray(object)) { this.code += '['; object.forEach(function (o, i) { if (o instanceof rxjs_1.Observable) { _this.code += "values[" + ++_this.id + "]"; _this.observables.push({ observable: o, key: String(_this.id) }); } else { _this.doCollect(o); } _this.code += ','; }); this.sliceComma(); this.code += ']'; } else { this.code += "values[" + ++this.id + "]"; this.values.push({ value: object, key: String(this.id) }); } }; Collector.prototype.sliceComma = function () { if (this.code.charAt(this.code.length - 1) === ',') { this.code = this.code.slice(0, this.code.length - 1); } }; Collector.prototype.initInjector = function () { this.code += '\n;return template;'; this.injector = Function('values', this.code); }; return Collector; }()); function combineTemplate(object, startWithNull) { if (startWithNull === void 0) { startWithNull = true; } return new Collector().collect(object, startWithNull); } exports.combineTemplate = combineTemplate;