@leyyo/cache
Version:
Common cache library
140 lines (132 loc) • 4.44 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CacheAbstractPropImpl = void 0;
const literal_1 = require("../literal");
// noinspection DuplicatedCode,JSUnusedLocalSymbols
class CacheAbstractPropImpl {
constructor(data, parent, validators) {
this._pure = {};
if (validators instanceof Map) {
this._validators = validators;
}
else {
this._validators = new Map();
}
this._init(data, parent);
}
// region internal
_init(pure, parent) {
if (parent && typeof parent === 'object' && !Array.isArray(parent)) {
for (const [k, v] of Object.entries(parent)) {
if (!["function", "symbol"].includes(typeof v)) {
this[k] = v;
}
}
}
this._setPure(pure);
}
_setPure(pure) {
for (const [k, v] of Object.entries(pure)) {
if (!["function", "symbol"].includes(typeof v)) {
this._pure[k] = v;
this[k] = v;
}
}
if (this._validators.size > 0) {
for (const [k, fn] of this._validators.entries()) {
if (!['undefined', 'function', 'symbol'].includes(typeof this[k])) {
this[k] = fn(this[k]);
}
}
}
}
_set(key, value, flag) {
if (flag) {
this._pure[key] = value;
this[key] = value;
}
else {
console.error(`Invalid prop value for ${key} as ${value}`);
}
}
_isNumber(value) {
return typeof value !== "number" || isNaN(value) || !isFinite(value);
}
// endregion internal
// region secure
$expiryMode(mode) {
return literal_1.ExpiryModeItems.includes(mode) ? mode : this.expiryMode;
}
$expirySpan(span) {
return literal_1.ExpirySpanItems.includes(span) ? span : this.expirySpan;
}
$expiryUnit(unit) {
return literal_1.ExpiryUnitItems.includes(unit) ? unit : this.expiryUnit;
}
$saveMode(mode) {
return literal_1.SaveModeItems.includes(mode) ? mode : this.saveMode;
}
$saveSpan(span) {
return literal_1.SaveSpanItems.includes(span) ? span : this.saveSpan;
}
$ttl(tuple) {
const [value, unit] = tuple;
const now = new Date().getTime();
if (!tuple || !Array.isArray(tuple) || tuple.length < 1) {
return now + this.milliseconds;
}
if (!this._isNumber(value) || value < 1) {
return now + this.milliseconds;
}
switch (unit !== null && unit !== void 0 ? unit : this.expiryUnit) {
case "milliseconds":
return now + value;
case "seconds":
return now + (value * 1000);
default: // minutes
return now + (value * 60000);
}
}
$timestamp(tuple) {
const [value, unit] = tuple;
const now = new Date().getTime();
if (!tuple || !Array.isArray(tuple) || tuple.length < 1) {
return now + this.milliseconds;
}
if (!this._isNumber(value) || value < 1) {
return now + this.milliseconds;
}
switch (unit !== null && unit !== void 0 ? unit : this.expiryUnit) {
case "milliseconds":
return value;
case "seconds":
return value * 1000;
default: // minutes
return value * 60000;
}
}
}
exports.CacheAbstractPropImpl = CacheAbstractPropImpl;
/*
* setEnabled(value: boolean) {
this._set('enabled', value, typeof value === 'boolean');
}
setExpiryMode(value: ExpiryMode) {
this._set('expiryMode', value, ExpiryModeItems.includes(value));
}
setExpirySpan(value: ExpirySpan) {
this._set('expirySpan', value, ExpirySpanItems.includes(value));
}
setExpiryUnit(value: ExpiryUnit) {
this._set('expiryUnit', value, ExpiryUnitItems.includes(value));
}
setMilliseconds(value: number) {
this._set('milliseconds', value, this._isNumber(value) && value > 0);
}
setSaveMode(value: SaveMode) {
this._set('saveMode', value, SaveModeItems.includes(value));
}
setSaveSpan(value: SaveSpan) {
this._set('saveSpan', value, SaveSpanItems.includes(value));
}
* */