@nxtedition/lib 23.4.4 → 23.5.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.
Files changed (3) hide show
  1. package/app.js +4 -0
  2. package/cache.js +45 -31
  3. package/package.json +1 -1
package/app.js CHANGED
@@ -453,6 +453,10 @@ export function makeApp(appConfig, onTerminate) {
453
453
 
454
454
  globalThis.ds = ds
455
455
 
456
+ ds.rpc.provide('cache/drop', () => {
457
+ ds.event.emit('cache/drop')
458
+ })
459
+
456
460
  appDestroyers.unshift(() => ds.close())
457
461
  }
458
462
 
package/cache.js CHANGED
@@ -2,7 +2,6 @@ import { LRUCache } from 'lru-cache'
2
2
 
3
3
  export class AsyncCache {
4
4
  #lru
5
- #prefix
6
5
  #valueSelector
7
6
  #keySelector
8
7
  #dedupe = new Map()
@@ -13,12 +12,12 @@ export class AsyncCache {
13
12
  this.#lru = new LRUCache({ max: 1024 })
14
13
 
15
14
  if (typeof location === 'string') {
16
- this.#prefix = location + ':'
15
+ // Do nothing...
17
16
  } else {
18
17
  throw new TypeError('location must be undefined or a string')
19
18
  }
20
19
 
21
- if (typeof valueSelector === 'function') {
20
+ if (typeof valueSelector === 'function' || valueSelector === undefined) {
22
21
  this.#valueSelector = valueSelector
23
22
  } else {
24
23
  throw new TypeError('valueSelector must be a function')
@@ -41,7 +40,7 @@ export class AsyncCache {
41
40
 
42
41
  if (typeof opts?.stale === 'number' || opts?.stale === undefined) {
43
42
  const stale = opts?.stale ?? Number.MAX_SAFE_INTEGER
44
- this.#stale = () => stale
43
+ this.#stale = (val, key) => stale
45
44
  } else if (typeof opts?.stale === 'function') {
46
45
  this.#stale = opts.stale
47
46
  } else {
@@ -58,10 +57,10 @@ export class AsyncCache {
58
57
 
59
58
  let cached = this.#lru.get(key)
60
59
 
61
- if (!cached) {
62
- const raw = globalThis.localStorage.getItem(this.#prefix + key)
63
- cached = raw ? JSON.parse(raw) : null
64
- }
60
+ // if (!cached) {
61
+ // const raw = globalThis.localStorage.getItem(this.#prefix + key)
62
+ // cached = raw ? JSON.parse(raw) : null
63
+ // }
65
64
 
66
65
  if (cached) {
67
66
  if (Date.now() < cached.expire) {
@@ -74,31 +73,46 @@ export class AsyncCache {
74
73
  }
75
74
  }
76
75
 
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)
76
+ let promise
77
+
78
+ if (this.#valueSelector) {
79
+ promise = this.#dedupe.get(key)
80
+ if (!promise) {
81
+ promise = this.#valueSelector(...args).then(
82
+ (value) => {
83
+ this.#dedupe.delete(key)
84
+ this.set(key, value)
85
+ return value
86
+ },
87
+ (err) => {
88
+ this.#dedupe.delete(key)
89
+
90
+ throw err
91
+ },
92
+ )
93
+ this.#dedupe.set(key, promise)
94
+ }
100
95
  }
101
96
 
102
97
  return cached ? cached.value : promise
103
98
  }
99
+
100
+ set(key, value) {
101
+ if (typeof key !== 'string' || key.length === 0) {
102
+ throw new TypeError('key must be a non-empty string')
103
+ }
104
+
105
+ const cached = {
106
+ expire: Date.now() + this.#ttl(value, key),
107
+ value,
108
+ }
109
+
110
+ // globalThis.localStorage.setItem(this.#prefix + key, JSON.stringify(cached))
111
+
112
+ this.#lru.set(key, cached)
113
+ }
114
+
115
+ clear() {
116
+ // TODO (fix): Implement...
117
+ }
104
118
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nxtedition/lib",
3
- "version": "23.4.4",
3
+ "version": "23.5.1",
4
4
  "license": "MIT",
5
5
  "author": "Robert Nagy <robert.nagy@boffins.se>",
6
6
  "type": "module",