@webreflection/utils 0.2.20 → 0.2.22

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@webreflection/utils",
3
- "version": "0.2.20",
3
+ "version": "0.2.22",
4
4
  "type": "module",
5
5
  "types": {
6
6
  "./all": "./types/all.d.ts",
package/src/cache.js CHANGED
@@ -1,22 +1,18 @@
1
1
  // @ts-check
2
2
 
3
- /**
4
- * @template K,V
5
- * @type {(self: Cache<K,V>) => void}
6
- */
7
- let cleanup;
8
-
9
3
  /**
10
4
  * @template K,V
11
5
  * @extends Map<K,V>
12
6
  */
13
7
  export default class Cache extends Map {
14
- static {
15
- cleanup = self => {
16
- self.#run = true;
17
- self.#queue.splice(0).forEach(self.delete, self);
18
- };
19
- }
8
+ /**
9
+ * @template K,V
10
+ * @type {(self: Cache<K,V>) => void}
11
+ */
12
+ #cleanup = () => {
13
+ this.#run = true;
14
+ this.#queue.splice(0).forEach(this.delete, this);
15
+ };
20
16
 
21
17
  /** @type {number} */
22
18
  #delay;
@@ -36,11 +32,11 @@ export default class Cache extends Map {
36
32
  this.#run = false;
37
33
  if (this.#delay < 1) {
38
34
  // @ts-ignore
39
- queueMicrotask(() => cleanup(this));
35
+ queueMicrotask(this.#cleanup);
40
36
  }
41
37
  else {
42
38
  // @ts-ignore
43
- setTimeout(cleanup, this.#delay, this);
39
+ setTimeout(this.#cleanup, this.#delay);
44
40
  }
45
41
  }
46
42
  }
@@ -64,7 +60,8 @@ export default class Cache extends Map {
64
60
  * @returns
65
61
  */
66
62
  getOrInsert(key, value) {
67
- return this.has(key) ? this.get(key) : this.put(key, value);
63
+ const known = this.get(key);
64
+ return known === void 0 && !this.has(key) ? this.put(key, value) : known;
68
65
  }
69
66
 
70
67
  /**
@@ -73,7 +70,8 @@ export default class Cache extends Map {
73
70
  * @returns
74
71
  */
75
72
  getOrInsertComputed(key, callback) {
76
- return this.has(key) ? this.get(key) : this.put(key, callback(key));
73
+ const known = this.get(key);
74
+ return known === void 0 && !this.has(key) ? this.put(key, callback(key)) : known;
77
75
  }
78
76
 
79
77
  /**
@@ -97,7 +97,7 @@ export default class JSONStorage {
97
97
  */
98
98
  get(key) {
99
99
  const value = this.#storage.getItem(key);
100
- return typeof value === 'string' ? this.#parse(value) : void 0;
100
+ return value != null ? this.#parse(value) : void 0;
101
101
  }
102
102
 
103
103
  /**
@@ -135,7 +135,8 @@ export default class JSONStorage {
135
135
  * @returns {this}
136
136
  */
137
137
  set(key, value) {
138
- this.#storage.setItem(key, this.#stringify(value));
138
+ const str = this.#stringify(value);
139
+ if (str != null) this.#storage.setItem(key, str);
139
140
  return this;
140
141
  }
141
142
 
package/src/registry.js CHANGED
@@ -68,6 +68,11 @@ export default class Registry extends Map {
68
68
  for (const [key, value] of iterable ?? []) this.set(key, value);
69
69
  }
70
70
 
71
+ clear() {
72
+ if (this.#unique) fail('Unable to clear registry with unique keys');
73
+ super.clear();
74
+ }
75
+
71
76
  /**
72
77
  * Remove a key when unique-key protection is disabled.
73
78
  *
@@ -88,8 +93,8 @@ export default class Registry extends Map {
88
93
  */
89
94
  set(key, value) {
90
95
  if (!this.#key(key)) fail('Invalid key:', key);
91
- if (this.#unique && super.has(key)) fail('Duplicate key:', key);
92
96
  if (!this.#value(value)) fail('Invalid value:', value);
97
+ if (this.#unique && super.has(key)) fail('Duplicate key:', key);
93
98
  return super.set(key, value);
94
99
  }
95
100
  }