UNPKG

@platform/cell.typesystem

Version:

The 'strongly typed sheets' system of the CellOS.

90 lines (89 loc) 3.7 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.wrapFetch = exports.isWrapped = void 0; var tslib_1 = require("tslib"); var common_1 = require("../common"); var TypeSystem_fetch_1 = require("../TypeSystem.fetch"); var TypeCacheCells_1 = require("./TypeCacheCells"); var TypeCacheKey_1 = require("./TypeCacheKey"); function isWrapped(fetch) { return fetch.cache instanceof common_1.MemoryCache; } exports.isWrapped = isWrapped; function wrapFetch(fetch, options) { var _this = this; if (options === void 0) { options = {}; } if (isWrapped(fetch)) { return fetch; } var _a = options.cache, cache = _a === void 0 ? common_1.MemoryCache.create() : _a, event$ = options.event$; var fetchKey = TypeCacheKey_1.TypeCacheKey.fetch; if (event$) { common_1.rx.payload(event$, 'TypedSheet/sync').subscribe(function (e) { var key = fetchKey('getCells', e.changes.uri); var entry = cache.get(key); if (entry) { entry.total.rows = -1; entry.sync(e.changes); } }); } var getNs = function (args) { return (0, tslib_1.__awaiter)(_this, void 0, void 0, function () { var key, res_1; return (0, tslib_1.__generator)(this, function (_a) { switch (_a.label) { case 0: key = fetchKey('getNs', args.ns.toString()); if (!cache.exists(key)) return [3, 1]; return [2, cache.get(key)]; case 1: return [4, fetch.getNs(args)]; case 2: res_1 = _a.sent(); if (res_1.ns && !res_1.error) { cache.put(key, res_1); } return [2, res_1]; } }); }); }; var getColumns = function (args) { return (0, tslib_1.__awaiter)(_this, void 0, void 0, function () { var key, res_2; return (0, tslib_1.__generator)(this, function (_a) { switch (_a.label) { case 0: key = fetchKey('getColumns', args.ns.toString()); if (!cache.exists(key)) return [3, 1]; return [2, cache.get(key)]; case 1: return [4, fetch.getColumns(args)]; case 2: res_2 = _a.sent(); if (res_2.columns && !res_2.error) { cache.put(key, res_2); } return [2, res_2]; } }); }); }; var getCells = function (args) { return (0, tslib_1.__awaiter)(_this, void 0, void 0, function () { var key, cells, res; return (0, tslib_1.__generator)(this, function (_a) { switch (_a.label) { case 0: key = fetchKey('getCells', args.ns.toString()); cells = cache.exists(key) ? cache.get(key) : cache.put(key, TypeCacheCells_1.TypeCacheCells.create(args.ns)).get(key); return [4, cells.query(args.query).get(fetch)]; case 1: res = _a.sent(); if (res.error || !res.cells || Object.keys(res.cells).length === 0) { cache.delete(key); } return [2, res]; } }); }); }; var res = (0, tslib_1.__assign)({ cache: cache, cacheKey: fetchKey }, TypeSystem_fetch_1.fetcher.fromFuncs({ getNs: getNs, getColumns: getColumns, getCells: getCells })); return res; } exports.wrapFetch = wrapFetch;