@nxtedition/lib 14.0.3 → 14.0.5
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 +49 -98
- package/util/template/javascript.js +21 -10
package/package.json
CHANGED
package/util/template/index.js
CHANGED
|
@@ -3,7 +3,6 @@ const rxjs = require('rxjs')
|
|
|
3
3
|
const fp = require('lodash/fp')
|
|
4
4
|
const getNxtpressionsCompiler = require('./nextpressions')
|
|
5
5
|
const getJavascriptCompiler = require('./javascript')
|
|
6
|
-
const weakCache = require('../../weakCache')
|
|
7
6
|
const JSON5 = require('json5')
|
|
8
7
|
|
|
9
8
|
module.exports = (options) => {
|
|
@@ -12,21 +11,7 @@ module.exports = (options) => {
|
|
|
12
11
|
js: getJavascriptCompiler(options),
|
|
13
12
|
}
|
|
14
13
|
|
|
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) => {
|
|
14
|
+
const compileArrayTemplate = (arr, args$) => {
|
|
30
15
|
if (!fp.isArray(arr)) {
|
|
31
16
|
throw new Error('invalid argument')
|
|
32
17
|
}
|
|
@@ -35,9 +20,9 @@ module.exports = (options) => {
|
|
|
35
20
|
let indices
|
|
36
21
|
|
|
37
22
|
for (let i = 0; i < arr.length; i++) {
|
|
38
|
-
const resolver =
|
|
23
|
+
const resolver = compileTemplate(arr[i], args$)
|
|
39
24
|
if (resolver) {
|
|
40
|
-
resolvers ??= [
|
|
25
|
+
resolvers ??= []
|
|
41
26
|
resolvers.push(resolver)
|
|
42
27
|
indices ??= []
|
|
43
28
|
indices.push(i)
|
|
@@ -45,24 +30,19 @@ module.exports = (options) => {
|
|
|
45
30
|
}
|
|
46
31
|
|
|
47
32
|
return resolvers
|
|
48
|
-
? (
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
)
|
|
33
|
+
? rxjs.combineLatest(resolvers).pipe(
|
|
34
|
+
rx.map((values) => {
|
|
35
|
+
const ret = [...arr]
|
|
36
|
+
for (let n = 0; n < values.length; n++) {
|
|
37
|
+
ret[indices[n]] = values[n]
|
|
38
|
+
}
|
|
39
|
+
return ret
|
|
40
|
+
})
|
|
41
|
+
)
|
|
58
42
|
: null
|
|
59
43
|
}
|
|
60
44
|
|
|
61
|
-
const
|
|
62
|
-
return compileArrayTemplateLazy(arr) ?? (() => rxjs.of(arr))
|
|
63
|
-
})
|
|
64
|
-
|
|
65
|
-
const compileObjectTemplateLazy = (obj) => {
|
|
45
|
+
const compileObjectTemplate = (obj, args$) => {
|
|
66
46
|
if (!fp.isPlainObject(obj)) {
|
|
67
47
|
throw new Error('invalid argument')
|
|
68
48
|
}
|
|
@@ -73,7 +53,7 @@ module.exports = (options) => {
|
|
|
73
53
|
const keys = Object.keys(obj)
|
|
74
54
|
|
|
75
55
|
for (let i = 0; i < keys.length; i++) {
|
|
76
|
-
const resolver =
|
|
56
|
+
const resolver = compileTemplate(obj[keys[i]], args$)
|
|
77
57
|
if (resolver) {
|
|
78
58
|
resolvers ??= []
|
|
79
59
|
resolvers.push(resolver)
|
|
@@ -83,24 +63,19 @@ module.exports = (options) => {
|
|
|
83
63
|
}
|
|
84
64
|
|
|
85
65
|
return resolvers
|
|
86
|
-
? (
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
)
|
|
66
|
+
? rxjs.combineLatest(resolvers).pipe(
|
|
67
|
+
rx.map((values) => {
|
|
68
|
+
const ret = { ...obj }
|
|
69
|
+
for (let n = 0; n < values.length; n++) {
|
|
70
|
+
ret[indices[n]] = values[n]
|
|
71
|
+
}
|
|
72
|
+
return ret
|
|
73
|
+
})
|
|
74
|
+
)
|
|
96
75
|
: null
|
|
97
76
|
}
|
|
98
77
|
|
|
99
|
-
const
|
|
100
|
-
return compileObjectTemplateLazy(obj) ?? (() => rxjs.of(obj))
|
|
101
|
-
})
|
|
102
|
-
|
|
103
|
-
function inner(str) {
|
|
78
|
+
const inner = function inner(str) {
|
|
104
79
|
const templateStart = str.lastIndexOf('{{')
|
|
105
80
|
if (templateStart === -1) {
|
|
106
81
|
return null
|
|
@@ -129,7 +104,16 @@ module.exports = (options) => {
|
|
|
129
104
|
}
|
|
130
105
|
}
|
|
131
106
|
|
|
132
|
-
|
|
107
|
+
function compileStringTemplate(str, args$) {
|
|
108
|
+
if (!fp.isString(str)) {
|
|
109
|
+
throw new Error('invalid argument')
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
const match = inner(str)
|
|
113
|
+
if (!match) {
|
|
114
|
+
return null
|
|
115
|
+
}
|
|
116
|
+
|
|
133
117
|
const { pre, type, body, post } = match
|
|
134
118
|
|
|
135
119
|
const compileExpression = compilers[type]
|
|
@@ -140,28 +124,14 @@ module.exports = (options) => {
|
|
|
140
124
|
const expr = compileExpression(body)
|
|
141
125
|
|
|
142
126
|
if (!pre && !post) {
|
|
143
|
-
return expr
|
|
127
|
+
return expr(args$)
|
|
144
128
|
}
|
|
145
129
|
|
|
146
|
-
return (
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
compileStringTemplate(`${pre}${stringify(body, type !== 'js')}${post}`)(...args)
|
|
150
|
-
)
|
|
130
|
+
return expr(args$).pipe(
|
|
131
|
+
rx.switchMap((body) =>
|
|
132
|
+
compileStringTemplate(`${pre}${stringify(body, type !== 'js')}${post}`, args$)
|
|
151
133
|
)
|
|
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))
|
|
134
|
+
)
|
|
165
135
|
}
|
|
166
136
|
|
|
167
137
|
function stringify(value, escape) {
|
|
@@ -179,43 +149,29 @@ module.exports = (options) => {
|
|
|
179
149
|
return typeof val === 'string' && val.indexOf('{{') !== -1
|
|
180
150
|
}
|
|
181
151
|
|
|
182
|
-
function
|
|
152
|
+
function compileTemplate(template, args$) {
|
|
183
153
|
if (fp.isPlainObject(template)) {
|
|
184
|
-
return
|
|
154
|
+
return compileObjectTemplate(template, args$)
|
|
185
155
|
} else if (fp.isArray(template)) {
|
|
186
|
-
return
|
|
156
|
+
return compileArrayTemplate(template, args$)
|
|
187
157
|
} else if (fp.isString(template)) {
|
|
188
|
-
return
|
|
158
|
+
return compileStringTemplate(template, args$)
|
|
189
159
|
} else {
|
|
190
160
|
return null
|
|
191
161
|
}
|
|
192
162
|
}
|
|
193
163
|
|
|
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()
|
|
164
|
+
async function resolveTemplate(template, args$) {
|
|
165
|
+
return rxjs.firstValueFrom(onResolveTemplate(template, args$))
|
|
210
166
|
}
|
|
211
167
|
|
|
212
|
-
function onResolveTemplate(
|
|
213
|
-
if (fp.isString(
|
|
214
|
-
return rxjs.of(
|
|
168
|
+
function onResolveTemplate(template, args$) {
|
|
169
|
+
if (fp.isString(template) && template.lastIndexOf('{{') === -1) {
|
|
170
|
+
return rxjs.of(template)
|
|
215
171
|
}
|
|
216
172
|
|
|
217
173
|
try {
|
|
218
|
-
return compileTemplate(
|
|
174
|
+
return compileTemplate(template, args$) ?? rxjs.of(template)
|
|
219
175
|
} catch (err) {
|
|
220
176
|
return rxjs.throwError(() => err)
|
|
221
177
|
}
|
|
@@ -225,11 +181,6 @@ module.exports = (options) => {
|
|
|
225
181
|
resolveTemplate,
|
|
226
182
|
onResolveTemplate,
|
|
227
183
|
compileTemplate,
|
|
228
|
-
|
|
229
|
-
// Deprecated
|
|
230
|
-
resolveObjectTemplate,
|
|
231
|
-
onResolveObjectTemplate,
|
|
232
|
-
compileObjectTemplate,
|
|
233
184
|
isTemplate,
|
|
234
185
|
}
|
|
235
186
|
}
|
|
@@ -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() {
|