@naturalcycles/nodejs-lib 15.129.0 → 15.129.1

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.
@@ -1,4 +1,5 @@
1
1
  import type { AsyncMemoCache, MemoCache } from '@naturalcycles/js-lib/decorators';
2
+ import { MISS } from '@naturalcycles/js-lib/types';
2
3
  import { LRUCache } from 'lru-cache';
3
4
  export type LRUMemoCacheOptions<KEY, VALUE> = Partial<LRUCache.Options<KEY, VALUE, any>>;
4
5
  /**
@@ -23,7 +24,7 @@ export declare class LRUAsyncMemoCache<KEY = any, VALUE = any> implements AsyncM
23
24
  constructor(opt: LRUMemoCacheOptions<KEY, VALUE>);
24
25
  private lru;
25
26
  has(k: any): Promise<boolean>;
26
- get(k: any): Promise<any>;
27
+ get(k: any): Promise<VALUE | typeof MISS>;
27
28
  set(k: any, v: any): Promise<void>;
28
29
  clear(): Promise<void>;
29
30
  }
@@ -1,3 +1,4 @@
1
+ import { MISS } from '@naturalcycles/js-lib/types';
1
2
  import { LRUCache } from 'lru-cache';
2
3
  /**
3
4
  * @example
@@ -27,6 +28,8 @@ export class LRUMemoCache {
27
28
  this.lru.clear();
28
29
  }
29
30
  }
31
+ // lru-cache never stores `undefined` (`set(k, undefined)` deletes `k`), so it's stored as this sentinel
32
+ const UNDEFINED = Symbol('undefined');
30
33
  /**
31
34
  * Same as LRUMemoCache, but implements AsyncMemoCache interface, for when you need it.
32
35
  */
@@ -42,10 +45,13 @@ export class LRUAsyncMemoCache {
42
45
  return this.lru.has(k);
43
46
  }
44
47
  async get(k) {
45
- return this.lru.get(k);
48
+ const v = this.lru.get(k);
49
+ if (v === undefined)
50
+ return MISS;
51
+ return v === UNDEFINED ? undefined : v;
46
52
  }
47
53
  async set(k, v) {
48
- this.lru.set(k, v);
54
+ this.lru.set(k, v === undefined ? UNDEFINED : v);
49
55
  }
50
56
  async clear() {
51
57
  this.lru.clear();
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@naturalcycles/nodejs-lib",
3
3
  "type": "module",
4
- "version": "15.129.0",
4
+ "version": "15.129.1",
5
5
  "dependencies": {
6
6
  "@naturalcycles/js-lib": "^15",
7
7
  "@standard-schema/spec": "^1",
@@ -1,4 +1,5 @@
1
1
  import type { AsyncMemoCache, MemoCache } from '@naturalcycles/js-lib/decorators'
2
+ import { MISS } from '@naturalcycles/js-lib/types'
2
3
  import { LRUCache } from 'lru-cache'
3
4
 
4
5
  // Partial, to be able to provide default `max`
@@ -38,6 +39,9 @@ export class LRUMemoCache<KEY = any, VALUE = any> implements MemoCache<KEY, VALU
38
39
  }
39
40
  }
40
41
 
42
+ // lru-cache never stores `undefined` (`set(k, undefined)` deletes `k`), so it's stored as this sentinel
43
+ const UNDEFINED = Symbol('undefined')
44
+
41
45
  /**
42
46
  * Same as LRUMemoCache, but implements AsyncMemoCache interface, for when you need it.
43
47
  */
@@ -55,12 +59,14 @@ export class LRUAsyncMemoCache<KEY = any, VALUE = any> implements AsyncMemoCache
55
59
  return this.lru.has(k)
56
60
  }
57
61
 
58
- async get(k: any): Promise<any> {
59
- return this.lru.get(k)
62
+ async get(k: any): Promise<VALUE | typeof MISS> {
63
+ const v = this.lru.get(k)
64
+ if (v === undefined) return MISS
65
+ return v === UNDEFINED ? (undefined as VALUE) : v
60
66
  }
61
67
 
62
68
  async set(k: any, v: any): Promise<void> {
63
- this.lru.set(k, v)
69
+ this.lru.set(k, v === undefined ? UNDEFINED : v)
64
70
  }
65
71
 
66
72
  async clear(): Promise<void> {