@ls-stack/utils 2.13.0 → 2.14.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.
- package/dist/cache.cjs +12 -2
- package/dist/cache.d.cts +11 -2
- package/dist/cache.d.ts +11 -2
- package/dist/cache.js +12 -2
- package/package.json +4 -4
package/dist/cache.cjs
CHANGED
|
@@ -132,7 +132,7 @@ function createCache({
|
|
|
132
132
|
}
|
|
133
133
|
};
|
|
134
134
|
return {
|
|
135
|
-
getOrInsert(cacheKey, val) {
|
|
135
|
+
getOrInsert(cacheKey, val, options) {
|
|
136
136
|
const now = Date.now();
|
|
137
137
|
const entry = cache.get(cacheKey);
|
|
138
138
|
if (!entry || isExpired(entry, now)) {
|
|
@@ -140,6 +140,9 @@ function createCache({
|
|
|
140
140
|
if (value instanceof RejectValue) {
|
|
141
141
|
return value.value;
|
|
142
142
|
}
|
|
143
|
+
if (options?.rejectWhen?.(value)) {
|
|
144
|
+
return value;
|
|
145
|
+
}
|
|
143
146
|
const unwrappedValue = unwrapValue(value, now);
|
|
144
147
|
cache.set(cacheKey, unwrappedValue);
|
|
145
148
|
trimToSize();
|
|
@@ -153,7 +156,7 @@ function createCache({
|
|
|
153
156
|
}
|
|
154
157
|
return entry.value;
|
|
155
158
|
},
|
|
156
|
-
async getOrInsertAsync(cacheKey, val) {
|
|
159
|
+
async getOrInsertAsync(cacheKey, val, options) {
|
|
157
160
|
const entry = cache.get(cacheKey);
|
|
158
161
|
if (entry && isPromise(entry.value)) {
|
|
159
162
|
return entry.value;
|
|
@@ -170,6 +173,13 @@ function createCache({
|
|
|
170
173
|
}
|
|
171
174
|
return result.value;
|
|
172
175
|
}
|
|
176
|
+
if (options?.rejectWhen?.(result)) {
|
|
177
|
+
const cacheValue = cache.get(cacheKey);
|
|
178
|
+
if (cacheValue?.value === promise) {
|
|
179
|
+
cache.delete(cacheKey);
|
|
180
|
+
}
|
|
181
|
+
return result;
|
|
182
|
+
}
|
|
173
183
|
const unwrappedValue = unwrapValue(result, Date.now());
|
|
174
184
|
cache.set(cacheKey, unwrappedValue);
|
|
175
185
|
return unwrappedValue.value;
|
package/dist/cache.d.cts
CHANGED
|
@@ -42,9 +42,18 @@ type Utils<T> = {
|
|
|
42
42
|
*/
|
|
43
43
|
withExpiration: (value: T, expiration: DurationObj) => WithExpiration<T>;
|
|
44
44
|
};
|
|
45
|
+
type GetOptions<T> = {
|
|
46
|
+
/**
|
|
47
|
+
* A function that determines whether a value should be rejected from being cached.
|
|
48
|
+
* If the function returns true, the value will be returned but not cached.
|
|
49
|
+
* @param value The value to check
|
|
50
|
+
* @returns true if the value should be rejected, false otherwise
|
|
51
|
+
*/
|
|
52
|
+
rejectWhen?: (value: T) => boolean;
|
|
53
|
+
};
|
|
45
54
|
type Cache<T> = {
|
|
46
|
-
getOrInsert: (cacheKey: string, val: (utils: Utils<T>) => T | RejectValue<T>) => T;
|
|
47
|
-
getOrInsertAsync: (cacheKey: string, val: (utils: Utils<T>) => Promise<T | RejectValue<T
|
|
55
|
+
getOrInsert: (cacheKey: string, val: (utils: Utils<T>) => T | RejectValue<T>, options?: GetOptions<T>) => T;
|
|
56
|
+
getOrInsertAsync: (cacheKey: string, val: (utils: Utils<T>) => Promise<T | RejectValue<T>>, options?: GetOptions<T>) => Promise<T>;
|
|
48
57
|
clear: () => void;
|
|
49
58
|
get: (cacheKey: string) => T | undefined;
|
|
50
59
|
set: (cacheKey: string, value: T | WithExpiration<T>) => void;
|
package/dist/cache.d.ts
CHANGED
|
@@ -42,9 +42,18 @@ type Utils<T> = {
|
|
|
42
42
|
*/
|
|
43
43
|
withExpiration: (value: T, expiration: DurationObj) => WithExpiration<T>;
|
|
44
44
|
};
|
|
45
|
+
type GetOptions<T> = {
|
|
46
|
+
/**
|
|
47
|
+
* A function that determines whether a value should be rejected from being cached.
|
|
48
|
+
* If the function returns true, the value will be returned but not cached.
|
|
49
|
+
* @param value The value to check
|
|
50
|
+
* @returns true if the value should be rejected, false otherwise
|
|
51
|
+
*/
|
|
52
|
+
rejectWhen?: (value: T) => boolean;
|
|
53
|
+
};
|
|
45
54
|
type Cache<T> = {
|
|
46
|
-
getOrInsert: (cacheKey: string, val: (utils: Utils<T>) => T | RejectValue<T>) => T;
|
|
47
|
-
getOrInsertAsync: (cacheKey: string, val: (utils: Utils<T>) => Promise<T | RejectValue<T
|
|
55
|
+
getOrInsert: (cacheKey: string, val: (utils: Utils<T>) => T | RejectValue<T>, options?: GetOptions<T>) => T;
|
|
56
|
+
getOrInsertAsync: (cacheKey: string, val: (utils: Utils<T>) => Promise<T | RejectValue<T>>, options?: GetOptions<T>) => Promise<T>;
|
|
48
57
|
clear: () => void;
|
|
49
58
|
get: (cacheKey: string) => T | undefined;
|
|
50
59
|
set: (cacheKey: string, value: T | WithExpiration<T>) => void;
|
package/dist/cache.js
CHANGED
|
@@ -88,7 +88,7 @@ function createCache({
|
|
|
88
88
|
}
|
|
89
89
|
};
|
|
90
90
|
return {
|
|
91
|
-
getOrInsert(cacheKey, val) {
|
|
91
|
+
getOrInsert(cacheKey, val, options) {
|
|
92
92
|
const now = Date.now();
|
|
93
93
|
const entry = cache.get(cacheKey);
|
|
94
94
|
if (!entry || isExpired(entry, now)) {
|
|
@@ -96,6 +96,9 @@ function createCache({
|
|
|
96
96
|
if (value instanceof RejectValue) {
|
|
97
97
|
return value.value;
|
|
98
98
|
}
|
|
99
|
+
if (options?.rejectWhen?.(value)) {
|
|
100
|
+
return value;
|
|
101
|
+
}
|
|
99
102
|
const unwrappedValue = unwrapValue(value, now);
|
|
100
103
|
cache.set(cacheKey, unwrappedValue);
|
|
101
104
|
trimToSize();
|
|
@@ -109,7 +112,7 @@ function createCache({
|
|
|
109
112
|
}
|
|
110
113
|
return entry.value;
|
|
111
114
|
},
|
|
112
|
-
async getOrInsertAsync(cacheKey, val) {
|
|
115
|
+
async getOrInsertAsync(cacheKey, val, options) {
|
|
113
116
|
const entry = cache.get(cacheKey);
|
|
114
117
|
if (entry && isPromise(entry.value)) {
|
|
115
118
|
return entry.value;
|
|
@@ -126,6 +129,13 @@ function createCache({
|
|
|
126
129
|
}
|
|
127
130
|
return result.value;
|
|
128
131
|
}
|
|
132
|
+
if (options?.rejectWhen?.(result)) {
|
|
133
|
+
const cacheValue = cache.get(cacheKey);
|
|
134
|
+
if (cacheValue?.value === promise) {
|
|
135
|
+
cache.delete(cacheKey);
|
|
136
|
+
}
|
|
137
|
+
return result;
|
|
138
|
+
}
|
|
129
139
|
const unwrappedValue = unwrapValue(result, Date.now());
|
|
130
140
|
cache.set(cacheKey, unwrappedValue);
|
|
131
141
|
return unwrappedValue.value;
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ls-stack/utils",
|
|
3
3
|
"description": "Typescript utils",
|
|
4
|
-
"version": "2.
|
|
4
|
+
"version": "2.14.0",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"files": [
|
|
7
7
|
"dist"
|
|
@@ -322,13 +322,13 @@
|
|
|
322
322
|
}
|
|
323
323
|
},
|
|
324
324
|
"scripts": {
|
|
325
|
-
"test": "vitest --ui",
|
|
326
|
-
"test
|
|
325
|
+
"test:ui": "vitest --ui",
|
|
326
|
+
"test": "vitest run",
|
|
327
327
|
"lint": "pnpm tsc && pnpm eslint",
|
|
328
328
|
"tsc": "tsc -p tsconfig.prod.json",
|
|
329
329
|
"tsc:watch": "tsc -p tsconfig.prod.json --watch",
|
|
330
330
|
"eslint": "CI=true eslint src/ scripts/ --color --max-warnings=0",
|
|
331
|
-
"build": "pnpm test
|
|
331
|
+
"build": "pnpm test && pnpm lint && pnpm build:no-test && pnpm build:update-exports",
|
|
332
332
|
"build:no-test": "tsup",
|
|
333
333
|
"build:update-exports": "tsm --no-warnings scripts/updatePackageExports.ts",
|
|
334
334
|
"build-test": "tsup --config tsup.test.config.ts",
|