@platform/cell.typesystem
Version:
The 'strongly typed sheets' system of the CellOS.
222 lines (221 loc) • 8.87 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var common_1 = require("./common");
var TypedSheetRow_1 = require("./TypedSheetRow");
var TypedSheetData = (function () {
function TypedSheetData(args) {
this._rows = [];
this._status = 'INIT';
this._total = -1;
this._loading = [];
this._isLoaded = false;
this._sheet = args.sheet;
this.typename = args.typename;
this.types = args.types;
this._ctx = args.ctx;
this._range = TypedSheetData.formatRange(args.range);
}
TypedSheetData.formatRange = function (input) {
var text = (input || '').trim();
var DEFAULT = TypedSheetData.DEFAULT;
if (!text) {
return DEFAULT.RANGE;
}
var range = common_1.coord.range.fromKey(text);
if (!range.isValid) {
return DEFAULT.RANGE;
}
var left = {
key: range.left.key,
index: range.left.row,
isInfinity: isInfinity(range.left.key),
};
var right = {
key: range.right.key,
index: range.right.row,
isInfinity: isInfinity(range.right.key),
};
if (left.isInfinity && right.isInfinity) {
return DEFAULT.RANGE;
}
if (left.isInfinity) {
left.index = DEFAULT.PAGE - 1;
}
if (right.isInfinity) {
right.index = DEFAULT.PAGE - 1;
}
var edges = [Math.max(0, left.index) + 1, Math.max(0, right.index) + 1].sort(diff);
return edges[0] + ":" + edges[1];
};
Object.defineProperty(TypedSheetData.prototype, "uri", {
get: function () {
return this._sheet.uri;
},
enumerable: true,
configurable: true
});
Object.defineProperty(TypedSheetData.prototype, "rows", {
get: function () {
return this._rows;
},
enumerable: true,
configurable: true
});
Object.defineProperty(TypedSheetData.prototype, "range", {
get: function () {
return this._range;
},
enumerable: true,
configurable: true
});
Object.defineProperty(TypedSheetData.prototype, "status", {
get: function () {
return this._status;
},
enumerable: true,
configurable: true
});
Object.defineProperty(TypedSheetData.prototype, "isLoaded", {
get: function () {
return this._isLoaded;
},
enumerable: true,
configurable: true
});
Object.defineProperty(TypedSheetData.prototype, "total", {
get: function () {
return this._total;
},
enumerable: true,
configurable: true
});
TypedSheetData.prototype.toString = function () {
return this.uri.toString();
};
TypedSheetData.prototype.exists = function (index) {
return Boolean(this._rows[index]);
};
TypedSheetData.prototype.row = function (index) {
if (index < 0) {
throw new Error("Row index must be >=0");
}
if (!this.exists(index)) {
this._rows[index] = this.createRow(index);
}
return this._rows[index];
};
TypedSheetData.prototype.load = function (args) {
var _a;
return tslib_1.__awaiter(this, void 0, void 0, function () {
var argRange, query, alreadyLoading, ns, promise;
var _this = this;
return tslib_1.__generator(this, function (_b) {
argRange = typeof args === 'string' ? args : (_a = args) === null || _a === void 0 ? void 0 : _a.range;
if (argRange) {
argRange = this.expandRange(argRange);
}
query = argRange || this.range;
alreadyLoading = this._loading.find(function (item) { return item.query === query; });
if (alreadyLoading) {
return [2, alreadyLoading.promise];
}
ns = this.uri.toString();
promise = new Promise(function (resolve, reject) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
var sheet, _a, total, error, range, min, max, wait;
var _this = this;
return tslib_1.__generator(this, function (_b) {
switch (_b.label) {
case 0:
this._status = 'LOADING';
sheet = this._sheet;
this.fire({
type: 'SHEET/loading',
payload: { sheet: sheet, range: query },
});
return [4, this._ctx.fetch.getCells({ ns: ns, query: query })];
case 1:
_a = _b.sent(), total = _a.total, error = _a.error;
if (error) {
reject(new Error(error.message));
}
range = common_1.coord.range.fromKey(query);
min = Math.max(0, range.left.row);
max = Math.min(total.rows - 1, range.right.row);
wait = Array.from({ length: max - min + 1 }).map(function (v, i) {
var index = i + min;
return _this.row(index).load();
});
return [4, Promise.all(wait)];
case 2:
_b.sent();
this._total = total.rows;
this._status = 'LOADED';
this._isLoaded = true;
this._loading = this._loading.filter(function (item) { return item.query !== query; });
this.fire({
type: 'SHEET/loaded',
payload: {
sheet: sheet,
range: this.range,
total: this.total,
},
});
return [2, resolve(this)];
}
});
}); });
this._loading = tslib_1.__spreadArrays(this._loading, [{ query: query, promise: promise }]);
return [2, promise];
});
});
};
TypedSheetData.prototype.filter = function (fn) {
return this.rows.filter(function (row, i) { return fn(row.props, i); });
};
TypedSheetData.prototype.find = function (fn) {
return this.rows.find(function (row, i) { return fn(row.props, i); });
};
TypedSheetData.prototype.map = function (fn) {
return this.rows.map(function (row, i) { return fn(row.props, i); });
};
TypedSheetData.prototype.forEach = function (fn) {
this.rows.forEach(function (row, i) { return fn(row.props, i); });
};
TypedSheetData.prototype.expandRange = function (range) {
range = TypedSheetData.formatRange(range);
this._range = this.isLoaded
? mergeRanges(range, this._range)
: range;
return this._range;
};
TypedSheetData.prototype.fire = function (e) {
this._ctx.event$.next(e);
};
TypedSheetData.prototype.createRow = function (index) {
var uri = common_1.Uri.create.row(this.uri.toString(), (index + 1).toString());
var columns = this.types;
var ctx = this._ctx;
var typename = this.typename;
var sheet = this._sheet;
return TypedSheetRow_1.TypedSheetRow.create({ sheet: sheet, typename: typename, uri: uri, columns: columns, ctx: ctx });
};
TypedSheetData.create = function (args) {
return new TypedSheetData(args);
};
TypedSheetData.DEFAULT = {
RANGE: '1:500',
PAGE: 500,
};
return TypedSheetData;
}());
exports.TypedSheetData = TypedSheetData;
var diff = function (a, b) { return a - b; };
var isInfinity = function (input) { return input === '*' || input === '**'; };
var mergeRanges = function (input1, input2) {
var range1 = common_1.coord.range.fromKey(input1).square;
var range2 = common_1.coord.range.fromKey(input2).square;
var min = Math.min(0, range1.left.row, range2.left.row) + 1;
var max = Math.max(range1.right.row, range2.right.row) + 1;
return min + ":" + max;
};