@darco2903/expiry-cache 2.0.2 → 3.0.0

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.
Files changed (45) hide show
  1. package/README.md +133 -25
  2. package/dist/ExpiryCache.d.ts +9 -22
  3. package/dist/ExpiryCache.js +8 -29
  4. package/dist/ExpiryCacheAsync.d.ts +11 -24
  5. package/dist/ExpiryCacheAsync.js +10 -34
  6. package/dist/ExpiryCacheSafe.d.ts +18 -0
  7. package/dist/ExpiryCacheSafe.js +31 -0
  8. package/dist/ExpiryCacheSafeAsync.d.ts +20 -0
  9. package/dist/ExpiryCacheSafeAsync.js +40 -0
  10. package/dist/ReturnOptions.d.ts +15 -0
  11. package/dist/ReturnOptions.js +24 -0
  12. package/dist/base/ExpiryCacheAsyncBase.d.ts +13 -0
  13. package/dist/base/ExpiryCacheAsyncBase.js +11 -0
  14. package/dist/base/ExpiryCacheBase.d.ts +112 -0
  15. package/dist/base/ExpiryCacheBase.js +177 -0
  16. package/dist/base/ExpiryCacheSyncBase.d.ts +5 -0
  17. package/dist/base/ExpiryCacheSyncBase.js +3 -0
  18. package/dist/base/index.d.ts +2 -0
  19. package/dist/base/index.js +2 -0
  20. package/dist/index.d.ts +4 -0
  21. package/dist/index.js +3 -0
  22. package/dist/interface/ExpiryCacheAsync.d.ts +6 -0
  23. package/dist/interface/ExpiryCacheAsync.js +2 -0
  24. package/dist/interface/ExpiryCacheSafe.d.ts +6 -0
  25. package/dist/interface/ExpiryCacheSafe.js +2 -0
  26. package/dist/interface/ExpiryCacheSync.d.ts +6 -0
  27. package/dist/interface/ExpiryCacheSync.js +2 -0
  28. package/dist/interface/ExpiryCacheUnsafe.d.ts +6 -0
  29. package/dist/interface/ExpiryCacheUnsafe.js +2 -0
  30. package/dist/interface/index.d.ts +4 -0
  31. package/dist/types/RefreshFunction.d.ts +11 -0
  32. package/dist/types/RefreshFunction.js +1 -0
  33. package/dist/types/ReturnType.d.ts +10 -0
  34. package/dist/types/ReturnType.js +1 -0
  35. package/dist/types/events.d.ts +8 -0
  36. package/dist/types/events.js +1 -0
  37. package/dist/types/index.d.ts +2 -0
  38. package/dist/types/index.js +1 -0
  39. package/dist/utils.d.ts +10 -0
  40. package/dist/utils.js +17 -0
  41. package/package.json +8 -5
  42. package/dist/ExpiryCacheBase.d.ts +0 -95
  43. package/dist/ExpiryCacheBase.js +0 -115
  44. package/dist/types.d.ts +0 -2
  45. /package/dist/{types.js → interface/index.js} +0 -0
