@leyyo/cache
Version:
Common cache library
78 lines (77 loc) • 3.33 kB
JavaScript
;
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.CacheSegmentAbstract = void 0;
const cache_segment_prop_impl_1 = require("./cache-segment-prop-impl");
const hub_1 = require("../hub");
const builder_1 = require("@leyyo/builder");
const util_1 = require("../util");
// noinspection JSUnusedGlobalSymbols
class CacheSegmentAbstract {
// endregion properties
// region constructor
constructor(defaultClient, path, prop, id) {
this._entities = [];
this.defaultClient = defaultClient;
this.path = path;
this.id = id;
this.prop = new cache_segment_prop_impl_1.CacheSegmentPropImpl(prop, hub_1.cacheHub.prop);
hub_1.cacheHub.$secure.$checkSegment(this);
}
// endregion constructor
get entities() {
return [...this._entities];
}
newEntity(path, fn, id, differentClient) {
id = util_1.cacheUtil.checkName('segment.newEntity', 'id', id, true);
path = util_1.cacheUtil.checkName('segment.newSegment', 'path', path, true);
if (differentClient !== undefined) {
util_1.cacheUtil.checkObject('segment.newEntity', 'client', differentClient, 'leyyo.cache,CacheClient');
}
else {
differentClient = this.defaultClient;
}
util_1.cacheUtil.checkLambda('segment.newEntity', 'lambda', fn, 1);
const prop = builder_1.Builder.retrieve(fn);
const lambda = hub_1.cacheHub.$secure.$getEntityCreator(differentClient.provider);
const entity = lambda(this, path, prop, id, differentClient);
this._entities.push(entity.$flat);
return entity;
}
info(check) {
return __awaiter(this, void 0, void 0, function* () {
var _a, _b;
const promises = this._entities.map(c => c.info(check));
return {
id: (_a = this.id) !== null && _a !== void 0 ? _a : undefined,
path: (_b = this.path) !== null && _b !== void 0 ? _b : undefined,
defaultClient: yield this.defaultClient.info(check),
prop: this.prop.$secure.$pure,
entities: yield Promise.all(promises),
};
});
}
changeProp(lambda) {
this.$setProp(util_1.cacheUtil.readProp(lambda(builder_1.Builder.build())));
}
// region secure
get $secure() {
return this;
}
get $back() {
return this;
}
$setProp(prop) {
this.prop.$secure.$setPure(prop);
this._entities.forEach(entity => entity.$secure.$setProp(prop));
}
}
exports.CacheSegmentAbstract = CacheSegmentAbstract;