@leyyo/cache
Version:
Common cache library
81 lines (80 loc) • 3.36 kB
JavaScript
"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.CacheEntityAbstract = void 0;
const cache_entity_prop_impl_1 = require("./cache-entity-prop-impl");
const builder_1 = require("@leyyo/builder");
const hub_1 = require("../hub");
const util_1 = require("../util");
// noinspection JSUnusedGlobalSymbols
class CacheEntityAbstract {
// endregion properties
// region constructor
constructor(segment, path, prop, id, defaultClient) {
this._channels = [];
this.path = path;
this.id = id;
this.segment = segment;
this.defaultClient = defaultClient;
this.prop = new cache_entity_prop_impl_1.CacheEntityPropImpl(prop, segment.prop);
hub_1.cacheHub.$secure.$checkEntity(this.$flat);
}
// endregion constructor
get channels() {
return [...this._channels];
}
newChannel(path, fn, id, differentClient) {
path = util_1.cacheUtil.checkName('entity.newChannel', 'path', path, true);
id = util_1.cacheUtil.checkName('entity.newChannel', 'id', id, true);
if (differentClient !== undefined) {
util_1.cacheUtil.checkObject('entity.newChannel', 'client', differentClient, 'leyyo.cache,CacheClient');
}
else {
differentClient = this.defaultClient;
}
util_1.cacheUtil.checkLambda('entity.newChannel', 'lambda', fn, 1);
const prop = util_1.cacheUtil.readProp(fn(builder_1.Builder.build()));
const lambda = hub_1.cacheHub.$secure.$getChannelCreator(differentClient.provider);
const channel = lambda(this.$flat, path, prop, id, differentClient);
this._channels.push(channel.$flat);
return channel;
}
info(check) {
return __awaiter(this, void 0, void 0, function* () {
const promises = this._channels.map(c => c.info(check));
return {
id: this.id ? this.id : undefined,
path: this.path ? this.path : undefined,
defaultClient: yield this.defaultClient.info(check),
prop: this.prop.$secure.$pure,
channels: yield Promise.all(promises),
};
});
}
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;
}
$setProp(prop) {
this.prop.$secure.$setPure(prop);
this._channels.forEach(channel => channel.$secure.$setProp(prop));
}
}
exports.CacheEntityAbstract = CacheEntityAbstract;