@nxtedition/lib 26.0.10 → 26.0.12

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/cache.js CHANGED
@@ -80,9 +80,9 @@ export class AsyncCache {
80
80
  this.#lru =
81
81
  opts?.lru === false || opts?.lru === null ? null : new LRUCache({ max: 4096, ...opts?.lru })
82
82
 
83
- for (let n = 0; true; n += 1) {
83
+ for (let n = 0; true; n++) {
84
84
  try {
85
- this.#db = new DatabaseSync(location, { timeout: 20, ...opts?.db })
85
+ this.#db ??= new DatabaseSync(location, { timeout: 20, ...opts?.db })
86
86
 
87
87
  this.#db.exec(`
88
88
  PRAGMA journal_mode = WAL;
@@ -104,14 +104,17 @@ export class AsyncCache {
104
104
  )
105
105
  this.#delQuery = this.#db.prepare(`DELETE FROM cache WHERE key = ?`)
106
106
  } catch (err) {
107
- this.#db?.close()
108
- this.#db = null
107
+ if (n >= 8) {
108
+ this.#db?.close()
109
+ this.#db = null
109
110
 
110
- this.#getQuery = null
111
- this.#setQuery = null
112
- this.#delQuery = null
111
+ this.#getQuery = null
112
+ this.#setQuery = null
113
+ this.#delQuery = null
113
114
 
114
- process.emitWarning(err)
115
+ process.emitWarning(err)
116
+ break
117
+ }
115
118
  }
116
119
  }
117
120
  }
@@ -125,6 +128,23 @@ export class AsyncCache {
125
128
  * @returns {{ value: V|Promise<V>, async: boolean }}
126
129
  */
127
130
  get(...args) {
131
+ return this.#load(args, true)
132
+ }
133
+
134
+ /**
135
+ * @param {...any} args
136
+ * @returns {{ value: V|Promise<V>, async: boolean }}
137
+ */
138
+ peek(...args) {
139
+ return this.#load(args, false)
140
+ }
141
+
142
+ /**
143
+ * @param {any[]} args
144
+ * @param {boolean} refresh
145
+ * @returns {{ value: V|Promise<V>, async: boolean }}
146
+ */
147
+ #load(args, refresh) {
128
148
  const key = this.#keySelector(...args)
129
149
 
130
150
  if (typeof key !== 'string' || key.length === 0) {
@@ -170,7 +190,7 @@ export class AsyncCache {
170
190
  }
171
191
 
172
192
  let promise
173
- if (this.#valueSelector) {
193
+ if (this.#valueSelector && refresh) {
174
194
  promise = this.#dedupe.get(key)
175
195
  if (promise === undefined) {
176
196
  promise = this.#valueSelector(...args).then(
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nxtedition/lib",
3
- "version": "26.0.10",
3
+ "version": "26.0.12",
4
4
  "license": "MIT",
5
5
  "author": "Robert Nagy <robert.nagy@boffins.se>",
6
6
  "type": "module",
@@ -71,12 +71,15 @@ class FetchEntry {
71
71
  })
72
72
  .catch((err) => {
73
73
  if (this.refresh) {
74
- this.error = Object.assign(err, { data: resource })
74
+ this.error = Object.assign(new Error('fetch failed'), {
75
+ data: { resource },
76
+ cause: err,
77
+ })
75
78
  this.refresh()
76
79
  }
77
80
  })
78
81
  } catch (err) {
79
- this.error = Object.assign(err, { data: resource })
82
+ this.error = Object.assign(new Error('fetch failed'), { data: { resource }, cause: err })
80
83
  this.refresh()
81
84
  }
82
85
  }