UNPKG

@platform/cell.typesystem

Version:

The 'strongly typed sheets' system of the CellOS.

285 lines (284 loc) 12.1 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var tslib_1 = require("tslib"); var operators_1 = require("rxjs/operators"); var TypeSystem_core_1 = require("../../TypeSystem.core"); var common_1 = require("./common"); var TypedSheetState = (function () { function TypedSheetState(args) { var _this = this; this._changes = {}; this._dispose$ = new common_1.t.Subject(); this.dispose$ = this._dispose$.pipe(operators_1.share()); this.clear = { cache: function () { var ns = _this.uri.id; var fetch = _this.fetch; var cache = fetch.cache; var prefix = { ns: fetch.cacheKey('getNs', ns), cells: fetch.cacheKey('getCells', ns), }; cache.keys .filter(function (key) { return key.startsWith(prefix.cells) || key.startsWith(prefix.ns); }) .forEach(function (key) { return cache.delete(key); }); }, changes: function (action) { var sheet = _this._sheet; var from = tslib_1.__assign({}, _this._changes); var to = {}; _this._changes = {}; _this.fire({ type: 'SHEET/changes/cleared', payload: { sheet: sheet, from: from, to: to, action: action }, }); }, }; this.change = { ns: function (to) { var ns = _this.uri.toString(); _this.fire({ type: 'SHEET/change', payload: { kind: 'NS', ns: ns, to: to }, }); }, cell: function (key, to) { var ns = _this.uri.id; _this.fire({ type: 'SHEET/change', payload: { kind: 'CELL', ns: ns, key: key, to: to }, }); }, }; this._sheet = args.sheet; this._event$ = args.event$; var fetch = TypeSystem_core_1.TypeCache.fetch(args.fetch, { cache: args.cache }); var getCells = function (args) { return tslib_1.__awaiter(_this, void 0, void 0, function () { var res, cellChanges, keys, cells_1; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: return [4, fetch.getCells(args)]; case 1: res = _a.sent(); cellChanges = this._changes.cells || {}; keys = Object.keys(cellChanges); if (keys.length > 0) { cells_1 = (res.cells = tslib_1.__assign({}, (res.cells || {}))); keys .filter(function (key) { return common_1.Schema.coord.cell.isCell(key) && cells_1[key]; }) .forEach(function (key) { return (cells_1[key] = tslib_1.__assign({}, cellChanges[key].to)); }); } return [2, res]; } }); }); }; var getNs = function (args) { return tslib_1.__awaiter(_this, void 0, void 0, function () { var ns; var _a; return tslib_1.__generator(this, function (_b) { ns = (_a = this._changes.ns) === null || _a === void 0 ? void 0 : _a.to; return [2, ns ? { ns: ns } : fetch.getNs(args)]; }); }); }; this.fetch = tslib_1.__assign(tslib_1.__assign({}, fetch), { getCells: getCells, getNs: getNs }); this.event$ = this._event$.pipe(operators_1.takeUntil(this._dispose$), operators_1.share()); this.change$ = this.event$.pipe(operators_1.takeUntil(this.dispose$), operators_1.filter(function (e) { return e.type === 'SHEET/change'; }), operators_1.map(function (e) { return e.payload; }), operators_1.filter(function (e) { return _this.isWithinNamespace(e.ns); }), operators_1.share()); this.changed$ = this.event$.pipe(operators_1.takeUntil(this.dispose$), operators_1.filter(function (e) { return e.type === 'SHEET/changed'; }), operators_1.map(function (e) { return e.payload; }), operators_1.filter(function (e) { return _this.isWithinNamespace(e.sheet.uri.toString()); }), operators_1.share()); this.change$ .pipe(operators_1.filter(function (e) { return e.kind === 'CELL'; }), operators_1.map(function (e) { return e; })) .subscribe(function (_a) { var key = _a.key, to = _a.to; return _this.fireCellChanged({ key: key, to: to }); }); this.change$ .pipe(operators_1.filter(function (e) { return e.kind === 'NS'; })) .subscribe(function (_a) { var to = _a.to; return _this.fireNsChanged({ to: to }); }); } TypedSheetState.create = function (args) { return new TypedSheetState(args); }; TypedSheetState.prototype.dispose = function () { this._dispose$.next(); this._dispose$.complete(); }; Object.defineProperty(TypedSheetState.prototype, "uri", { get: function () { return this._sheet.uri; }, enumerable: true, configurable: true }); Object.defineProperty(TypedSheetState.prototype, "isDisposed", { get: function () { return this._dispose$.isStopped; }, enumerable: true, configurable: true }); Object.defineProperty(TypedSheetState.prototype, "changes", { get: function () { var changes = this._changes; return common_1.deleteUndefined({ ns: changes.ns ? tslib_1.__assign({}, changes.ns) : undefined, cells: changes.cells ? tslib_1.__assign({}, changes.cells) : undefined, }); }, enumerable: true, configurable: true }); Object.defineProperty(TypedSheetState.prototype, "hasChanges", { get: function () { var changes = this._changes; if (changes.ns) { return true; } if (changes.cells && Object.keys(changes.cells).length > 0) { return true; } return false; }, enumerable: true, configurable: true }); TypedSheetState.prototype.getNs = function () { return tslib_1.__awaiter(this, void 0, void 0, function () { var ns; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: ns = this.uri.id; return [4, this.fetch.getNs({ ns: ns })]; case 1: return [2, (_a.sent()).ns]; } }); }); }; TypedSheetState.prototype.getCell = function (key) { return tslib_1.__awaiter(this, void 0, void 0, function () { var cellChanges, ns, query, res; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: if (!common_1.Schema.coord.cell.isCell(key)) { throw new Error("Expected a cell key (eg \"A1\")."); } cellChanges = this._changes.cells || {}; if (cellChanges[key]) { return [2, cellChanges[key].to]; } ns = this.uri.id; query = key + ":" + key; return [4, this.fetch.getCells({ ns: ns, query: query })]; case 1: res = _a.sent(); return [2, (res.cells || {})[key]]; } }); }); }; TypedSheetState.prototype.fire = function (e) { this._event$.next(e); }; TypedSheetState.prototype.fireNsChanged = function (args) { return tslib_1.__awaiter(this, void 0, void 0, function () { var to, existing, from, _a, change; return tslib_1.__generator(this, function (_b) { switch (_b.label) { case 0: to = args.to; existing = this._changes.ns; if (existing && common_1.R.equals(existing.to, to)) { return [2]; } if (!existing) return [3, 1]; _a = existing.from; return [3, 3]; case 1: return [4, this.getNs()]; case 2: _a = _b.sent(); _b.label = 3; case 3: from = (_a) || {}; change = { kind: 'NS', ns: this.uri.id, from: from, to: to, }; this._changes = tslib_1.__assign(tslib_1.__assign({}, this._changes), { ns: change }); this.fireChanged({ change: change }); return [2]; } }); }); }; TypedSheetState.prototype.fireCellChanged = function (args) { return tslib_1.__awaiter(this, void 0, void 0, function () { var to, key, existing, ns, from, _a, change, cells; var _b; return tslib_1.__generator(this, function (_c) { switch (_c.label) { case 0: to = args.to, key = args.key; existing = (this._changes.cells || {})[key]; if (existing && common_1.R.equals(existing.to, to)) { return [2]; } ns = this.uri.id; if (!existing) return [3, 1]; _a = existing.from; return [3, 3]; case 1: return [4, this.getCell(key)]; case 2: _a = _c.sent(); _c.label = 3; case 3: from = (_a) || {}; delete from.hash; delete to.hash; change = { kind: 'CELL', ns: ns, key: key, from: from, to: to, }; cells = tslib_1.__assign(tslib_1.__assign({}, (this._changes.cells || {})), (_b = {}, _b[key] = change, _b)); this._changes = tslib_1.__assign(tslib_1.__assign({}, this._changes), { cells: cells }); this.fireChanged({ change: change }); return [2]; } }); }); }; TypedSheetState.prototype.fireChanged = function (args) { var change = args.change; this.fire({ type: 'SHEET/changed', payload: { sheet: this._sheet, change: change, changes: this.changes, }, }); }; TypedSheetState.prototype.isWithinNamespace = function (input) { var text = (input || '').trim(); var ns = this.uri; if (!text.includes(':')) { return common_1.Uri.strip.ns(text) === ns.id; } if (text.startsWith('ns:') || !text.includes(':')) { return text === ns.toString(); } if (text.startsWith('cell:')) { return text.startsWith("cell:" + ns.id + ":"); } return false; }; return TypedSheetState; }()); exports.TypedSheetState = TypedSheetState;