@@ -0,0 +1,112 @@
1
+ import { TypedEmitterProtected } from "@darco2903/typed-emitter";
2
+ import { Time, Millisecond } from "@darco2903/secondthought";
3
+ import type { RefreshFunction } from "../types/RefreshFunction.js";
4
+ import type { CacheReturnType } from "../types/ReturnType.js";
5
+ import { ReturnOptions } from "../ReturnOptions.js";
6
+ import type { CacheEvents } from "../types/events.js";
7
+ export declare abstract class ExpiryCacheBase<T, F extends RefreshFunction<T, E>, M extends CacheEvents<T, E>, E = never> extends TypedEmitterProtected<M> {
8
+ /** The cached data. */
9
+ protected data: T;
10
+ /** The function to refresh the cached data. */
11
+ protected refreshFn: F;
12
+ private _expirationTimeout;
13
+ /** The time in milliseconds after which the cache expires. 0 means never expires. */
14
+ protected _expirationTime: Millisecond;
15
+ /** The expiration timestamp in milliseconds. 0 means never expires, -1 means already expired or no data. */
16
+ protected _expiresAt: Millisecond;
17
+ /**
18
+ * Gets the expiration time in milliseconds. This is the duration after which the cache expires, not the absolute expiration timestamp.
19
+ */
20
+ get expirationTime(): number;
21
+ /**
22
+ * Gets the expiration time as a Millisecond object. This is the duration after which the cache expires, not the absolute expiration timestamp.
23
+ */
24
+ get expirationTimeAsTime(): Millisecond;
25
+ /**
26
+ * Gets the expiration timestamp in milliseconds.
27
+ */
28
+ get expiresAt(): number;
29
+ /**
30
+ * Gets the expiration timestamp as a Millisecond object.
31
+ */
32
+ get expiresAtAsTime(): Millisecond;
33
+ /**
34
+ * Checks if the cache is set to expire.
35
+ */
36
+ get doesExpire(): boolean;
37
+ /**
38
+ * Checks if the cache is expired.
39
+ */
40
+ get isExpired(): boolean;
41
+ /**
42
+ * Gets the time to live (TTL) in milliseconds. Returns null if the cache does not expire.
43
+ */
44
+ get timeToLive(): number | null;
45
+ /**
46
+ * Gets the time to live (TTL) as a Millisecond object. Returns null if the cache does not expire.
47
+ */
48
+ get timeToLiveAsTime(): Millisecond | null;
49
+ /**
50
+ * Creates an instance of ExpiryCache.
51
+ * @param data The initial data to be cached.
52
+ * @param refreshFn The function to refresh the cached data.
53
+ * @param expirationTime The time in milliseconds after which the cache expires. Defaults to 60_000 (1 minute). If set to 0, the cache will never expire.
54
+ */
55
+ constructor(data: T, refreshFn: F, expirationTime?: number | Time);
56
+ /**
57
+ * Expires the cache immediately.
58
+ */
59
+ expire(): void;
60
+ /**
61
+ * Sets the cache to never expire.
62
+ */
63
+ neverExpire(): void;
64
+ private setExpirationTimeout;
65
+ /**
66
+ * Calculates the expiration timestamp based on the current time plus the given milliseconds. If ms is 0, it returns 0 to indicate never expires.
67
+ * @param ms The time in milliseconds after which the cache should expire. If set to 0, the cache will never expire.
68
+ */
69
+ protected expIn(ms: Millisecond): Millisecond;
70
+ /**
71
+ * Sets the expiration timestamp based on the current time plus the given milliseconds.
72
+ * @param ms The time in milliseconds after which the cache should expire. If set to 0, the cache will never expire.
73
+ */
74
+ setExpiresIn(ms: number | Time): void;
75
+ /**
76
+ * Sets the expiration timestamp to a specific time.
77
+ * @param expiresAt The expiration timestamp in milliseconds or as a Time object. If set to 0, the cache will never expire.
78
+ */
79
+ setExpiresAt(expiresAt: number | Time): void;
80
+ protected _setExpiresAt(expiresAt: Millisecond): void;
81
+ /**
82
+ * Sets the cached data and resets the expiration timestamp based on the expiration time.
83
+ * @param data The new data to be cached.
84
+ */
85
+ protected setData(data: T | ReturnOptions<T>): void;
86
+ /**
87
+ * Sets the cached data and sets a new expiration timestamp.
88
+ * @param expiresAt The new expiration timestamp in milliseconds. If set to 0, the cache will never expire.
89
+ */
90
+ protected setDataExpiresAt(data: T, expiresAt: Millisecond): void;
91
+ /**
92
+ * Sets the cached data and sets a new expiration time.
93
+ * @param expirationTime The new expiration time in milliseconds. If set to 0, the cache will never expire.
94
+ */
95
+ protected setDataExpiresIn(data: T, expirationTime: Millisecond): void;
96
+ /**
97
+ * Gets the cached data without checking expiration.
98
+ */
99
+ getRawData(): T;
100
+ /**
101
+ * Gets the cached data if not expired, otherwise returns null.
102
+ */
103
+ getData(): T | null;
104
+ /**
105
+ * @param args The arguments to pass to the refresh function.
106
+ */
107
+ abstract refresh(...args: Parameters<F>): CacheReturnType<T, E>;
108
+ /**
109
+ * @param args The arguments to pass to the refresh function in case the cache is expired.
110
+ */
111
+ abstract getDataOrRefresh(...args: Parameters<F>): CacheReturnType<T, E>;
112
+ }
@@ -0,0 +1,177 @@
1
+ import { TypedEmitterProtected } from "@darco2903/typed-emitter";
2
+ import { Time, Millisecond, Minute } from "@darco2903/secondthought";
3
+ import { ReturnOptions, ReturnOptionsExpiresAtClass, ReturnOptionsExpiresInClass } from "../ReturnOptions.js";
4
+ import { argToMs } from "../utils.js";
5
+ export class ExpiryCacheBase extends TypedEmitterProtected {
6
+ /**
7
+ * Gets the expiration time in milliseconds. This is the duration after which the cache expires, not the absolute expiration timestamp.
8
+ */
9
+ get expirationTime() {
10
+ return this._expirationTime.time;
11
+ }
12
+ /**
13
+ * Gets the expiration time as a Millisecond object. This is the duration after which the cache expires, not the absolute expiration timestamp.
14
+ */
15
+ get expirationTimeAsTime() {
16
+ return this._expirationTime;
17
+ }
18
+ /**
19
+ * Gets the expiration timestamp in milliseconds.
20
+ */
21
+ get expiresAt() {
22
+ return this._expiresAt.time;
23
+ }
24
+ /**
25
+ * Gets the expiration timestamp as a Millisecond object.
26
+ */
27
+ get expiresAtAsTime() {
28
+ return this._expiresAt;
29
+ }
30
+ /**
31
+ * Checks if the cache is set to expire.
32
+ */
33
+ get doesExpire() {
34
+ return this._expiresAt.time !== 0;
35
+ }
36
+ /**
37
+ * Checks if the cache is expired.
38
+ */
39
+ get isExpired() {
40
+ return this.doesExpire && Millisecond.now().greaterThanOrEqual(this._expiresAt);
41
+ }
42
+ /**
43
+ * Gets the time to live (TTL) in milliseconds. Returns null if the cache does not expire.
44
+ */
45
+ get timeToLive() {
46
+ if (this.doesExpire) {
47
+ return Math.max(0, this._expiresAt.clone().sub(Millisecond.now()).time);
48
+ }
49
+ return null;
50
+ }
51
+ /**
52
+ * Gets the time to live (TTL) as a Millisecond object. Returns null if the cache does not expire.
53
+ */
54
+ get timeToLiveAsTime() {
55
+ if (this.doesExpire) {
56
+ return Time.max(new Millisecond(0), this._expiresAt.clone().sub(Millisecond.now())).toMillisecond();
57
+ }
58
+ return null;
59
+ }
60
+ /**
61
+ * Creates an instance of ExpiryCache.
62
+ * @param data The initial data to be cached.
63
+ * @param refreshFn The function to refresh the cached data.
64
+ * @param expirationTime The time in milliseconds after which the cache expires. Defaults to 60_000 (1 minute). If set to 0, the cache will never expire.
65
+ */
66
+ constructor(data, refreshFn, expirationTime = new Minute(1)) {
67
+ super();
68
+ const expTime = argToMs(expirationTime);
69
+ this.data = data;
70
+ this._expiresAt = expTime.time === 0 ? expTime : Millisecond.now().add(expTime);
71
+ this._expirationTime = expTime;
72
+ this.refreshFn = refreshFn;
73
+ this._expirationTimeout = null;
74
+ this.setExpirationTimeout();
75
+ }
76
+ /**
77
+ * Expires the cache immediately.
78
+ */
79
+ expire() {
80
+ this._expiresAt = new Millisecond(-1);
81
+ this._emit("expired");
82
+ }
83
+ /**
84
+ * Sets the cache to never expire.
85
+ */
86
+ neverExpire() {
87
+ this._expiresAt = new Millisecond(0);
88
+ }
89
+ setExpirationTimeout() {
90
+ if (this._expirationTimeout) {
91
+ clearTimeout(this._expirationTimeout);
92
+ }
93
+ if (this.timeToLive !== null) {
94
+ this._expirationTimeout = setTimeout(() => {
95
+ this._expirationTimeout = null;
96
+ this._emit("expired");
97
+ }, this.timeToLive);
98
+ this._expirationTimeout.unref();
99
+ }
100
+ }
101
+ /**
102
+ * Calculates the expiration timestamp based on the current time plus the given milliseconds. If ms is 0, it returns 0 to indicate never expires.
103
+ * @param ms The time in milliseconds after which the cache should expire. If set to 0, the cache will never expire.
104
+ */
105
+ expIn(ms) {
106
+ return ms.time === 0 ? ms : Millisecond.now().add(ms);
107
+ }
108
+ /**
109
+ * Sets the expiration timestamp based on the current time plus the given milliseconds.
110
+ * @param ms The time in milliseconds after which the cache should expire. If set to 0, the cache will never expire.
111
+ */
112
+ setExpiresIn(ms) {
113
+ const t = argToMs(ms);
114
+ this._expirationTime = t;
115
+ this._setExpiresAt(this.expIn(t));
116
+ }
117
+ /**
118
+ * Sets the expiration timestamp to a specific time.
119
+ * @param expiresAt The expiration timestamp in milliseconds or as a Time object. If set to 0, the cache will never expire.
120
+ */
121
+ setExpiresAt(expiresAt) {
122
+ const t = argToMs(expiresAt);
123
+ this._setExpiresAt(t);
124
+ }
125
+ _setExpiresAt(expiresAt) {
126
+ this._expiresAt = expiresAt;
127
+ this.setExpirationTimeout();
128
+ }
129
+ /**
130
+ * Sets the cached data and resets the expiration timestamp based on the expiration time.
131
+ * @param data The new data to be cached.
132
+ */
133
+ setData(data) {
134
+ if (data instanceof ReturnOptions) {
135
+ if (data instanceof ReturnOptionsExpiresInClass) {
136
+ this.setDataExpiresIn(data.data, data.expiresIn);
137
+ }
138
+ else if (data instanceof ReturnOptionsExpiresAtClass) {
139
+ this.setDataExpiresAt(data.data, data.expiresAt);
140
+ }
141
+ }
142
+ else {
143
+ this.setDataExpiresIn(data, this._expirationTime);
144
+ }
145
+ }
146
+ /**
147
+ * Sets the cached data and sets a new expiration timestamp.
148
+ * @param expiresAt The new expiration timestamp in milliseconds. If set to 0, the cache will never expire.
149
+ */
150
+ setDataExpiresAt(data, expiresAt) {
151
+ this.data = data;
152
+ this._setExpiresAt(expiresAt);
153
+ }
154
+ /**
155
+ * Sets the cached data and sets a new expiration time.
156
+ * @param expirationTime The new expiration time in milliseconds. If set to 0, the cache will never expire.
157
+ */
158
+ setDataExpiresIn(data, expirationTime) {
159
+ this.data = data;
160
+ this.setExpiresIn(expirationTime);
161
+ }
162
+ /**
163
+ * Gets the cached data without checking expiration.
164
+ */
165
+ getRawData() {
166
+ return this.data;
167
+ }
168
+ /**
169
+ * Gets the cached data if not expired, otherwise returns null.
170
+ */
171
+ getData() {
172
+ if (this.isExpired) {
173
+ return null;
174
+ }
175
+ return this.data;
176
+ }
177
+ }
@@ -0,0 +1,5 @@
1
+ import { ExpiryCacheBase } from "./ExpiryCacheBase.js";
2
+ import type { RefreshFunctionSync } from "../types/RefreshFunction.js";
3
+ import type { CacheEvents } from "../types/events.js";
4
+ export declare abstract class ExpiryCacheSyncBase<T, F extends RefreshFunctionSync<T, E>, M extends CacheEvents<T, E>, E = never> extends ExpiryCacheBase<T, F, M, E> {
5
+ }
@@ -0,0 +1,3 @@
1
+ import { ExpiryCacheBase } from "./ExpiryCacheBase.js";
2
+ export class ExpiryCacheSyncBase extends ExpiryCacheBase {
3
+ }
@@ -0,0 +1,2 @@
1
+ export * from "./ExpiryCacheAsyncBase.js";
2
+ export * from "./ExpiryCacheSyncBase.js";
@@ -0,0 +1,2 @@
1
+ export * from "./ExpiryCacheAsyncBase.js";
2
+ export * from "./ExpiryCacheSyncBase.js";
package/dist/index.d.ts CHANGED
@@ -1,2 +1,6 @@
1
1
  export * from "./ExpiryCache.js";
