UNPKG

@connectv/core

Version:

agent-based reactive programming library for typescript/javascript

127 lines 3.96 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 __()); }; })(); Object.defineProperty(exports, "__esModule", { value: true }); var rxjs_1 = require("rxjs"); var tracker_1 = require("../shared/tracker"); var pin_1 = require("./pin"); /** * * Represents a map of labelled pins. The labelled pins are created * first time they are requested, allowing for possibly huge * maps without high memory cost. * */ var PinMap = /** @class */ (function (_super) { __extends(PinMap, _super); /** * * @param factory will be used to create each new pin. * */ function PinMap(factory) { if (factory === void 0) { factory = function () { return new pin_1.Pin(); }; } var _this = _super.call(this) || this; _this.factory = factory; _this._pins = {}; return _this; } /** * * Fetches the pin with the given label, and create it if not * created already. * * @param label * */ PinMap.prototype.get = function (label) { if (!(label in this._pins)) { var _pin = this.factory(label); this._pins[label] = _pin; if (this._subject) this._subject.next([label, _pin]); return _pin; } return this._pins[label]; }; /** * * Checks if a pin with given label is created, without * creating the pin. * * @param label * */ PinMap.prototype.instantiated = function (label) { return label in this._pins; }; Object.defineProperty(PinMap.prototype, "pins", { /** * * @returns an array of all created pins. * */ get: function () { return Object.values(this._pins); }, enumerable: true, configurable: true }); Object.defineProperty(PinMap.prototype, "entries", { /** * * @returns an entry list (pairs of `[string, Pin]`) of created pins. * */ get: function () { return Object.entries(this._pins); }, enumerable: true, configurable: true }); /** * * Subscribes to the event of creation of a new pin. The subscriber function * will also be invoked on all of the already created pairs. * * @param subscriber * @returns a [`Subscription`](https://rxjs-dev.firebaseapp.com/guide/subscription) object * that you can later unsubscribe from using `.unsubscribe()` * */ PinMap.prototype.subscribe = function (subscriber) { if (!this._subject) this._subject = new rxjs_1.Subject(); this.entries.forEach(function (entry) { return subscriber.apply(void 0, entry); }); return this.track(this._subject.subscribe(function (entry) { return subscriber.apply(void 0, entry); })); }; /** * * Clears all the created pins and remove references to them, * also will remove all subscriptions. * */ PinMap.prototype.clear = function () { this.pins.forEach(function (pin) { return pin.clear(); }); this._pins = {}; if (this._subject) { this._subject.complete(); this._subject = undefined; } return _super.prototype.clear.call(this); }; return PinMap; }(tracker_1.Tracker)); exports.PinMap = PinMap; //# sourceMappingURL=pin-map.js.map