@platform/cell.typesystem
Version:
The 'strongly typed sheets' system of the CellOS.
210 lines (209 loc) • 9.37 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var common_1 = require("./common");
var TypedSheet_1 = require("./TypedSheet");
var TypedSheetRefs = (function () {
function TypedSheetRefs(args) {
this.ns = common_1.Uri.ns(TypedSheetRefs.PLACEHOLDER, false);
this.typeDef = args.typeDef;
this.typename = args.typename;
this.parent = args.parent;
this._ctx = args.ctx;
}
TypedSheetRefs.create = function (args) {
return new TypedSheetRefs(args);
};
TypedSheetRefs.refLinkName = function (args) {
var typeDef = args.typeDef;
var nameKey = 'type';
return nameKey;
};
TypedSheetRefs.refLinkKey = function (args) {
var typeDef = args.typeDef;
var nameKey = TypedSheetRefs.refLinkName({ typeDef: typeDef });
return common_1.Schema.ref.links.toKey(nameKey);
};
TypedSheetRefs.refLink = function (args) {
var typeDef = args.typeDef, _a = args.links, links = _a === void 0 ? {} : _a;
var linkName = TypedSheetRefs.refLinkName({ typeDef: typeDef });
var linkKey = TypedSheetRefs.refLinkKey({ typeDef: typeDef });
var link = common_1.Schema.ref.links.find(links).byName(linkName);
return { linkKey: linkKey, linkName: linkName, link: link };
};
Object.defineProperty(TypedSheetRefs.prototype, "isLoaded", {
get: function () {
return Boolean(this._sheet);
},
enumerable: true,
configurable: true
});
Object.defineProperty(TypedSheetRefs.prototype, "sheet", {
get: function () {
if (!this.isLoaded) {
var err = "Sheet '" + this.ns.toString() + "' property called before ready [isLoaded].";
throw new Error(err);
}
return this._sheet;
},
enumerable: true,
configurable: true
});
TypedSheetRefs.prototype.load = function () {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var promise;
var _this = this;
return tslib_1.__generator(this, function (_a) {
if (this.isLoaded) {
return [2, this];
}
if (this._load) {
return [2, this._load];
}
this.fire({
type: 'SHEET/refs/loading',
payload: { sheet: this.parent.sheet, refs: this },
});
promise = new Promise(function (resolve, reject) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
var _a, fetch, cache, event$, pool, linkInfo, link, pooledSheet, def, _b;
return tslib_1.__generator(this, function (_c) {
switch (_c.label) {
case 0:
_a = this._ctx, fetch = _a.fetch, cache = _a.cache, event$ = _a.event$, pool = _a.pool;
return [4, this.getLink()];
case 1:
linkInfo = _c.sent();
link = linkInfo.link;
pooledSheet = link && link.uri.type === 'NS' ? pool.sheet(link.uri) : undefined;
if (!pooledSheet) return [3, 2];
this._sheet = pooledSheet;
return [3, 5];
case 2: return [4, this.ensureLink(linkInfo)];
case 3:
_c.sent();
def = this.typeDef;
_b = this;
return [4, TypedSheet_1.TypedSheet.create({
implements: def.type.uri,
ns: this.ns.toString(),
fetch: fetch,
cache: cache,
event$: event$,
pool: pool,
})];
case 4:
_b._sheet = _c.sent();
_c.label = 5;
case 5:
delete this._load;
this.fire({
type: 'SHEET/refs/loaded',
payload: { sheet: this.parent.sheet, refs: this },
});
resolve(this);
return [2];
}
});
}); });
this._load = promise;
return [2, promise];
});
});
};
TypedSheetRefs.prototype.data = function (options) {
if (options === void 0) { options = {}; }
return tslib_1.__awaiter(this, void 0, void 0, function () {
var typename;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
if (!!this.isLoaded) return [3, 2];
return [4, this.load()];
case 1:
_a.sent();
_a.label = 2;
case 2:
typename = this.typename;
return [2, this.sheet.data(tslib_1.__assign(tslib_1.__assign({}, options), { typename: typename })).load()];
}
});
});
};
TypedSheetRefs.prototype.fire = function (e) {
this._ctx.event$.next(e);
};
TypedSheetRefs.prototype.getCell = function () {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var ns, key, query, res;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
ns = this.parent.cell.ns;
key = this.parent.cell.key;
query = key + ":" + key;
return [4, this._ctx.fetch.getCells({ ns: ns, query: query })];
case 1:
res = _a.sent();
return [2, (res.cells || {})[key]];
}
});
});
};
TypedSheetRefs.prototype.getLink = function () {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var typeDef, data, links, _a, linkKey, link;
return tslib_1.__generator(this, function (_b) {
switch (_b.label) {
case 0:
typeDef = this.typeDef;
return [4, this.getCell()];
case 1:
data = (_b.sent()) || {};
links = data.links || {};
_a = TypedSheetRefs.refLink({ typeDef: typeDef, links: links }), linkKey = _a.linkKey, link = _a.link;
return [2, { data: data, links: links, linkKey: linkKey, link: link }];
}
});
});
};
TypedSheetRefs.prototype.ensureLink = function (input) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var _a, data, links, linkKey, link, _b, payload, isChanged;
var _c;
return tslib_1.__generator(this, function (_d) {
switch (_d.label) {
case 0:
_b = input;
if (_b) return [3, 2];
return [4, this.getLink()];
case 1:
_b = (_d.sent());
_d.label = 2;
case 2:
_a = _b, data = _a.data, links = _a.links, linkKey = _a.linkKey, link = _a.link;
if (this.ns.toString() === TypedSheetRefs.PLACEHOLDER) {
this.ns = link
? common_1.Uri.ns(link.uri.toString())
: common_1.Uri.ns(common_1.Schema.cuid());
}
if (!links[linkKey]) {
payload = {
kind: 'CELL',
ns: this.parent.sheet.uri.toString(),
key: this.parent.cell.key,
to: tslib_1.__assign(tslib_1.__assign({}, data), { links: tslib_1.__assign(tslib_1.__assign({}, links), (_c = {}, _c[linkKey] = this.ns.toString(), _c)) }),
};
isChanged = !common_1.R.equals(data, payload.to);
if (isChanged) {
this.fire({ type: 'SHEET/change', payload: payload });
}
}
return [2];
}
});
});
};
TypedSheetRefs.PLACEHOLDER = "ns:" + '0'.repeat(25);
return TypedSheetRefs;
}());
exports.TypedSheetRefs = TypedSheetRefs;