2
2
  export * from "./ExpiryCacheAsync.js";
3
+ export * from "./ExpiryCacheSafe.js";
4
+ export * from "./ExpiryCacheSafeAsync.js";
5
+ export type * from "./types/index.js";
6
+ export { ReturnOptions } from "./ReturnOptions.js";
package/dist/index.js CHANGED
@@ -1,2 +1,5 @@
1
1
  export * from "./ExpiryCache.js";
2
2
  export * from "./ExpiryCacheAsync.js";
3
+ export * from "./ExpiryCacheSafe.js";
4
+ export * from "./ExpiryCacheSafeAsync.js";
5
+ export { ReturnOptions } from "./ReturnOptions.js";
@@ -0,0 +1,6 @@
1
+ import type { ReturnTypeAsync } from "../types/ReturnType.js";
2
+ import type { RefreshFunctionAsync } from "../types/RefreshFunction.js";
3
+ export declare abstract class IExpiryCacheAsync<T, F extends RefreshFunctionAsync<T, E>, E = any> {
4
+ abstract refresh(...args: Parameters<F>): ReturnTypeAsync<T, E>;
5
+ abstract getDataOrRefresh(...args: Parameters<F>): ReturnTypeAsync<T, E>;
6
+ }
@@ -0,0 +1,2 @@
1
+ export class IExpiryCacheAsync {
2
+ }
@@ -0,0 +1,6 @@
1
+ import type { ReturnTypeSafe } from "../types/ReturnType.js";
2
+ import type { RefreshFunctionSafe } from "../types/RefreshFunction.js";
3
+ export declare abstract class IExpiryCacheSafe<T, F extends RefreshFunctionSafe<T, E>, E> {
4
+ abstract refresh(...args: Parameters<F>): ReturnTypeSafe<T, E>;
5
+ abstract getDataOrRefresh(...args: Parameters<F>): ReturnTypeSafe<T, E>;
6
+ }
@@ -0,0 +1,2 @@
1
+ export class IExpiryCacheSafe {
2
+ }
@@ -0,0 +1,6 @@
1
+ import type { ReturnTypeSync } from "../types/ReturnType.js";
2
+ import type { RefreshFunctionSync } from "../types/RefreshFunction.js";
3
+ export declare abstract class IExpiryCacheSync<T, F extends RefreshFunctionSync<T, E>, E = any> {
4
+ abstract refresh(...args: Parameters<F>): ReturnTypeSync<T, E>;
5
+ abstract getDataOrRefresh(...args: Parameters<F>): ReturnTypeSync<T, E>;
6
+ }
@@ -0,0 +1,2 @@
1
+ export class IExpiryCacheSync {
2
+ }
@@ -0,0 +1,6 @@
1
+ import type { ReturnTypeUnsafe } from "../types/ReturnType.js";
2
+ import type { RefreshFunctionUnsafe } from "../types/RefreshFunction.js";
3
+ export declare abstract class IExpiryCacheUnsafe<T, F extends RefreshFunctionUnsafe<T>> {
4
+ abstract refresh(...args: Parameters<F>): ReturnTypeUnsafe<T>;
5
+ abstract getDataOrRefresh(...args: Parameters<F>): ReturnTypeUnsafe<T>;
6
+ }
@@ -0,0 +1,2 @@
1
+ export class IExpiryCacheUnsafe {
2
+ }
@@ -0,0 +1,4 @@
1
+ export type * from "./ExpiryCacheAsync.js";
2
+ export type * from "./ExpiryCacheSafe.js";
3
+ export type * from "./ExpiryCacheSync.js";
4
+ export type * from "./ExpiryCacheUnsafe.js";
@@ -0,0 +1,11 @@
1
+ import type { ReturnTypeSafeAsync, ReturnTypeSafeSync, ReturnTypeUnsafeAsync, ReturnTypeUnsafeSync } from "./ReturnType.js";
2
+ import type { ReturnOptions } from "../ReturnOptions.js";
3
+ export type RefreshFunctionUnsafeSync<T> = (...args: any[]) => ReturnTypeUnsafeSync<T | ReturnOptions<T>>;
4
+ export type RefreshFunctionUnsafeAsync<T> = (...args: any[]) => ReturnTypeUnsafeAsync<T | ReturnOptions<T>>;
5
+ export type RefreshFunctionSafeSync<T, E> = (...args: any[]) => ReturnTypeSafeSync<T | ReturnOptions<T>, E>;
6
+ export type RefreshFunctionSafeAsync<T, E> = (...args: any[]) => ReturnTypeSafeAsync<T | ReturnOptions<T>, E>;
7
+ export type RefreshFunctionUnsafe<T> = RefreshFunctionUnsafeSync<T> | RefreshFunctionUnsafeAsync<T>;
8
+ export type RefreshFunctionSafe<T, E> = RefreshFunctionSafeSync<T, E> | RefreshFunctionSafeAsync<T, E>;
9
+ export type RefreshFunctionSync<T, E = never> = RefreshFunctionUnsafeSync<T> | RefreshFunctionSafeSync<T, E>;
10
+ export type RefreshFunctionAsync<T, E = never> = RefreshFunctionUnsafeAsync<T> | RefreshFunctionSafeAsync<T, E>;
11
+ export type RefreshFunction<T, E = never> = RefreshFunctionUnsafe<T> | RefreshFunctionSafe<T, E>;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,10 @@
1
+ import type { Result, ResultAsync } from "neverthrow";
2
+ export type ReturnTypeUnsafeSync<T> = T;
3
+ export type ReturnTypeUnsafeAsync<T> = Promise<T>;
4
+ export type ReturnTypeSafeSync<T, U> = Result<T, U>;
5
+ export type ReturnTypeSafeAsync<T, U> = ResultAsync<T, U>;
6
+ export type ReturnTypeUnsafe<T> = ReturnTypeUnsafeSync<T> | ReturnTypeUnsafeAsync<T>;
7
+ export type ReturnTypeSafe<T, U> = ReturnTypeSafeSync<T, U> | ReturnTypeSafeAsync<T, U>;
8
+ export type ReturnTypeSync<T, U> = ReturnTypeUnsafeSync<T> | ReturnTypeSafeSync<T, U>;
9
+ export type ReturnTypeAsync<T, U> = ReturnTypeUnsafeAsync<T> | ReturnTypeSafeAsync<T, U>;
10
+ export type CacheReturnType<T, U> = ReturnTypeUnsafe<T> | ReturnTypeSafe<T, U>;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,8 @@
1
+ export interface CacheEventsUnsafe<T> {
2
+ expired: [];
3
+ refreshed: [T];
4
+ }
5
+ export interface CacheEventsSafe<T, E> extends CacheEventsUnsafe<T> {
6
+ error: [E];
7
+ }
8
+ export type CacheEvents<T, E = never> = CacheEventsSafe<T, E> | CacheEventsUnsafe<T>;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,2 @@
1
+ export { RefreshFunctionUnsafeSync, RefreshFunctionUnsafeAsync, RefreshFunctionSafeSync, RefreshFunctionSafeAsync, } from "./RefreshFunction.js";
2
+ export { ReturnTypeUnsafeSync, ReturnTypeUnsafeAsync, ReturnTypeSafeSync, ReturnTypeSafeAsync, } from "./ReturnType.js";
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,10 @@
1
+ import { type Time, Millisecond } from "@darco2903/secondthought";
2
+ import { ReturnOptions } from "./ReturnOptions.js";
3
+ /**
4
+ * Converts a number or Time object to milliseconds.
5
+ */
6
+ export declare function argToMs(arg: number | Time): Millisecond;
7
+ /**
8
+ * Maps the return value of the refresh function to the actual data type. If the result is an instance of ReturnOptions, it extracts the data from it. Otherwise, it returns the result directly.
9
+ */
10
+ export declare function mapRefreshReturn<T>(result: T | ReturnOptions<T>): T;
package/dist/utils.js ADDED
@@ -0,0 +1,17 @@
1
+ import { Millisecond } from "@darco2903/secondthought";
2
+ import { ReturnOptions } from "./ReturnOptions.js";
3
+ /**
4
+ * Converts a number or Time object to milliseconds.
5
+ */
6
+ export function argToMs(arg) {
7
+ return typeof arg === "number" ? new Millisecond(arg) : arg.toMillisecond();
8
+ }
9
+ /**
10
+ * Maps the return value of the refresh function to the actual data type. If the result is an instance of ReturnOptions, it extracts the data from it. Otherwise, it returns the result directly.
11
+ */
12
+ export function mapRefreshReturn(result) {
13
+ if (result instanceof ReturnOptions) {
14
+ return result.data;
15
+ }
16
+ return result;
17
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@darco2903/expiry-cache",
3
- "version": "2.0.2",
3
+ "version": "3.0.0",
4
4
  "author": "Darco2903",
5
5
  "description": "",
6
6
  "keywords": [],
@@ -14,20 +14,23 @@
14
14
  "publishConfig": {
15
15
  "access": "public"
16
16
  },
17
+ "dependencies": {
18
+ "@darco2903/typed-emitter": "^1.3.1"
19
+ },
17
20
  "peerDependencies": {
18
- "@darco2903/secondthought": "^1.3.0"
21
+ "@darco2903/secondthought": "^1.3.0",
22
+ "neverthrow": "^8.2.0"
19
23
  },
20
24
  "devDependencies": {
21
25
  "@darco2903/web-common": "^0.7.0",
22
- "@vitest/ui": "^3.2.4",
23
- "rimraf": "^6.1.0",
26
+ "@types/node": "^25.5.0",
27
+ "rimraf": "^6.1.3",
24
28
  "typescript": "^5.9.3",
25
29
  "vitest": "^3.2.4"
26
30
  },
27
31
  "scripts": {
28
32
  "build": "tsc",
29
33
  "test": "vitest",
30
- "test:ui": "vitest --ui",
31
34
  "clean": "rimraf -g dist darco2903-expiry-cache-*.tgz"
32
35
  }
33
36
  }
