@nxtedition/lib 23.4.1 → 23.4.3

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 +11 -14
  2. package/package.json +1 -1
package/cache.js CHANGED
@@ -1,4 +1,4 @@
1
- import { LRU } from 'lru-cache'
1
+ import { LRUCache } from 'lru-cache'
2
2
 
3
3
  export class AsyncCache {
4
4
  #lru
@@ -9,9 +9,14 @@ export class AsyncCache {
9
9
  #ttl
10
10
  #stale
11
11
 
12
- constructor(valueSelector, keySelector, opts) {
13
- this.#lru = new LRU({ max: 1024 })
14
- this.#valueSelector = valueSelector
12
+ constructor(location, valueSelector, keySelector, opts) {
13
+ this.#lru = new LRUCache({ max: 1024 })
14
+
15
+ if (typeof location === 'string') {
16
+ this.#prefix = location + ':'
17
+ } else {
18
+ throw new TypeError('location must be undefined or a string')
19
+ }
15
20
 
16
21
  if (typeof valueSelector === 'function') {
17
22
  this.#valueSelector = valueSelector
@@ -25,16 +30,8 @@ export class AsyncCache {
25
30
  throw new TypeError('keySelector must be a function')
26
31
  }
27
32
 
28
- if (opts?.location === undefined) {
29
- this.#prefix = (opts?.location ?? Math.random().toString(36)) + ':'
30
- } else if (typeof opts?.location === 'string') {
31
- this.#prefix = opts.location
32
- } else {
33
- throw new TypeError('location must be undefined or a string')
34
- }
35
-
36
33
  if (typeof opts?.ttl === 'number' || opts?.ttl === undefined) {
37
- const ttl = opts.ttl
34
+ const ttl = opts.ttl ?? Number.MAX_SAFE_INTEGER
38
35
  this.#ttl = (val, key) => ttl
39
36
  } else if (typeof opts?.ttl === 'function') {
40
37
  this.#ttl = opts.ttl
@@ -43,7 +40,7 @@ export class AsyncCache {
43
40
  }
44
41
 
45
42
  if (typeof opts?.stale === 'number' || opts?.stale === undefined) {
46
- const stale = opts.stale
43
+ const stale = opts.stale ?? Number.MAX_SAFE_INTEGER
47
44
  this.#stale = () => stale
48
45
  } else if (typeof opts?.stale === 'function') {
49
46
  this.#stale = opts.stale
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nxtedition/lib",
3
- "version": "23.4.1",
3
+ "version": "23.4.3",
4
4
  "license": "MIT",
5
5
  "author": "Robert Nagy <robert.nagy@boffins.se>",
6
6
  "type": "module",