@nxtedition/lib 23.4.0 → 23.4.2

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 +32 -11
  2. package/package.json +1 -1
package/cache.js CHANGED
@@ -2,43 +2,64 @@ import { LRU } from 'lru-cache'
2
2
 
3
3
  export class AsyncCache {
4
4
  #lru
5
- #keySelector
5
+ #prefix
6
6
  #valueSelector
7
+ #keySelector
7
8
  #dedupe = new Map()
8
9
  #ttl
9
10
  #stale
10
11
 
11
- constructor(valueSelector, opts) {
12
+ constructor(location, valueSelector, keySelector, opts) {
12
13
  this.#lru = new LRU({ max: 1024 })
13
- this.#keySelector = opts?.keySelector ?? ((...args) => args[0])
14
- this.#valueSelector = valueSelector
14
+
15
+ if (typeof location === 'string') {
16
+ this.#prefix = location + ':'
17
+ } else {
18
+ throw new TypeError('location must be undefined or a string')
19
+ }
20
+
21
+ if (typeof valueSelector === 'function') {
22
+ this.#valueSelector = valueSelector
23
+ } else {
24
+ throw new TypeError('valueSelector must be a function')
25
+ }
26
+
27
+ if (typeof keySelector === 'function' || keySelector === undefined) {
28
+ this.#keySelector = keySelector ?? ((...args) => JSON.stringify(args))
29
+ } else {
30
+ throw new TypeError('keySelector must be a function')
31
+ }
15
32
 
16
33
  if (typeof opts?.ttl === 'number' || opts?.ttl === undefined) {
17
- const ttl = opts.ttl
18
- return (val, key) => ttl
34
+ const ttl = opts.ttl ?? Number.MAX_SAFE_INTEGER
35
+ this.#ttl = (val, key) => ttl
19
36
  } else if (typeof opts?.ttl === 'function') {
20
37
  this.#ttl = opts.ttl
21
38
  } else {
22
- throw new TypeError('ttl must be a number or a function')
39
+ throw new TypeError('ttl must be a undefined, number or a function')
23
40
  }
24
41
 
25
42
  if (typeof opts?.stale === 'number' || opts?.stale === undefined) {
26
- const stale = opts.stale
43
+ const stale = opts.stale ?? Number.MAX_SAFE_INTEGER
27
44
  this.#stale = () => stale
28
45
  } else if (typeof opts?.stale === 'function') {
29
46
  this.#stale = opts.stale
30
47
  } else {
31
- throw new TypeError('stale must be a number or a function')
48
+ throw new TypeError('stale must be a undefined, number or a function')
32
49
  }
33
50
  }
34
51
 
35
52
  async get(...args) {
36
53
  const key = this.#keySelector(...args)
37
54
 
55
+ if (typeof key !== 'string' || key.length === 0) {
56
+ throw new TypeError('keySelector must return a non-empty string')
57
+ }
58
+
38
59
  let cached = this.#lru.get(key)
39
60
 
40
61
  if (!cached) {
41
- const raw = globalThis.localStorage.getItem(key)
62
+ const raw = globalThis.localStorage.getItem(this.#prefix + key)
42
63
  cached = raw ? JSON.parse(raw) : null
43
64
  }
44
65
 
@@ -64,7 +85,7 @@ export class AsyncCache {
64
85
  value,
65
86
  }
66
87
 
67
- globalThis.localStorage.setItem(key, JSON.stringify(cached))
88
+ globalThis.localStorage.setItem(this.#prefix + key, JSON.stringify(cached))
68
89
  this.#lru.set(key, cached)
69
90
 
70
91
  return value
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nxtedition/lib",
3
- "version": "23.4.0",
3
+ "version": "23.4.2",
4
4
  "license": "MIT",
5
5
  "author": "Robert Nagy <robert.nagy@boffins.se>",
6
6
  "type": "module",