UNPKG

@connectv/core

Version:

agent-based reactive programming library for typescript/javascript

98 lines 3.91 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; result["default"] = mod; return result; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); var rxjs_1 = require("rxjs"); var operators_1 = require("rxjs/operators"); var emission_1 = __importStar(require("../shared/emission")); var group_1 = __importDefault(require("./group")); var pin_1 = require("./pin"); var pin_map_1 = require("./pin-map"); /** * * Represents [pack](https://connective.dev/docs/pack) pins. * */ var Pack = /** @class */ (function (_super) { __extends(Pack, _super); function Pack(pinmap) { var _this = _super.call(this) || this; _this.pinmap = pinmap; if (pinmap) _this.track(pinmap.subscribe(function (_, pin) { return pin.to(_this); })); return _this; } /** * * Resolves the underlying observable by * [combining the latest values](https://rxjs-dev.firebaseapp.com/api/index/function/combineLatest) * from corresponding observables of inbound pins. * * If a `PinMap` is passed to the constructor, it will instead resolve * by combining the latest values from instantiated pins of the passed `PinMap`. * * @param inbound * */ Pack.prototype.resolve = function (inbound) { if (this.pinmap) { var _entries_1 = this.pinmap.entries; if (_entries_1.length == 0) return rxjs_1.of(emission_1.default()); return rxjs_1.combineLatest.apply(void 0, _entries_1.map(function (entry) { return entry[1].observable; })).pipe(operators_1.map(function (emissions) { return emission_1.Emission.from(emissions, _entries_1.reduce(function (_map, entry, index) { _map[entry[0]] = emissions[index].value; return _map; }, {})); })); } else return rxjs_1.combineLatest.apply(void 0, inbound .map(function (pin) { return pin.observable; })).pipe(operators_1.map(function (emissions) { return emission_1.Emission.from(emissions); })); }; return Pack; }(pin_1.Pin)); exports.Pack = Pack; /** * * Creates a [pack](https://connective.dev/docs/pack) pin. * * @param stuff If passed, the pin will be connected to all given pins. * If any of the stuff is a `PinMap` instead of a `Pin`, then upon resolution * the pack will be connected to all of its realized pins. * */ function pack() { var stuff = []; for (var _i = 0; _i < arguments.length; _i++) { stuff[_i] = arguments[_i]; } var _mapped = stuff.map(function (each) { return (each instanceof pin_map_1.PinMap) ? new Pack(each) : each; }); if (_mapped.length == 0) return new Pack(); if (_mapped.length == 1) return (_mapped[0] instanceof Pack) ? _mapped[0] : _mapped[0].to(new Pack()); return group_1.default.apply(void 0, _mapped).to(new Pack()); } exports.pack = pack; exports.default = pack; //# sourceMappingURL=pack.js.map