@nxtedition/lib 23.9.8 → 23.9.10

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 (2) hide show
  1. package/cache.js +24 -9
  2. package/package.json +1 -1
package/cache.js CHANGED
@@ -1,7 +1,5 @@
1
1
  import { LRUCache } from 'lru-cache'
2
2
 
3
- function noop() {}
4
-
5
3
  export class AsyncCache {
6
4
  #lru
7
5
  #valueSelector
@@ -79,22 +77,29 @@ export class AsyncCache {
79
77
  if (!promise) {
80
78
  promise = this.#valueSelector(...args).then(
81
79
  (value) => {
82
- this.#dedupe.delete(key)
83
80
  this.set(key, value)
84
- return value
81
+ return [null, value]
85
82
  },
86
83
  (err) => {
87
- this.#dedupe.delete(key)
88
-
89
- throw err
84
+ this.delete(key)
85
+ return [err, null]
90
86
  },
91
87
  )
92
- promise.catch(noop) // Prevent unhandled rejections
93
88
  this.#dedupe.set(key, promise)
94
89
  }
95
90
  }
96
91
 
97
- return cached ? cached.value : promise
92
+ if (cached) {
93
+ return cached.value
94
+ }
95
+
96
+ const [err, value] = await promise
97
+
98
+ if (err) {
99
+ throw err
100
+ }
101
+
102
+ return value
98
103
  }
99
104
 
100
105
  set(key, value) {
@@ -108,6 +113,16 @@ export class AsyncCache {
108
113
  }
109
114
 
110
115
  this.#lru.set(key, cached)
116
+ this.#dedupe.delete(key)
117
+ }
118
+
119
+ delete(key) {
120
+ if (typeof key !== 'string' || key.length === 0) {
121
+ throw new TypeError('key must be a non-empty string')
122
+ }
123
+
124
+ this.#lru.delete(key)
125
+ this.#dedupe.delete(key)
111
126
  }
112
127
 
113
128
  clear() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nxtedition/lib",
3
- "version": "23.9.8",
3
+ "version": "23.9.10",
4
4
  "license": "MIT",
5
5
  "author": "Robert Nagy <robert.nagy@boffins.se>",
6
6
  "type": "module",