@nest-omni/core 3.1.1-12 → 3.1.1-13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/cache/cache-metrics.service.d.ts +83 -0
- package/cache/cache-metrics.service.js +298 -0
- package/cache/cache-serialization.service.d.ts +29 -0
- package/cache/cache-serialization.service.js +217 -0
- package/cache/cache.health.d.ts +35 -0
- package/cache/cache.health.js +193 -0
- package/cache/cache.module.d.ts +24 -0
- package/cache/cache.module.js +105 -0
- package/cache/cache.service.d.ts +35 -0
- package/cache/cache.service.js +280 -0
- package/cache/cache.warmup.service.d.ts +34 -0
- package/cache/cache.warmup.service.js +199 -0
- package/cache/decorators/cache-evict.decorator.d.ts +2 -0
- package/cache/decorators/cache-evict.decorator.js +72 -0
- package/cache/decorators/cache-put.decorator.d.ts +2 -0
- package/cache/decorators/cache-put.decorator.js +48 -0
- package/cache/decorators/cacheable.decorator.d.ts +4 -0
- package/cache/decorators/cacheable.decorator.js +68 -0
- package/cache/decorators/index.d.ts +3 -0
- package/cache/decorators/index.js +11 -0
- package/cache/dependencies/callback.dependency.d.ts +11 -0
- package/cache/dependencies/callback.dependency.js +92 -0
- package/cache/dependencies/chain.dependency.d.ts +20 -0
- package/cache/dependencies/chain.dependency.js +113 -0
- package/cache/dependencies/db.dependency.d.ts +15 -0
- package/cache/dependencies/db.dependency.js +71 -0
- package/cache/dependencies/file.dependency.d.ts +17 -0
- package/cache/dependencies/file.dependency.js +63 -0
- package/cache/dependencies/index.d.ts +6 -0
- package/cache/dependencies/index.js +22 -0
- package/cache/dependencies/tag.dependency.d.ts +16 -0
- package/cache/dependencies/tag.dependency.js +75 -0
- package/cache/dependencies/time.dependency.d.ts +19 -0
- package/cache/dependencies/time.dependency.js +71 -0
- package/cache/examples/quick-start.d.ts +5 -0
- package/cache/examples/quick-start.js +341 -0
- package/cache/index.d.ts +14 -0
- package/cache/index.js +41 -0
- package/cache/interfaces/cache-dependency.interface.d.ts +11 -0
- package/cache/interfaces/cache-dependency.interface.js +2 -0
- package/cache/interfaces/cache-options.interface.d.ts +32 -0
- package/cache/interfaces/cache-options.interface.js +9 -0
- package/cache/interfaces/cache-provider.interface.d.ts +28 -0
- package/cache/interfaces/cache-provider.interface.js +2 -0
- package/cache/interfaces/index.d.ts +3 -0
- package/cache/interfaces/index.js +19 -0
- package/cache/providers/base-cache.provider.d.ts +16 -0
- package/cache/providers/base-cache.provider.js +32 -0
- package/cache/providers/cls-cache.provider.d.ts +16 -0
- package/cache/providers/cls-cache.provider.js +140 -0
- package/cache/providers/index.d.ts +4 -0
- package/cache/providers/index.js +23 -0
- package/cache/providers/memory-cache.provider.d.ts +23 -0
- package/cache/providers/memory-cache.provider.js +130 -0
- package/cache/providers/redis-cache.provider.d.ts +23 -0
- package/cache/providers/redis-cache.provider.js +215 -0
- package/cache/utils/dependency-manager.util.d.ts +15 -0
- package/cache/utils/dependency-manager.util.js +141 -0
- package/cache/utils/index.d.ts +2 -0
- package/cache/utils/index.js +18 -0
- package/cache/utils/key-generator.util.d.ts +13 -0
- package/cache/utils/key-generator.util.js +74 -0
- package/index.d.ts +1 -0
- package/index.js +1 -0
- package/package.json +4 -17
- package/redis-lock/index.d.ts +4 -0
- package/redis-lock/index.js +13 -0
- package/{setup → redis-lock}/redis-lock.decorator.d.ts +3 -0
- package/{setup → redis-lock}/redis-lock.decorator.js +25 -14
- package/redis-lock/redis-lock.examples.d.ts +13 -0
- package/redis-lock/redis-lock.examples.js +159 -0
- package/redis-lock/redis-lock.module.d.ts +23 -0
- package/redis-lock/redis-lock.module.js +117 -0
- package/{setup → redis-lock}/redis-lock.service.d.ts +9 -21
- package/{setup → redis-lock}/redis-lock.service.js +29 -103
- package/setup/index.d.ts +1 -2
- package/setup/index.js +1 -2
- package/setup/schedule.decorator.js +1 -1
- package/shared/serviceRegistryModule.js +63 -4
- package/shared/services/api-config.service.d.ts +0 -5
- package/shared/services/api-config.service.js +3 -27
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { DataSource } from 'typeorm';
|
|
2
|
+
import type { CacheDependency } from '../interfaces/cache-dependency.interface';
|
|
3
|
+
export declare class DbDependency implements CacheDependency {
|
|
4
|
+
private readonly sql;
|
|
5
|
+
private readonly paramsFactory?;
|
|
6
|
+
private readonly name?;
|
|
7
|
+
private static dataSource;
|
|
8
|
+
constructor(sql: string, paramsFactory?: (...args: any[]) => any[], name?: string);
|
|
9
|
+
static setDataSource(dataSource: DataSource): void;
|
|
10
|
+
static getDataSource(): DataSource | null;
|
|
11
|
+
getKey(): string;
|
|
12
|
+
getData(): Promise<any>;
|
|
13
|
+
isChanged(oldData: any): Promise<boolean>;
|
|
14
|
+
private hashCode;
|
|
15
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.DbDependency = void 0;
|
|
13
|
+
class DbDependency {
|
|
14
|
+
constructor(sql, paramsFactory, name) {
|
|
15
|
+
this.sql = sql;
|
|
16
|
+
this.paramsFactory = paramsFactory;
|
|
17
|
+
this.name = name;
|
|
18
|
+
if (!sql || typeof sql !== 'string') {
|
|
19
|
+
throw new Error('DbDependency requires a valid SQL query');
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
static setDataSource(dataSource) {
|
|
23
|
+
this.dataSource = dataSource;
|
|
24
|
+
}
|
|
25
|
+
static getDataSource() {
|
|
26
|
+
return this.dataSource;
|
|
27
|
+
}
|
|
28
|
+
getKey() {
|
|
29
|
+
const queryHash = this.hashCode(this.sql);
|
|
30
|
+
const name = this.name || 'db';
|
|
31
|
+
return `db:${name}:${queryHash}`;
|
|
32
|
+
}
|
|
33
|
+
getData() {
|
|
34
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
35
|
+
var _a;
|
|
36
|
+
if (!DbDependency.dataSource) {
|
|
37
|
+
throw new Error('DbDependency requires DataSource to be set. Call DbDependency.setDataSource(dataSource) during app initialization.');
|
|
38
|
+
}
|
|
39
|
+
try {
|
|
40
|
+
const params = ((_a = this.paramsFactory) === null || _a === void 0 ? void 0 : _a.call(this)) || [];
|
|
41
|
+
const result = yield DbDependency.dataSource.query(this.sql, params);
|
|
42
|
+
return JSON.stringify(result);
|
|
43
|
+
}
|
|
44
|
+
catch (error) {
|
|
45
|
+
throw new Error(`DbDependency query failed: ${error instanceof Error ? error.message : String(error)}`);
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
isChanged(oldData) {
|
|
50
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
51
|
+
try {
|
|
52
|
+
const currentData = yield this.getData();
|
|
53
|
+
return oldData !== currentData;
|
|
54
|
+
}
|
|
55
|
+
catch (_a) {
|
|
56
|
+
return true;
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
hashCode(str) {
|
|
61
|
+
let hash = 0;
|
|
62
|
+
for (let i = 0; i < str.length; i++) {
|
|
63
|
+
const char = str.charCodeAt(i);
|
|
64
|
+
hash = (hash << 5) - hash + char;
|
|
65
|
+
hash = hash & hash;
|
|
66
|
+
}
|
|
67
|
+
return Math.abs(hash).toString(36);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
exports.DbDependency = DbDependency;
|
|
71
|
+
DbDependency.dataSource = null;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { CacheDependency } from '../interfaces/cache-dependency.interface';
|
|
2
|
+
export declare class FileDependency implements CacheDependency {
|
|
3
|
+
private readonly filePath;
|
|
4
|
+
constructor(filePath: string | ((...args: any[]) => string));
|
|
5
|
+
getKey(): string;
|
|
6
|
+
getData(): Promise<{
|
|
7
|
+
path: string;
|
|
8
|
+
mtime: number;
|
|
9
|
+
size: number;
|
|
10
|
+
}>;
|
|
11
|
+
isChanged(oldData: {
|
|
12
|
+
path: string;
|
|
13
|
+
mtime: number;
|
|
14
|
+
size: number;
|
|
15
|
+
} | null): Promise<boolean>;
|
|
16
|
+
private resolveFilePath;
|
|
17
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.FileDependency = void 0;
|
|
13
|
+
const node_fs_1 = require("node:fs");
|
|
14
|
+
class FileDependency {
|
|
15
|
+
constructor(filePath) {
|
|
16
|
+
if (!filePath) {
|
|
17
|
+
throw new Error('FileDependency requires a file path');
|
|
18
|
+
}
|
|
19
|
+
this.filePath = filePath;
|
|
20
|
+
}
|
|
21
|
+
getKey() {
|
|
22
|
+
const path = typeof this.filePath === 'function' ? 'dynamic' : this.filePath;
|
|
23
|
+
return `file:${path}`;
|
|
24
|
+
}
|
|
25
|
+
getData() {
|
|
26
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
27
|
+
const path = yield this.resolveFilePath();
|
|
28
|
+
try {
|
|
29
|
+
const stats = yield node_fs_1.promises.stat(path);
|
|
30
|
+
return {
|
|
31
|
+
path,
|
|
32
|
+
mtime: stats.mtimeMs,
|
|
33
|
+
size: stats.size,
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
catch (error) {
|
|
37
|
+
throw new Error(`FileDependency: Cannot access file ${path}: ${error instanceof Error ? error.message : String(error)}`);
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
isChanged(oldData) {
|
|
42
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
43
|
+
try {
|
|
44
|
+
const currentData = yield this.getData();
|
|
45
|
+
return (currentData.path !== oldData.path ||
|
|
46
|
+
currentData.mtime !== oldData.mtime ||
|
|
47
|
+
currentData.size !== oldData.size);
|
|
48
|
+
}
|
|
49
|
+
catch (_a) {
|
|
50
|
+
return true;
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
resolveFilePath() {
|
|
55
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
56
|
+
if (typeof this.filePath === 'function') {
|
|
57
|
+
return this.filePath();
|
|
58
|
+
}
|
|
59
|
+
return this.filePath;
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
exports.FileDependency = FileDependency;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./callback.dependency"), exports);
|
|
18
|
+
__exportStar(require("./chain.dependency"), exports);
|
|
19
|
+
__exportStar(require("./db.dependency"), exports);
|
|
20
|
+
__exportStar(require("./file.dependency"), exports);
|
|
21
|
+
__exportStar(require("./tag.dependency"), exports);
|
|
22
|
+
__exportStar(require("./time.dependency"), exports);
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { CacheDependency } from '../interfaces/cache-dependency.interface';
|
|
2
|
+
export declare class TagDependency implements CacheDependency {
|
|
3
|
+
private readonly tags;
|
|
4
|
+
private static readonly PREFIX;
|
|
5
|
+
private static tagVersions;
|
|
6
|
+
constructor(tags: string[]);
|
|
7
|
+
static getTagVersion(tag: string): number;
|
|
8
|
+
static invalidateTag(tag: string): void;
|
|
9
|
+
static invalidateTags(tags: string[]): void;
|
|
10
|
+
static resetAllTags(): void;
|
|
11
|
+
static getAllTags(): Map<string, number>;
|
|
12
|
+
getKey(): string;
|
|
13
|
+
getData(): Promise<Record<string, number>>;
|
|
14
|
+
isChanged(oldData: Record<string, number>): Promise<boolean>;
|
|
15
|
+
reset(): Promise<void>;
|
|
16
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.TagDependency = void 0;
|
|
13
|
+
class TagDependency {
|
|
14
|
+
constructor(tags) {
|
|
15
|
+
this.tags = tags;
|
|
16
|
+
if (!tags || tags.length === 0) {
|
|
17
|
+
throw new Error('TagDependency requires at least one tag');
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
static getTagVersion(tag) {
|
|
21
|
+
return this.tagVersions.get(tag) || 0;
|
|
22
|
+
}
|
|
23
|
+
static invalidateTag(tag) {
|
|
24
|
+
const currentVersion = this.tagVersions.get(tag) || 0;
|
|
25
|
+
this.tagVersions.set(tag, currentVersion + 1);
|
|
26
|
+
}
|
|
27
|
+
static invalidateTags(tags) {
|
|
28
|
+
for (const tag of tags) {
|
|
29
|
+
this.invalidateTag(tag);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
static resetAllTags() {
|
|
33
|
+
this.tagVersions.clear();
|
|
34
|
+
}
|
|
35
|
+
static getAllTags() {
|
|
36
|
+
return new Map(this.tagVersions);
|
|
37
|
+
}
|
|
38
|
+
getKey() {
|
|
39
|
+
return `tag:${this.tags.sort().join(',')}`;
|
|
40
|
+
}
|
|
41
|
+
getData() {
|
|
42
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
43
|
+
const data = {};
|
|
44
|
+
for (const tag of this.tags) {
|
|
45
|
+
data[tag] = TagDependency.getTagVersion(tag);
|
|
46
|
+
}
|
|
47
|
+
return data;
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
isChanged(oldData) {
|
|
51
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
52
|
+
if (!oldData || typeof oldData !== 'object') {
|
|
53
|
+
return true;
|
|
54
|
+
}
|
|
55
|
+
for (const tag of this.tags) {
|
|
56
|
+
const currentVersion = TagDependency.getTagVersion(tag);
|
|
57
|
+
const oldVersion = oldData[tag];
|
|
58
|
+
if (currentVersion !== oldVersion) {
|
|
59
|
+
return true;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
return false;
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
reset() {
|
|
66
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
67
|
+
for (const tag of this.tags) {
|
|
68
|
+
TagDependency.invalidateTag(tag);
|
|
69
|
+
}
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
exports.TagDependency = TagDependency;
|
|
74
|
+
TagDependency.PREFIX = 'cache:tag:';
|
|
75
|
+
TagDependency.tagVersions = new Map();
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { CacheDependency } from '../interfaces/cache-dependency.interface';
|
|
2
|
+
export declare class TimeDependency implements CacheDependency {
|
|
3
|
+
private readonly duration;
|
|
4
|
+
private createdAt;
|
|
5
|
+
constructor(duration: number | (() => number));
|
|
6
|
+
getKey(): string;
|
|
7
|
+
getData(): Promise<{
|
|
8
|
+
createdAt: number;
|
|
9
|
+
duration: number;
|
|
10
|
+
}>;
|
|
11
|
+
isChanged(oldData: {
|
|
12
|
+
createdAt: number;
|
|
13
|
+
duration: number;
|
|
14
|
+
}): Promise<boolean>;
|
|
15
|
+
reset(): Promise<void>;
|
|
16
|
+
getRemainingTime(): number;
|
|
17
|
+
isExpired(): boolean;
|
|
18
|
+
private resolveDuration;
|
|
19
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.TimeDependency = void 0;
|
|
13
|
+
class TimeDependency {
|
|
14
|
+
constructor(duration) {
|
|
15
|
+
if (typeof duration !== 'number' && typeof duration !== 'function') {
|
|
16
|
+
throw new Error('TimeDependency requires duration in milliseconds or a function returning duration');
|
|
17
|
+
}
|
|
18
|
+
if (typeof duration === 'number' && duration <= 0) {
|
|
19
|
+
throw new Error('TimeDependency duration must be positive');
|
|
20
|
+
}
|
|
21
|
+
this.duration = duration;
|
|
22
|
+
this.createdAt = Date.now();
|
|
23
|
+
}
|
|
24
|
+
getKey() {
|
|
25
|
+
const durationValue = this.resolveDuration();
|
|
26
|
+
return `time:${durationValue}:${this.createdAt}`;
|
|
27
|
+
}
|
|
28
|
+
getData() {
|
|
29
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
30
|
+
return {
|
|
31
|
+
createdAt: this.createdAt,
|
|
32
|
+
duration: this.resolveDuration(),
|
|
33
|
+
};
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
isChanged(oldData) {
|
|
37
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
38
|
+
if (!oldData || typeof oldData !== 'object') {
|
|
39
|
+
return true;
|
|
40
|
+
}
|
|
41
|
+
const now = Date.now();
|
|
42
|
+
const elapsed = now - oldData.createdAt;
|
|
43
|
+
return elapsed >= oldData.duration;
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
reset() {
|
|
47
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
48
|
+
this.createdAt = Date.now();
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
getRemainingTime() {
|
|
52
|
+
const duration = this.resolveDuration();
|
|
53
|
+
const elapsed = Date.now() - this.createdAt;
|
|
54
|
+
const remaining = duration - elapsed;
|
|
55
|
+
return Math.max(0, remaining);
|
|
56
|
+
}
|
|
57
|
+
isExpired() {
|
|
58
|
+
return this.getRemainingTime() === 0;
|
|
59
|
+
}
|
|
60
|
+
resolveDuration() {
|
|
61
|
+
if (typeof this.duration === 'function') {
|
|
62
|
+
const value = this.duration();
|
|
63
|
+
if (typeof value !== 'number' || value <= 0) {
|
|
64
|
+
throw new Error('TimeDependency duration function must return a positive number');
|
|
65
|
+
}
|
|
66
|
+
return value;
|
|
67
|
+
}
|
|
68
|
+
return this.duration;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
exports.TimeDependency = TimeDependency;
|