@nxtedition/lib 14.0.2 → 14.0.4
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/package.json +1 -1
- package/util/template/index.js +50 -98
- package/util/template/javascript.js +25 -13
package/package.json
CHANGED
package/util/template/index.js
CHANGED
|
@@ -12,21 +12,7 @@ module.exports = (options) => {
|
|
|
12
12
|
js: getJavascriptCompiler(options),
|
|
13
13
|
}
|
|
14
14
|
|
|
15
|
-
|
|
16
|
-
return resolveObjectTemplate(...args)
|
|
17
|
-
.pipe(rx.first())
|
|
18
|
-
.toPromise()
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
function onResolveObjectTemplate(obj, ...args) {
|
|
22
|
-
try {
|
|
23
|
-
return compileObjectTemplate(obj)(...args)
|
|
24
|
-
} catch (err) {
|
|
25
|
-
return rxjs.throwError(() => err)
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
const compileArrayTemplateLazy = (arr) => {
|
|
15
|
+
const compileArrayTemplate = (arr, args$) => {
|
|
30
16
|
if (!fp.isArray(arr)) {
|
|
31
17
|
throw new Error('invalid argument')
|
|
32
18
|
}
|
|
@@ -35,9 +21,9 @@ module.exports = (options) => {
|
|
|
35
21
|
let indices
|
|
36
22
|
|
|
37
23
|
for (let i = 0; i < arr.length; i++) {
|
|
38
|
-
const resolver =
|
|
24
|
+
const resolver = compileTemplate(arr[i], args$)
|
|
39
25
|
if (resolver) {
|
|
40
|
-
resolvers ??= [
|
|
26
|
+
resolvers ??= []
|
|
41
27
|
resolvers.push(resolver)
|
|
42
28
|
indices ??= []
|
|
43
29
|
indices.push(i)
|
|
@@ -45,24 +31,19 @@ module.exports = (options) => {
|
|
|
45
31
|
}
|
|
46
32
|
|
|
47
33
|
return resolvers
|
|
48
|
-
? (
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
)
|
|
34
|
+
? rxjs.combineLatest(resolvers).pipe(
|
|
35
|
+
rx.map((values) => {
|
|
36
|
+
const ret = [...arr]
|
|
37
|
+
for (let n = 0; n < values.length; n++) {
|
|
38
|
+
ret[indices[n]] = values[n]
|
|
39
|
+
}
|
|
40
|
+
return ret
|
|
41
|
+
})
|
|
42
|
+
)
|
|
58
43
|
: null
|
|
59
44
|
}
|
|
60
45
|
|
|
61
|
-
const
|
|
62
|
-
return compileArrayTemplateLazy(arr) ?? (() => rxjs.of(arr))
|
|
63
|
-
})
|
|
64
|
-
|
|
65
|
-
const compileObjectTemplateLazy = (obj) => {
|
|
46
|
+
const compileObjectTemplate = (obj, args$) => {
|
|
66
47
|
if (!fp.isPlainObject(obj)) {
|
|
67
48
|
throw new Error('invalid argument')
|
|
68
49
|
}
|
|
@@ -73,7 +54,7 @@ module.exports = (options) => {
|
|
|
73
54
|
const keys = Object.keys(obj)
|
|
74
55
|
|
|
75
56
|
for (let i = 0; i < keys.length; i++) {
|
|
76
|
-
const resolver =
|
|
57
|
+
const resolver = compileTemplate(obj[keys[i]], args$)
|
|
77
58
|
if (resolver) {
|
|
78
59
|
resolvers ??= []
|
|
79
60
|
resolvers.push(resolver)
|
|
@@ -83,24 +64,19 @@ module.exports = (options) => {
|
|
|
83
64
|
}
|
|
84
65
|
|
|
85
66
|
return resolvers
|
|
86
|
-
? (
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
)
|
|
67
|
+
? rxjs.combineLatest(resolvers).pipe(
|
|
68
|
+
rx.map((values) => {
|
|
69
|
+
const ret = { ...obj }
|
|
70
|
+
for (let n = 0; n < values.length; n++) {
|
|
71
|
+
ret[indices[n]] = values[n]
|
|
72
|
+
}
|
|
73
|
+
return ret
|
|
74
|
+
})
|
|
75
|
+
)
|
|
96
76
|
: null
|
|
97
77
|
}
|
|
98
78
|
|
|
99
|
-
const
|
|
100
|
-
return compileObjectTemplateLazy(obj) ?? (() => rxjs.of(obj))
|
|
101
|
-
})
|
|
102
|
-
|
|
103
|
-
function inner(str) {
|
|
79
|
+
const inner = weakCache(function inner(str) {
|
|
104
80
|
const templateStart = str.lastIndexOf('{{')
|
|
105
81
|
if (templateStart === -1) {
|
|
106
82
|
return null
|
|
@@ -127,9 +103,18 @@ module.exports = (options) => {
|
|
|
127
103
|
body: str.slice(bodyStart, bodyEnd),
|
|
128
104
|
post: str.slice(templateEnd),
|
|
129
105
|
}
|
|
130
|
-
}
|
|
106
|
+
})
|
|
107
|
+
|
|
108
|
+
function compileStringTemplate(str, args$) {
|
|
109
|
+
if (!fp.isString(str)) {
|
|
110
|
+
throw new Error('invalid argument')
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
const match = inner(str)
|
|
114
|
+
if (!match) {
|
|
115
|
+
return null
|
|
116
|
+
}
|
|
131
117
|
|
|
132
|
-
const _compileStringTemplate = weakCache(function _compileStringTemplate(str, match) {
|
|
133
118
|
const { pre, type, body, post } = match
|
|
134
119
|
|
|
135
120
|
const compileExpression = compilers[type]
|
|
@@ -140,28 +125,14 @@ module.exports = (options) => {
|
|
|
140
125
|
const expr = compileExpression(body)
|
|
141
126
|
|
|
142
127
|
if (!pre && !post) {
|
|
143
|
-
return expr
|
|
128
|
+
return expr(args$)
|
|
144
129
|
}
|
|
145
130
|
|
|
146
|
-
return (
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
compileStringTemplate(`${pre}${stringify(body, type !== 'js')}${post}`)(...args)
|
|
150
|
-
)
|
|
131
|
+
return expr(args$).pipe(
|
|
132
|
+
rx.switchMap((body) =>
|
|
133
|
+
compileStringTemplate(`${pre}${stringify(body, type !== 'js')}${post}`, args$)
|
|
151
134
|
)
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
function compileStringTemplateLazy(str) {
|
|
155
|
-
if (!fp.isString(str)) {
|
|
156
|
-
throw new Error('invalid argument')
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
const match = inner(str)
|
|
160
|
-
return match ? _compileStringTemplate(str, match) : null
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
function compileStringTemplate(str) {
|
|
164
|
-
return compileStringTemplateLazy(str) ?? (() => rxjs.of(str))
|
|
135
|
+
)
|
|
165
136
|
}
|
|
166
137
|
|
|
167
138
|
function stringify(value, escape) {
|
|
@@ -179,43 +150,29 @@ module.exports = (options) => {
|
|
|
179
150
|
return typeof val === 'string' && val.indexOf('{{') !== -1
|
|
180
151
|
}
|
|
181
152
|
|
|
182
|
-
function
|
|
153
|
+
function compileTemplate(template, args$) {
|
|
183
154
|
if (fp.isPlainObject(template)) {
|
|
184
|
-
return
|
|
155
|
+
return compileObjectTemplate(template, args$)
|
|
185
156
|
} else if (fp.isArray(template)) {
|
|
186
|
-
return
|
|
157
|
+
return compileArrayTemplate(template, args$)
|
|
187
158
|
} else if (fp.isString(template)) {
|
|
188
|
-
return
|
|
159
|
+
return compileStringTemplate(template, args$)
|
|
189
160
|
} else {
|
|
190
161
|
return null
|
|
191
162
|
}
|
|
192
163
|
}
|
|
193
164
|
|
|
194
|
-
function
|
|
195
|
-
|
|
196
|
-
return compileObjectTemplate(template)
|
|
197
|
-
} else if (fp.isArray(template)) {
|
|
198
|
-
return compileArrayTemplate(template)
|
|
199
|
-
} else if (fp.isString(template)) {
|
|
200
|
-
return compileStringTemplate(template)
|
|
201
|
-
} else {
|
|
202
|
-
return () => rxjs.of(template)
|
|
203
|
-
}
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
async function resolveTemplate(template, ...args) {
|
|
207
|
-
return onResolveTemplate(template, ...args)
|
|
208
|
-
.pipe(rx.first())
|
|
209
|
-
.toPromise()
|
|
165
|
+
async function resolveTemplate(template, args$) {
|
|
166
|
+
return rxjs.firstValueFrom(onResolveTemplate(template, args$))
|
|
210
167
|
}
|
|
211
168
|
|
|
212
|
-
function onResolveTemplate(
|
|
213
|
-
if (fp.isString(
|
|
214
|
-
return rxjs.of(
|
|
169
|
+
function onResolveTemplate(template, args$) {
|
|
170
|
+
if (fp.isString(template) && template.lastIndexOf('{{') === -1) {
|
|
171
|
+
return rxjs.of(template)
|
|
215
172
|
}
|
|
216
173
|
|
|
217
174
|
try {
|
|
218
|
-
return compileTemplate(
|
|
175
|
+
return compileTemplate(template, args$) ?? rxjs.of(template)
|
|
219
176
|
} catch (err) {
|
|
220
177
|
return rxjs.throwError(() => err)
|
|
221
178
|
}
|
|
@@ -225,11 +182,6 @@ module.exports = (options) => {
|
|
|
225
182
|
resolveTemplate,
|
|
226
183
|
onResolveTemplate,
|
|
227
184
|
compileTemplate,
|
|
228
|
-
|
|
229
|
-
// Deprecated
|
|
230
|
-
resolveObjectTemplate,
|
|
231
|
-
onResolveObjectTemplate,
|
|
232
|
-
compileObjectTemplate,
|
|
233
185
|
isTemplate,
|
|
234
186
|
}
|
|
235
187
|
}
|
|
@@ -19,6 +19,9 @@ class TimerEntry {
|
|
|
19
19
|
|
|
20
20
|
dispose() {
|
|
21
21
|
clearTimeout(this.timer)
|
|
22
|
+
|
|
23
|
+
this.refresh = null
|
|
24
|
+
this.timer = null
|
|
22
25
|
}
|
|
23
26
|
}
|
|
24
27
|
|
|
@@ -28,6 +31,7 @@ class FetchEntry {
|
|
|
28
31
|
this.counter = null
|
|
29
32
|
this.refresh = refresh
|
|
30
33
|
this.ac = new AbortController()
|
|
34
|
+
this.signal = this.ac.signal
|
|
31
35
|
this.body = null
|
|
32
36
|
this.status = null
|
|
33
37
|
this.options = options
|
|
@@ -38,22 +42,28 @@ class FetchEntry {
|
|
|
38
42
|
// TODO (fix): expire...
|
|
39
43
|
|
|
40
44
|
undici
|
|
41
|
-
.fetch(resource, { ...this.options, signal: this.
|
|
45
|
+
.fetch(resource, { ...this.options, signal: this.signal })
|
|
42
46
|
.then(async (res) => {
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
47
|
+
if (!this.signal.aborted) {
|
|
48
|
+
// TODO (fix): max size...
|
|
49
|
+
this.body = Buffer.from(await res.arrayBuffer())
|
|
50
|
+
this.status = res.status
|
|
51
|
+
this.headers = res.headers
|
|
52
|
+
this.refresh()
|
|
53
|
+
}
|
|
48
54
|
})
|
|
49
55
|
.catch((err) => {
|
|
50
|
-
this.
|
|
51
|
-
|
|
56
|
+
if (!this.signal.aborted) {
|
|
57
|
+
this.error = err
|
|
58
|
+
this.refresh()
|
|
59
|
+
}
|
|
52
60
|
})
|
|
53
61
|
}
|
|
54
62
|
|
|
55
63
|
dispose() {
|
|
56
64
|
this.ac.abort()
|
|
65
|
+
|
|
66
|
+
this.refresh = null
|
|
57
67
|
}
|
|
58
68
|
}
|
|
59
69
|
|
|
@@ -78,7 +88,9 @@ class RecordEntry {
|
|
|
78
88
|
} else {
|
|
79
89
|
this.record.off('update', this.refresh)
|
|
80
90
|
}
|
|
91
|
+
|
|
81
92
|
this.record = null
|
|
93
|
+
this.refresh = null
|
|
82
94
|
}
|
|
83
95
|
}
|
|
84
96
|
|
|
@@ -186,9 +198,8 @@ module.exports = ({ ds, ...options }) => {
|
|
|
186
198
|
} else {
|
|
187
199
|
this._args = this._handler ? proxyify(args, this) : args
|
|
188
200
|
this._ready = true
|
|
201
|
+
this._refreshNT(this)
|
|
189
202
|
}
|
|
190
|
-
|
|
191
|
-
this._refreshNT(this)
|
|
192
203
|
}
|
|
193
204
|
|
|
194
205
|
suspend() {
|
|
@@ -300,9 +311,10 @@ module.exports = ({ ds, ...options }) => {
|
|
|
300
311
|
}
|
|
301
312
|
}
|
|
302
313
|
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
314
|
+
// TODO (perf): Make this work.
|
|
315
|
+
// if (!self._entries) {
|
|
316
|
+
// self._args = null
|
|
317
|
+
// }
|
|
306
318
|
|
|
307
319
|
self._disposing = false
|
|
308
320
|
}
|