UNPKG

@platform/cell.typesystem

Version:

The 'strongly typed sheets' system of the CellOS.

119 lines (118 loc) 4.07 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SheetPool = void 0; var rxjs_1 = require("rxjs"); var operators_1 = require("rxjs/operators"); var SheetPool = (function () { function SheetPool() { this._dispose$ = new rxjs_1.Subject(); this._items = {}; this.dispose$ = this._dispose$.pipe((0, operators_1.share)()); } SheetPool.prototype.dispose = function () { if (!this.isDisposed) { this._dispose$.next(); this._dispose$.complete(); this._items = {}; } }; Object.defineProperty(SheetPool.prototype, "isDisposed", { get: function () { return this._dispose$.isStopped; }, enumerable: false, configurable: true }); Object.defineProperty(SheetPool.prototype, "count", { get: function () { return Object.keys(this._items).length; }, enumerable: false, configurable: true }); Object.defineProperty(SheetPool.prototype, "sheets", { get: function () { var items = this._items; return Object.keys(items).reduce(function (acc, key) { acc[key] = items[key].sheet; return acc; }, {}); }, enumerable: false, configurable: true }); SheetPool.prototype.exists = function (sheet) { this.throwIfDisposed('exists'); return Boolean(this.sheet(sheet)); }; SheetPool.prototype.sheet = function (sheet) { this.throwIfDisposed('sheet'); var ns = this.ns(sheet); var item = this._items[ns]; return item ? item.sheet : undefined; }; SheetPool.prototype.add = function (sheet, options) { var _this = this; if (options === void 0) { options = {}; } this.throwIfDisposed('add'); if (this.exists(sheet)) { return this; } var ns = this.ns(sheet); this._items[ns] = { sheet: sheet, children: [] }; sheet.dispose$.pipe((0, operators_1.take)(1)).subscribe(function () { return _this.remove(sheet); }); if (options.parent) { var parent_1 = this._items[this.ns(options.parent)]; if (parent_1 && !parent_1.children.includes(ns)) { parent_1.children.push(ns); } } return this; }; SheetPool.prototype.remove = function (sheet) { var _this = this; this.throwIfDisposed('remove'); var ns = this.ns(sheet); var item = this._items[ns]; delete this._items[ns]; if (item) { item.children.forEach(function (child) { return _this.remove(child); }); } return this; }; SheetPool.prototype.children = function (sheet) { var _this = this; this.throwIfDisposed('children'); var build = function (sheet, sheets) { if (sheets === void 0) { sheets = []; } var ns = _this.ns(sheet); if (!sheets.some(function (sheet) { return sheet.toString() === ns; })) { var item = _this._items[ns]; if (item) { item.children.forEach(function (ns) { var child = _this._items[ns]; if (child) { sheets.push(child.sheet); } build(ns); }); } } return sheets; }; return build(sheet); }; SheetPool.prototype.throwIfDisposed = function (action) { if (this.isDisposed) { throw new Error("Cannot '" + action + "' because [SheetPool] is disposed."); } }; SheetPool.prototype.ns = function (sheet) { var ns = sheet.toString(); ns = ns.includes(':') ? ns : "ns:" + ns; return ns; }; SheetPool.create = function () { return new SheetPool(); }; return SheetPool; }()); exports.SheetPool = SheetPool;