@leyyo/cache
Version:
Common cache library
261 lines (260 loc) • 7.89 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.CacheInvalidatorImpl = void 0;
const cache_invalidator_repo_impl_1 = require("./cache-invalidator-repo-impl");
// noinspection JSUnusedGlobalSymbols,JSUnusedLocalSymbols
class CacheInvalidatorImpl {
constructor(channel) {
this.channel = channel;
}
// region private
_disabled(result, command) {
const obj = {
keys: [],
disabled: true,
result,
command,
add: (i, c) => obj,
addMore: (i, c) => obj,
addFromRelations: (doc) => obj,
delete: (id) => obj,
deleteMore: (id) => obj,
};
return obj;
}
_ignore(result, error, command) {
const obj = {
keys: [],
result,
error,
command,
add: (i, c) => obj,
addMore: (i, c) => obj,
addFromRelations: (doc) => obj,
delete: (id) => obj,
deleteMore: (id) => obj,
};
return obj;
}
// endregion private
// region link-to-channel
_grabId(properties, doc) {
if (properties.length < 1) {
return null;
}
const idParts = properties.map(p => doc[p]).filter(p => doc[p] !== undefined);
return idParts.length > 0 ? this.channel.format.basic(idParts) : null;
}
_add(input, id, consumer) {
if (input.keys.length < 1) {
return input;
}
id = this.channel.format.basic(id);
if (id) {
input.keys.forEach(key => this._notify(key, [id], consumer === null || consumer === void 0 ? void 0 : consumer.id));
}
return input;
}
_addMore(input, ids, consumer) {
if (input.keys.length < 1) {
return input;
}
const arr = this.channel.format.basics(ids);
if (arr.length > 0) {
input.keys.forEach(key => this._notify(key, arr, consumer === null || consumer === void 0 ? void 0 : consumer.id));
}
return input;
}
_addFromRelationsSelf(doc, prop, keys) {
let properties;
if (!prop) {
properties = this.channel.$secure.$pNames;
}
else {
properties = this.channel.format.checkName(prop);
if (properties.length < 1) {
properties = this.channel.$secure.$pNames;
}
}
if (properties.length > 0) {
const id = this._grabId(properties, doc);
if (id) {
keys.forEach(key => this._notify(key, [id]));
}
}
}
_addFromRelationsRemote(doc, prop, keys, consumerId) {
if (!prop) {
return;
}
const properties = this.channel.format.checkName(prop);
if (properties.length < 1) {
return;
}
const id = this._grabId(properties, doc);
if (id) {
keys.forEach(key => this._notify(key, [id], consumerId));
}
}
_addFromRelationsGroup(doc, relation, keys) {
var _a;
switch (relation.resource) {
case "self":
this._addFromRelationsSelf(doc, relation.property, keys);
break;
case "consumer":
this._addFromRelationsRemote(doc, relation.property, keys, (_a = relation.consumer) === null || _a === void 0 ? void 0 : _a.id);
break;
case "consumerId":
this._addFromRelationsRemote(doc, relation.property, keys, relation.consumerId);
}
}
_addFromRelations(input, doc, relations) {
if (input.keys.length < 1 || !doc) {
return input;
}
// itself
if (!relations) {
this._addFromRelationsSelf(doc, null, input.keys);
}
else if (Array.isArray(relations)) {
relations.forEach(relation => {
this._addFromRelationsGroup(doc, relation, input.keys);
});
}
else if (typeof relations === 'object') {
this._addFromRelationsGroup(doc, relations, input.keys);
}
return input;
}
_delete(input, id) {
id = this.channel.format.basic(id);
if (id) {
this._invalidate(id);
}
return input;
}
_deleteMore(input, ids) {
ids = this.channel.format.basics(ids);
if (ids.length > 0) {
ids.forEach(id => this._invalidate(id));
}
return input;
}
// endregion link-to-channel
// region execution
failed(error, keys, command) {
const obj = {
error,
command,
keys,
add: (i, c) => obj,
addMore: (i, c) => obj,
addFromRelations: (doc) => obj,
delete: (id) => obj,
deleteMore: (id) => obj,
};
return obj;
}
success(result, keys, command) {
const obj = {
result,
command,
keys: keys,
success: true,
add: (id, channel) => this._add(obj, id, channel),
addMore: (ids, channel) => this._addMore(obj, ids, channel),
addFromRelations: (doc, relations) => this._addFromRelations(obj, doc, relations),
delete: (id) => this._delete(obj, id),
deleteMore: (ids) => this._deleteMore(obj, ids),
};
return obj;
}
// endregion execution
// region disabled
disabledArray(cmd) {
return this._disabled([], cmd);
}
disabledFalse(cmd) {
return this._disabled(false, cmd);
}
disabledText(cmd) {
return this._disabled(null, cmd);
}
disabledNull(cmd) {
return this._disabled(null, cmd);
}
disabledRecord(cmd) {
return this._disabled({}, cmd);
}
disabledTrue(cmd) {
return this._disabled(true, cmd);
}
disabledNumber(v1, v2) {
let def;
let cmd;
if (v1 === undefined && v2 === undefined) {
def = 0;
}
else if (typeof v1 === 'number') {
def = v1;
cmd = v2;
}
return this._disabled(def, cmd);
}
// endregion disabled
// region ignore
ignoreArray(error, cmd) {
return this._ignore([], error, cmd);
}
ignoreFalse(error, cmd) {
return this._ignore(false, error, cmd);
}
ignoreText(error, cmd) {
return this._ignore(null, error, cmd);
}
ignoreNull(error, cmd) {
return this._ignore(null, error, cmd);
}
ignoreRecord(error, cmd) {
return this._ignore({}, error, cmd);
}
ignoreTrue(error, cmd) {
return this._ignore(true, error, cmd);
}
ignoreNumber(v1, v2, v3) {
let def;
let error;
let cmd;
if (v1 === undefined && v2 === undefined && v3 === undefined) {
def = 0;
}
else if (typeof v1 === 'number') {
def = v1;
error = v2;
cmd = v3;
}
else if (typeof v1 === 'string' || v1 instanceof Error) {
def = 0;
error = v1;
cmd = v2;
}
return this._ignore(def, error, cmd);
}
// endregion ignore
// region secure
get $back() {
return this;
}
get $secure() {
return this;
}
_notify(fullDataKey, basicIds, idOwner) {
idOwner = idOwner !== null && idOwner !== void 0 ? idOwner : this.channel.id;
cache_invalidator_repo_impl_1.invalidatorRepo.notify(this.channel.id, fullDataKey, idOwner, basicIds);
}
_invalidate(basicId) {
cache_invalidator_repo_impl_1.invalidatorRepo.invalidate(this.channel.id, basicId);
}
}
exports.CacheInvalidatorImpl = CacheInvalidatorImpl;