@platform/cell.typesystem
Version:
The 'strongly typed sheets' system of the CellOS.
119 lines (118 loc) • 5.34 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.TypeCacheCells = void 0;
var tslib_1 = require("tslib");
var common_1 = require("../common");
var TypeCacheCells = (function () {
function TypeCacheCells(ns) {
this.total = { rows: -1 };
this.cells = {};
this.rowQueries = [];
this.ns = ns.toString();
}
TypeCacheCells.prototype.query = function (query) {
var _this = this;
query = (query || '').trim();
var self = this;
var range = common_1.CellRange.fromKey(query).square;
var api = {
toString: function () { return query; },
get exists() {
return self.rowQueries.some(function (rows) { return common_1.CellRange.fromKey(rows).contains(query); });
},
get: function (fetch, options) {
if (options === void 0) { options = {}; }
return (0, tslib_1.__awaiter)(_this, void 0, void 0, function () {
var ns, rowQuery, res;
return (0, tslib_1.__generator)(this, function (_a) {
switch (_a.label) {
case 0:
ns = this.ns;
if (!(!options.force && api.exists)) return [3, 2];
return [4, this.refreshTotal({ fetch: fetch, ns: ns, skip: this.total.rows > -1 })];
case 1:
_a.sent();
return [2, this.toResult({ range: range })];
case 2:
rowQuery = this.toFullRow(range);
return [4, fetch.getCells({ ns: ns, query: rowQuery })];
case 3:
res = _a.sent();
this.cells = res.cells ? (0, tslib_1.__assign)((0, tslib_1.__assign)({}, this.cells), res.cells) : this.cells;
this.total = (0, tslib_1.__assign)((0, tslib_1.__assign)({}, this.total), res.total);
this.error = res.error ? res.error : this.error;
this.rowQueries = this.rowQueries.includes(rowQuery)
? this.rowQueries
: (0, tslib_1.__spreadArray)((0, tslib_1.__spreadArray)([], this.rowQueries, true), [rowQuery], false);
return [2, this.toResult({ range: range })];
}
});
});
},
};
return api;
};
TypeCacheCells.prototype.sync = function (changes) {
var cells = changes.cells || {};
var keys = Object.keys(cells);
if (keys.length > 0) {
var ns_1 = common_1.Uri.strip.ns(this.ns);
var diffs = keys
.map(function (key) { return cells[key]; })
.filter(function (diff) { return diff.kind === 'CELL' && common_1.Uri.strip.ns(diff.ns) === ns_1; })
.reduce(function (acc, diff) {
acc[diff.key] = diff.to;
return acc;
}, {});
this.cells = (0, tslib_1.__assign)((0, tslib_1.__assign)({}, this.cells), diffs);
this.total.rows = -1;
}
return this;
};
TypeCacheCells.prototype.toRange = function (input) {
return typeof input === 'object' ? input : common_1.CellRange.fromKey(input);
};
TypeCacheCells.prototype.toFullRow = function (input) {
var range = this.toRange(input);
return range.left.row + 1 + ":" + (range.right.row + 1);
};
TypeCacheCells.prototype.filterCells = function (args) {
var cells = args.cells;
var range = this.toRange(args.range);
return Object.keys(cells).reduce(function (acc, next) {
if (range.contains(next)) {
acc[next] = cells[next];
}
return acc;
}, {});
};
TypeCacheCells.prototype.toResult = function (args) {
var range = args.range;
return {
cells: this.filterCells({ range: range, cells: this.cells }),
total: this.total,
error: this.error,
};
};
TypeCacheCells.prototype.refreshTotal = function (args) {
return (0, tslib_1.__awaiter)(this, void 0, void 0, function () {
var fetch, ns, skip, res;
return (0, tslib_1.__generator)(this, function (_a) {
switch (_a.label) {
case 0:
fetch = args.fetch, ns = args.ns, skip = args.skip;
if (!!skip) return [3, 2];
return [4, fetch.getCells({ ns: ns, query: 'A1:A1' })];
case 1:
res = _a.sent();
this.total = (0, tslib_1.__assign)((0, tslib_1.__assign)({}, this.total), res.total);
_a.label = 2;
case 2: return [2];
}
});
});
};
TypeCacheCells.create = function (ns) { return new TypeCacheCells(ns); };
return TypeCacheCells;
}());
exports.TypeCacheCells = TypeCacheCells;