@nxtedition/lib 23.4.4 → 23.5.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 (2) hide show
  1. package/cache.js +34 -24
  2. package/package.json +1 -1
package/cache.js CHANGED
@@ -18,7 +18,7 @@ export class AsyncCache {
18
18
  throw new TypeError('location must be undefined or a string')
19
19
  }
20
20
 
21
- if (typeof valueSelector === 'function') {
21
+ if (typeof valueSelector === 'function' || valueSelector === undefined) {
22
22
  this.#valueSelector = valueSelector
23
23
  } else {
24
24
  throw new TypeError('valueSelector must be a function')
@@ -74,31 +74,41 @@ export class AsyncCache {
74
74
  }
75
75
  }
76
76
 
77
- let promise = this.#dedupe.get(key)
78
- if (!promise) {
79
- promise = this.#valueSelector(...args).then(
80
- (value) => {
81
- this.#dedupe.delete(key)
82
-
83
- cached = {
84
- expire: Date.now() + this.#ttl(value, key),
85
- value,
86
- }
87
-
88
- globalThis.localStorage.setItem(this.#prefix + key, JSON.stringify(cached))
89
- this.#lru.set(key, cached)
90
-
91
- return value
92
- },
93
- (err) => {
94
- this.#dedupe.delete(key)
95
-
96
- throw err
97
- },
98
- )
99
- this.#dedupe.set(key, promise)
77
+ let promise
78
+
79
+ if (this.#valueSelector) {
80
+ promise = this.#dedupe.get(key)
81
+ if (!promise) {
82
+ promise = this.#valueSelector(...args).then(
83
+ (value) => {
84
+ this.#dedupe.delete(key)
85
+ this.set(key, value)
86
+ return value
87
+ },
88
+ (err) => {
89
+ this.#dedupe.delete(key)
90
+
91
+ throw err
92
+ },
93
+ )
94
+ this.#dedupe.set(key, promise)
95
+ }
100
96
  }
101
97
 
102
98
  return cached ? cached.value : promise
103
99
  }
100
+
101
+ set(key, value) {
102
+ if (typeof key !== 'string' || key.length === 0) {
103
+ throw new TypeError('key must be a non-empty string')
104
+ }
105
+
106
+ const cached = {
107
+ expire: Date.now() + this.#ttl(value, key),
108
+ value,
109
+ }
110
+
111
+ globalThis.localStorage.setItem(this.#prefix + key, JSON.stringify(cached))
112
+ this.#lru.set(key, cached)
113
+ }
104
114
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nxtedition/lib",
3
- "version": "23.4.4",
3
+ "version": "23.5.0",
4
4
  "license": "MIT",
5
5
  "author": "Robert Nagy <robert.nagy@boffins.se>",
6
6
  "type": "module",