@ls-stack/utils 2.11.0 → 2.12.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 CHANGED
@@ -20,6 +20,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
20
20
  // src/cache.ts
21
21
  var cache_exports = {};
22
22
  __export(cache_exports, {
23
+ RejectValue: () => RejectValue,
23
24
  cachedGetter: () => cachedGetter,
24
25
  createCache: () => createCache
25
26
  });
@@ -43,6 +44,12 @@ function cachedGetter(getter) {
43
44
  }
44
45
  };
45
46
  }
47
+ var RejectValue = class {
48
+ value;
49
+ constructor(value) {
50
+ this.value = value;
51
+ }
52
+ };
46
53
  function createCache({
47
54
  maxCacheSize = 1e3,
48
55
  maxItemAge,
@@ -77,12 +84,18 @@ function createCache({
77
84
  function isExpired(timestamp, now) {
78
85
  return maxItemAge !== void 0 && now - timestamp > maxItemAge * 1e3;
79
86
  }
87
+ const utils = {
88
+ reject: (value) => new RejectValue(value)
89
+ };
80
90
  return {
81
91
  getOrInsert(cacheKey, val) {
82
92
  const now = Date.now();
83
93
  const entry = cache.get(cacheKey);
84
94
  if (!entry || isExpired(entry.timestamp, now)) {
85
- const value = val();
95
+ const value = val(utils);
96
+ if (value instanceof RejectValue) {
97
+ return value.value;
98
+ }
86
99
  cache.set(cacheKey, { value, timestamp: now });
87
100
  trimToSize();
88
101
  cleanExpiredItems();
@@ -104,7 +117,14 @@ function createCache({
104
117
  if (entry && !isExpired(entry.timestamp, now)) {
105
118
  return entry.value;
106
119
  }
107
- const promise = val().then((result) => {
120
+ const promise = val(utils).then((result) => {
121
+ if (result instanceof RejectValue) {
122
+ const cacheValue = cache.get(cacheKey);
123
+ if (cacheValue?.value === promise) {
124
+ cache.delete(cacheKey);
125
+ }
126
+ return result.value;
127
+ }
108
128
  cache.set(cacheKey, { value: result, timestamp: Date.now() });
109
129
  return result;
110
130
  }).catch((error) => {
@@ -160,6 +180,7 @@ function createCache({
160
180
  }
161
181
  // Annotate the CommonJS export names for ESM import in node:
162
182
  0 && (module.exports = {
183
+ RejectValue,
163
184
  cachedGetter,
164
185
  createCache
165
186
  });
package/dist/cache.d.cts CHANGED
@@ -18,9 +18,17 @@ type Options = {
18
18
  */
19
19
  expirationThrottle?: number;
20
20
  };
21
+ declare class RejectValue<T> {
22
+ value: T;
23
+ constructor(value: T);
24
+ }
21
25
  type Cache<T> = {
22
- getOrInsert: (cacheKey: string, val: () => T) => T;
23
- getOrInsertAsync: (cacheKey: string, val: () => Promise<T>) => Promise<T>;
26
+ getOrInsert: (cacheKey: string, val: (utils: {
27
+ reject: (value: T) => RejectValue<T>;
28
+ }) => T | RejectValue<T>) => T;
29
+ getOrInsertAsync: (cacheKey: string, val: (utils: {
30
+ reject: (value: T) => RejectValue<T>;
31
+ }) => Promise<T | RejectValue<T>>) => Promise<T>;
24
32
  clear: () => void;
25
33
  get: (cacheKey: string) => T | undefined;
26
34
  set: (cacheKey: string, value: T) => void;
@@ -36,4 +44,4 @@ type Cache<T> = {
36
44
  };
37
45
  declare function createCache<T>({ maxCacheSize, maxItemAge, expirationThrottle, }?: Options): Cache<T>;
38
46
 
39
- export { type Cache, cachedGetter, createCache };
47
+ export { type Cache, RejectValue, cachedGetter, createCache };
package/dist/cache.d.ts CHANGED
@@ -18,9 +18,17 @@ type Options = {
18
18
  */
19
19
  expirationThrottle?: number;
20
20
  };
21
+ declare class RejectValue<T> {
22
+ value: T;
23
+ constructor(value: T);
24
+ }
21
25
  type Cache<T> = {
22
- getOrInsert: (cacheKey: string, val: () => T) => T;
23
- getOrInsertAsync: (cacheKey: string, val: () => Promise<T>) => Promise<T>;
26
+ getOrInsert: (cacheKey: string, val: (utils: {
27
+ reject: (value: T) => RejectValue<T>;
28
+ }) => T | RejectValue<T>) => T;
29
+ getOrInsertAsync: (cacheKey: string, val: (utils: {
30
+ reject: (value: T) => RejectValue<T>;
31
+ }) => Promise<T | RejectValue<T>>) => Promise<T>;
24
32
  clear: () => void;
25
33
  get: (cacheKey: string) => T | undefined;
26
34
  set: (cacheKey: string, value: T) => void;
@@ -36,4 +44,4 @@ type Cache<T> = {
36
44
  };
37
45
  declare function createCache<T>({ maxCacheSize, maxItemAge, expirationThrottle, }?: Options): Cache<T>;
38
46
 
39
- export { type Cache, cachedGetter, createCache };
47
+ export { type Cache, RejectValue, cachedGetter, createCache };
package/dist/cache.js CHANGED
@@ -12,6 +12,12 @@ function cachedGetter(getter) {
12
12
  }
13
13
  };
14
14
  }
15
+ var RejectValue = class {
16
+ value;
17
+ constructor(value) {
18
+ this.value = value;
19
+ }
20
+ };
15
21
  function createCache({
16
22
  maxCacheSize = 1e3,
17
23
  maxItemAge,
@@ -46,12 +52,18 @@ function createCache({
46
52
  function isExpired(timestamp, now) {
47
53
  return maxItemAge !== void 0 && now - timestamp > maxItemAge * 1e3;
48
54
  }
55
+ const utils = {
56
+ reject: (value) => new RejectValue(value)
57
+ };
49
58
  return {
50
59
  getOrInsert(cacheKey, val) {
51
60
  const now = Date.now();
52
61
  const entry = cache.get(cacheKey);
53
62
  if (!entry || isExpired(entry.timestamp, now)) {
54
- const value = val();
63
+ const value = val(utils);
64
+ if (value instanceof RejectValue) {
65
+ return value.value;
66
+ }
55
67
  cache.set(cacheKey, { value, timestamp: now });
56
68
  trimToSize();
57
69
  cleanExpiredItems();
@@ -73,7 +85,14 @@ function createCache({
73
85
  if (entry && !isExpired(entry.timestamp, now)) {
74
86
  return entry.value;
75
87
  }
76
- const promise = val().then((result) => {
88
+ const promise = val(utils).then((result) => {
89
+ if (result instanceof RejectValue) {
90
+ const cacheValue = cache.get(cacheKey);
91
+ if (cacheValue?.value === promise) {
92
+ cache.delete(cacheKey);
93
+ }
94
+ return result.value;
95
+ }
77
96
  cache.set(cacheKey, { value: result, timestamp: Date.now() });
78
97
  return result;
79
98
  }).catch((error) => {
@@ -128,6 +147,7 @@ function createCache({
128
147
  };
129
148
  }
130
149
  export {
150
+ RejectValue,
131
151
  cachedGetter,
132
152
  createCache
133
153
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@ls-stack/utils",
3
3
  "description": "Typescript utils",
4
- "version": "2.11.0",
4
+ "version": "2.12.0",
5
5
  "license": "MIT",
6
6
  "files": [
7
7
  "dist"