@nxtedition/lib 26.0.11 → 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 +54 -32
- package/package.json +1 -1
- package/util/template/javascript.js +5 -2
package/cache.js
CHANGED
|
@@ -80,37 +80,42 @@ 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
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
83
|
+
for (let n = 0; true; n++) {
|
|
84
|
+
try {
|
|
85
|
+
this.#db ??= new DatabaseSync(location, { timeout: 20, ...opts?.db })
|
|
86
|
+
|
|
87
|
+
this.#db.exec(`
|
|
88
|
+
PRAGMA journal_mode = WAL;
|
|
89
|
+
PRAGMA synchronous = NORMAL;
|
|
90
|
+
PRAGMA temp_store = memory;
|
|
91
|
+
PRAGMA optimize;
|
|
92
|
+
|
|
93
|
+
CREATE TABLE IF NOT EXISTS cache (
|
|
94
|
+
key TEXT PRIMARY KEY NOT NULL,
|
|
95
|
+
val TEXT NOT NULL,
|
|
96
|
+
ttl INTEGER NOT NULL,
|
|
97
|
+
stale INTEGER NOT NULL
|
|
98
|
+
);
|
|
99
|
+
`)
|
|
100
|
+
|
|
101
|
+
this.#getQuery = this.#db.prepare(`SELECT val, ttl, stale FROM cache WHERE key = ?`)
|
|
102
|
+
this.#setQuery = this.#db.prepare(
|
|
103
|
+
`INSERT OR REPLACE INTO cache (key, val, ttl, stale) VALUES (?, ?, ?, ?)`,
|
|
104
|
+
)
|
|
105
|
+
this.#delQuery = this.#db.prepare(`DELETE FROM cache WHERE key = ?`)
|
|
106
|
+
} catch (err) {
|
|
107
|
+
if (n >= 8) {
|
|
108
|
+
this.#db?.close()
|
|
109
|
+
this.#db = null
|
|
110
|
+
|
|
111
|
+
this.#getQuery = null
|
|
112
|
+
this.#setQuery = null
|
|
113
|
+
this.#delQuery = null
|
|
114
|
+
|
|
115
|
+
process.emitWarning(err)
|
|
116
|
+
break
|
|
117
|
+
}
|
|
118
|
+
}
|
|
114
119
|
}
|
|
115
120
|
}
|
|
116
121
|
|
|
@@ -123,6 +128,23 @@ export class AsyncCache {
|
|
|
123
128
|
* @returns {{ value: V|Promise<V>, async: boolean }}
|
|
124
129
|
*/
|
|
125
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) {
|
|
126
148
|
const key = this.#keySelector(...args)
|
|
127
149
|
|
|
128
150
|
if (typeof key !== 'string' || key.length === 0) {
|
|
@@ -168,7 +190,7 @@ export class AsyncCache {
|
|
|
168
190
|
}
|
|
169
191
|
|
|
170
192
|
let promise
|
|
171
|
-
if (this.#valueSelector) {
|
|
193
|
+
if (this.#valueSelector && refresh) {
|
|
172
194
|
promise = this.#dedupe.get(key)
|
|
173
195
|
if (promise === undefined) {
|
|
174
196
|
promise = this.#valueSelector(...args).then(
|
package/package.json
CHANGED
|
@@ -71,12 +71,15 @@ class FetchEntry {
|
|
|
71
71
|
})
|
|
72
72
|
.catch((err) => {
|
|
73
73
|
if (this.refresh) {
|
|
74
|
-
this.error = Object.assign(
|
|
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(
|
|
82
|
+
this.error = Object.assign(new Error('fetch failed'), { data: { resource }, cause: err })
|
|
80
83
|
this.refresh()
|
|
81
84
|
}
|
|
82
85
|
}
|