@@ -1,95 +0,0 @@
1
- import { type Time, Millisecond } from "@darco2903/secondthought";
2
- import type { RefreshFunction, RefreshFunctionAsync } from "./types.js";
3
- export declare abstract class ExpiryCacheBase<T, U extends RefreshFunction<T> | RefreshFunctionAsync<T>> {
4
- protected data: T;
5
- protected callback: U;
6
- readonly expirationTime: number;
7
- /** The expiration timestamp in milliseconds. 0 means never expires, -1 means already expired or no data. */
8
- protected _expiresAt: Millisecond;
9
- /**
10
- * Gets the expiration timestamp in milliseconds.
11
- */
12
- get expiresAt(): number;
13
- /**
14
- * Checks if the cache is set to expire.
15
- */
16
- get doesExpire(): boolean;
17
- /**
18
- * Checks if the cache is expired.
19
- */
20
- get isExpired(): boolean;
21
- /**
22
- * Gets the time to live (TTL) in milliseconds.
23
- */
24
- get timeToLive(): number | null;
25
- /**
26
- * Converts a number or Time object to milliseconds.
27
- */
28
- protected argToMs(arg: number | Time): Millisecond;
29
- /**
30
- * Creates an instance of ExpiryCache.
31
- * @param data The initial data to be cached.
32
- * @param callback The function to refresh the cached data.
33
- * @param expirationTime The time in milliseconds after which the cache expires. Defaults to 60_000 (1 minute). If set to 0, the cache will never expire.
34
- */
35
- constructor(data: T, callback: U, expirationTime?: number | Time);
36
- /**
37
- * Expires the cache immediately.
38
- */
39
- expire(): void;
40
- /**
41
- * Sets the cache to never expire.
42
- */
43
- neverExpire(): void;
44
- protected expIn(ms: Millisecond): Millisecond;
45
- /**
46
- * Sets the expiration timestamp based on the current time plus the given milliseconds.
47
- * @param ms The time in milliseconds after which the cache should expire. If set to 0, the cache will never expire.
48
- */
49
- setExpiresIn(ms: number | Time): void;
50
- /**
51
- * Sets the expiration timestamp to a specific time.
52
- * @param timestamp The expiration timestamp in milliseconds.
53
- */
54
- setExpiresAt(timestamp: number | Time): void;
55
- /**
56
- * Sets the cached data and resets the expiration timestamp based on the expiration time.
57
- */
58
- protected setData(data: T): void;
59
- /**
60
- * Sets the cached data and sets a new expiration timestamp.
61
- * @param expiresAt The new expiration timestamp in milliseconds.
62
- */
63
- protected setDataExpiresAt(data: T, expiresAt: Millisecond): void;
64
- /**
65
- * Sets the cached data and sets a new expiration time.
66
- * @param expirationTime The new expiration time in milliseconds.
67
- */
68
- protected setDataExpiresIn(data: T, expirationTime: Millisecond): void;
69
- /**
70
- * Gets the raw cached data without checking expiration.
71
- */
72
- getRawData(): T;
73
- /**
74
- * Gets the cached data if not expired, otherwise returns null.
75
- */
76
- getData(): T | null;
77
- /**
78
- * Refreshes the cached data using the callback function.
79
- */
80
- abstract refresh(...args: Parameters<U>): void | Promise<void>;
81
- /**
82
- * Gets the cached data or refreshes it if expired.
83
- */
84
- abstract getDataOrRefresh(...args: Parameters<U>): T | Promise<T>;
85
- /**
86
- * Refreshes the cached data and sets a new expiration timestamp.
87
- * @param expiresAt The new expiration timestamp in milliseconds.
88
- */
89
- abstract refreshExpiresAt(expiresAt: number | Time, ...args: Parameters<U>): void | Promise<void>;
90
- /**
91
- * Refreshes the cached data and sets a new expiration time.
92
- * @param expirationTime The new expiration time in milliseconds.
93
- */
94
- abstract refreshExpiresIn(expirationTime: number | Time, ...args: Parameters<U>): void | Promise<void>;
95
- }