UNPKG

@leyyo/cache

Version:
154 lines (153 loc) 5.98 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.CacheChannelAbstract = void 0; const invalidator_1 = require("../invalidator"); const util_1 = require("../util"); const cache_channel_prop_impl_1 = require("./cache-channel-prop-impl"); const builder_1 = require("@leyyo/builder"); const hub_1 = require("../hub"); const config_1 = require("../config"); // noinspection JSUnusedGlobalSymbols class CacheChannelAbstract { // endregion properties // region constructor constructor(entity, path, prop, id, client) { this._pNames = []; this.entity = entity; this.path = path; this.id = id; this.client = client; this.prop = new cache_channel_prop_impl_1.CacheChannelPropImpl(prop, entity.prop); this.invalidator = new invalidator_1.CacheInvalidatorImpl(this); this._pNames = util_1.cacheUtil.parseProperties(this.prop.property); this.util = util_1.cacheUtil; hub_1.cacheHub.$secure.$checkInvalidatorConsumer(this.$flat); const parts = [this.entity.segment.path, this.entity.path, this.path] .map(item => util_1.cacheUtil.alphaNumeric(item)) .filter(item => !!item); if (parts.length > 0) { this.full = parts.join(config_1.DLM_BETWEEN_PARENTS) + config_1.DLM_AFTER_PARENT; } else { this.full = config_1.DLM_AFTER_PARENT; } } // endregion constructor // region plugins info(check) { return __awaiter(this, void 0, void 0, function* () { var _a, _b; return { id: (_a = this.id) !== null && _a !== void 0 ? _a : undefined, path: (_b = this.path) !== null && _b !== void 0 ? _b : undefined, client: yield this.client.info(check), prop: this.prop.$secure.$pure, }; }); } // endregion plugins changeProp(lambda) { this.$setProp(util_1.cacheUtil.readProp(lambda(builder_1.Builder.build()))); } // region secure get $flat() { return this; } get $secure() { return this; } get $back() { return this; } get $pNames() { return this._pNames; } $setProp(prop) { this.prop.$secure.$setPure(prop); this._pNames = util_1.cacheUtil.parseProperties(this.prop.property); } // endregion secure $invalidatorForInvalidate(data) { // Array<idBasicKeys> if (data.ids.length < 1) { return; } data.ids.forEach(id => { const { full } = this.format.invalidation(id); if (full) { this.hash.$secure .$getAll(full) .then(dataRec => { // Record<dataOwner, Array<dataFullKey>> const dataOwnerMap = {}; // Record<dataFullKey, dataOwner> for (const [dataFullKey, dataOwner] of Object.entries(dataRec)) { if (dataOwnerMap[dataOwner] === undefined) { dataOwnerMap[dataOwner] = []; } dataOwnerMap[dataOwner].push(dataFullKey); } for (const [dataOwner, dataFullKeys] of Object.entries(dataOwnerMap)) { // delete data keys if (dataOwner === this.id) { this.basic.$secure .$deleteMore(dataFullKeys) .then() .catch(e => { // todo console.log(e); }); } else { const consumer = hub_1.cacheHub.getInvalidatorConsumer(dataOwner); consumer.$invalidatorForDelete({ from: '', ids: dataFullKeys, }); } } // delete invalidator key this.basic.$secure .$delete(full) .then() .catch(e => { // todo console.log(e); }); }); } }); } // Record<idBasicKey, Record<dataFullKey, dataOwner>> $invalidatorForNotify(data) { for (const [idBasicKey, dataRec] of Object.entries(data === null || data === void 0 ? void 0 : data.records)) { if (Object.keys(dataRec).length < 1) { continue; } const { full } = this.format.invalidation(idBasicKey); if (full) { this.hash.$secure.$set(full, dataRec).then(); } } } $invalidatorForDelete(data) { // triggered from another channel this.basic.$secure .$deleteMore(data.ids) .then() .catch(e => { // todo console.log(e); }); } } exports.CacheChannelAbstract = CacheChannelAbstract;