@thi.ng/rdom 0.6.9 → 0.7.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/CHANGELOG.md +50 -32
- package/README.md +16 -7
- package/checks.d.ts +3 -0
- package/checks.js +2 -0
- package/compile.d.ts +1 -1
- package/compile.js +8 -6
- package/component.d.ts +1 -1
- package/component.js +2 -2
- package/dom.d.ts +2 -2
- package/dom.js +15 -7
- package/event.js +1 -1
- package/index.d.ts +15 -14
- package/index.js +15 -14
- package/klist.d.ts +2 -2
- package/klist.js +4 -4
- package/list.d.ts +2 -2
- package/list.js +3 -3
- package/object.d.ts +3 -3
- package/object.js +3 -3
- package/package.json +80 -29
- package/promise.d.ts +2 -2
- package/promise.js +1 -1
- package/replace.d.ts +45 -0
- package/replace.js +62 -0
- package/scheduler.d.ts +1 -1
- package/sub.d.ts +3 -2
- package/sub.js +9 -8
- package/switch.d.ts +3 -3
- package/switch.js +5 -5
- package/wrap.d.ts +1 -1
- package/wrap.js +2 -2
- package/lib/index.js +0 -753
- package/lib/index.js.map +0 -1
- package/lib/index.umd.js +0 -1
- package/lib/index.umd.js.map +0 -1
- package/utils.d.ts +0 -5
- package/utils.js +0 -3
package/lib/index.js
DELETED
|
@@ -1,753 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
-
|
|
5
|
-
var checks = require('@thi.ng/checks');
|
|
6
|
-
var api = require('@thi.ng/api');
|
|
7
|
-
var errors = require('@thi.ng/errors');
|
|
8
|
-
var hiccup = require('@thi.ng/hiccup');
|
|
9
|
-
var prefixes = require('@thi.ng/prefixes');
|
|
10
|
-
var paths = require('@thi.ng/paths');
|
|
11
|
-
var rstream = require('@thi.ng/rstream');
|
|
12
|
-
var strings = require('@thi.ng/strings');
|
|
13
|
-
|
|
14
|
-
const isSubscribable = (x) => checks.implementsFunction(x, "subscribe");
|
|
15
|
-
const isComponent = (x) => checks.implementsFunction(x, "mount");
|
|
16
|
-
|
|
17
|
-
const $tree = async (tree, parent, idx = -1) => checks.isArray(tree)
|
|
18
|
-
? $treeElem(tree, parent, idx)
|
|
19
|
-
: isComponent(tree)
|
|
20
|
-
? tree.mount(parent, idx)
|
|
21
|
-
: api.isDeref(tree)
|
|
22
|
-
? $tree(tree.deref(), parent)
|
|
23
|
-
: checks.isNotStringAndIterable(tree)
|
|
24
|
-
? $treeIter(tree, parent)
|
|
25
|
-
: tree != null
|
|
26
|
-
? $el("span", null, tree, parent, idx)
|
|
27
|
-
: null;
|
|
28
|
-
const $treeElem = (tree, parent, idx) => {
|
|
29
|
-
const tag = tree[0];
|
|
30
|
-
return checks.isString(tag)
|
|
31
|
-
? $treeTag(tree, parent, idx)
|
|
32
|
-
:
|
|
33
|
-
isComponent(tag)
|
|
34
|
-
? tag.mount(parent, idx, ...tree.slice(1))
|
|
35
|
-
:
|
|
36
|
-
checks.isFunction(tag)
|
|
37
|
-
? $tree(tag.apply(null, tree.slice(1)), parent)
|
|
38
|
-
:
|
|
39
|
-
errors.unsupported(`tag: ${tag}`);
|
|
40
|
-
};
|
|
41
|
-
const $treeTag = (tree, parent, idx) => {
|
|
42
|
-
const n = tree.length;
|
|
43
|
-
const { 0: tag, 1: attribs, 2: body } = tree;
|
|
44
|
-
if (n === 3 && (checks.isString(body) || checks.isNumber(body))) {
|
|
45
|
-
const tmp = /^\w+/.exec(tag);
|
|
46
|
-
if (tmp && hiccup.NO_SPANS[tmp[0]]) {
|
|
47
|
-
parent = $el(tag, attribs, body, parent, idx);
|
|
48
|
-
return parent;
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
parent = $el(tag, attribs, null, parent, idx);
|
|
52
|
-
for (let i = 2; i < n; i++) {
|
|
53
|
-
$tree(tree[i], parent);
|
|
54
|
-
}
|
|
55
|
-
return parent;
|
|
56
|
-
};
|
|
57
|
-
const $treeIter = (tree, parent) => {
|
|
58
|
-
for (let t of tree) {
|
|
59
|
-
$tree(t, parent);
|
|
60
|
-
}
|
|
61
|
-
return null;
|
|
62
|
-
};
|
|
63
|
-
const $el = (tag, attribs, body, parent, idx = -1) => {
|
|
64
|
-
const match = hiccup.RE_TAG.exec(tag);
|
|
65
|
-
if (match) {
|
|
66
|
-
attribs = hiccup.mergeEmmetAttribs(Object.assign({}, attribs), match[2], match[3]);
|
|
67
|
-
tag = match[1];
|
|
68
|
-
}
|
|
69
|
-
let el;
|
|
70
|
-
const qidx = tag.indexOf(":");
|
|
71
|
-
if (qidx < 0) {
|
|
72
|
-
el = hiccup.SVG_TAGS[tag]
|
|
73
|
-
? document.createElementNS(prefixes.XML_SVG, tag)
|
|
74
|
-
: document.createElement(tag);
|
|
75
|
-
}
|
|
76
|
-
else {
|
|
77
|
-
el = document.createElementNS(PREFIXES[tag.substr(0, qidx)], tag);
|
|
78
|
-
}
|
|
79
|
-
attribs && $attribs(el, attribs);
|
|
80
|
-
body != null && $text(el, body);
|
|
81
|
-
parent && $addChild(parent, el, idx);
|
|
82
|
-
return el;
|
|
83
|
-
};
|
|
84
|
-
const $addChild = (parent, child, idx = -1) => {
|
|
85
|
-
checks.isNumber(idx)
|
|
86
|
-
? idx < 0 || idx >= parent.children.length
|
|
87
|
-
? parent.appendChild(child)
|
|
88
|
-
: parent.insertBefore(child, parent.children[idx])
|
|
89
|
-
: parent.insertBefore(child, idx);
|
|
90
|
-
};
|
|
91
|
-
const $remove = (el) => el.remove();
|
|
92
|
-
const $moveTo = (newParent, el, idx = -1) => {
|
|
93
|
-
$remove(el);
|
|
94
|
-
$addChild(newParent, el, idx);
|
|
95
|
-
};
|
|
96
|
-
const $clear = (el) => ((el.innerHTML = ""), el);
|
|
97
|
-
const $text = (el, body) => {
|
|
98
|
-
el.innerText = String(api.deref(body));
|
|
99
|
-
};
|
|
100
|
-
const $html = (el, body) => {
|
|
101
|
-
el.innerHTML = String(api.deref(body));
|
|
102
|
-
};
|
|
103
|
-
const $attribs = (el, attribs) => {
|
|
104
|
-
for (let id in attribs) {
|
|
105
|
-
setAttrib(el, id, attribs[id], attribs);
|
|
106
|
-
}
|
|
107
|
-
};
|
|
108
|
-
const setAttrib = (el, id, val, attribs) => {
|
|
109
|
-
checks.implementsFunction(val, "deref") && (val = val.deref());
|
|
110
|
-
const isListener = id.startsWith("on");
|
|
111
|
-
if (isListener) {
|
|
112
|
-
if (checks.isString(val)) {
|
|
113
|
-
el.setAttribute(id, val);
|
|
114
|
-
}
|
|
115
|
-
else {
|
|
116
|
-
id = id.substr(2);
|
|
117
|
-
checks.isArray(val)
|
|
118
|
-
? el.addEventListener(id, val[0], val[1])
|
|
119
|
-
: el.addEventListener(id, val);
|
|
120
|
-
}
|
|
121
|
-
return;
|
|
122
|
-
}
|
|
123
|
-
checks.isFunction(val) && (val = val(attribs));
|
|
124
|
-
checks.isArray(val) && (val = val.join(hiccup.ATTRIB_JOIN_DELIMS[id] || " "));
|
|
125
|
-
switch (id) {
|
|
126
|
-
case "class":
|
|
127
|
-
el.className = checks.isString(val)
|
|
128
|
-
? val
|
|
129
|
-
: hiccup.mergeClasses(el.className, val);
|
|
130
|
-
break;
|
|
131
|
-
case "style":
|
|
132
|
-
$style(el, val);
|
|
133
|
-
break;
|
|
134
|
-
case "value":
|
|
135
|
-
updateValueAttrib(el, val);
|
|
136
|
-
break;
|
|
137
|
-
case "data":
|
|
138
|
-
updateDataAttribs(el, val);
|
|
139
|
-
break;
|
|
140
|
-
case "prefix":
|
|
141
|
-
el.setAttribute(id, checks.isString(val) ? val : hiccup.formatPrefixes(val));
|
|
142
|
-
break;
|
|
143
|
-
case "accessKey":
|
|
144
|
-
case "autocapitalize":
|
|
145
|
-
case "checked":
|
|
146
|
-
case "contentEditable":
|
|
147
|
-
case "dir":
|
|
148
|
-
case "draggable":
|
|
149
|
-
case "hidden":
|
|
150
|
-
case "id":
|
|
151
|
-
case "indeterminate":
|
|
152
|
-
case "lang":
|
|
153
|
-
case "scrollLeft":
|
|
154
|
-
case "scrollTop":
|
|
155
|
-
case "selectionEnd":
|
|
156
|
-
case "selectionStart":
|
|
157
|
-
case "slot":
|
|
158
|
-
case "spellcheck":
|
|
159
|
-
case "tabIndex":
|
|
160
|
-
case "title":
|
|
161
|
-
el[id] = val;
|
|
162
|
-
break;
|
|
163
|
-
default: {
|
|
164
|
-
const idx = id.indexOf(":");
|
|
165
|
-
if (idx < 0) {
|
|
166
|
-
val === false || val == null
|
|
167
|
-
? el.removeAttribute(id)
|
|
168
|
-
: el.setAttribute(id, val);
|
|
169
|
-
}
|
|
170
|
-
else {
|
|
171
|
-
const ns = PREFIXES[id.substr(0, idx)];
|
|
172
|
-
val === false || val == null
|
|
173
|
-
? el.removeAttributeNS(ns, id)
|
|
174
|
-
: el.setAttributeNS(ns, id, val);
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
}
|
|
178
|
-
};
|
|
179
|
-
const updateValueAttrib = (el, value) => {
|
|
180
|
-
let ev;
|
|
181
|
-
switch (el.type) {
|
|
182
|
-
case "text":
|
|
183
|
-
case "textarea":
|
|
184
|
-
case "password":
|
|
185
|
-
case "search":
|
|
186
|
-
case "number":
|
|
187
|
-
case "email":
|
|
188
|
-
case "url":
|
|
189
|
-
case "tel":
|
|
190
|
-
case "date":
|
|
191
|
-
case "datetime-local":
|
|
192
|
-
case "time":
|
|
193
|
-
case "week":
|
|
194
|
-
case "month":
|
|
195
|
-
if ((ev = el.value) !== undefined && checks.isString(value)) {
|
|
196
|
-
const off = value.length - (ev.length - (el.selectionStart || 0));
|
|
197
|
-
el.value = value;
|
|
198
|
-
el.selectionStart = el.selectionEnd = off;
|
|
199
|
-
break;
|
|
200
|
-
}
|
|
201
|
-
default:
|
|
202
|
-
el.value = value;
|
|
203
|
-
}
|
|
204
|
-
};
|
|
205
|
-
const updateDataAttribs = (el, attribs) => {
|
|
206
|
-
const data = el.dataset;
|
|
207
|
-
for (let id in attribs) {
|
|
208
|
-
const v = api.deref(attribs[id]);
|
|
209
|
-
data[id] = checks.isFunction(v) ? v(attribs) : v;
|
|
210
|
-
}
|
|
211
|
-
};
|
|
212
|
-
const $style = (el, rules) => {
|
|
213
|
-
if (checks.isString(rules)) {
|
|
214
|
-
el.setAttribute("style", rules);
|
|
215
|
-
}
|
|
216
|
-
else {
|
|
217
|
-
const style = el.style;
|
|
218
|
-
for (let id in rules) {
|
|
219
|
-
let v = api.deref(rules[id]);
|
|
220
|
-
checks.isFunction(v) && (v = v(rules));
|
|
221
|
-
style[id] = v != null ? v : "";
|
|
222
|
-
}
|
|
223
|
-
}
|
|
224
|
-
};
|
|
225
|
-
const PREFIXES = {
|
|
226
|
-
svg: prefixes.XML_SVG,
|
|
227
|
-
xlink: prefixes.XML_XLINK,
|
|
228
|
-
xmlns: prefixes.XML_XMLNS,
|
|
229
|
-
};
|
|
230
|
-
const registerPrefix = (prefix, url) => {
|
|
231
|
-
api.assert(!PREFIXES[prefix], `${prefix} already registered: ${PREFIXES[prefix]}`);
|
|
232
|
-
PREFIXES[prefix] = url;
|
|
233
|
-
};
|
|
234
|
-
|
|
235
|
-
class RAFScheduler {
|
|
236
|
-
constructor() {
|
|
237
|
-
this.tasks = new Map();
|
|
238
|
-
this.raf = -1;
|
|
239
|
-
}
|
|
240
|
-
add(scope, fn) {
|
|
241
|
-
const tasks = this.tasks.get(scope);
|
|
242
|
-
tasks ? tasks.push(fn) : this.tasks.set(scope, [fn]);
|
|
243
|
-
this.raf < 0 &&
|
|
244
|
-
(this.raf = requestAnimationFrame(this.update.bind(this)));
|
|
245
|
-
}
|
|
246
|
-
cancel(scope) {
|
|
247
|
-
this.tasks.delete(scope);
|
|
248
|
-
}
|
|
249
|
-
update() {
|
|
250
|
-
for (let tasks of this.tasks.values()) {
|
|
251
|
-
for (let i = tasks.length; --i >= 0; tasks[i]())
|
|
252
|
-
;
|
|
253
|
-
}
|
|
254
|
-
this.tasks.clear();
|
|
255
|
-
this.raf = -1;
|
|
256
|
-
}
|
|
257
|
-
}
|
|
258
|
-
class NullScheduler {
|
|
259
|
-
add(_, fn) {
|
|
260
|
-
fn();
|
|
261
|
-
}
|
|
262
|
-
cancel() { }
|
|
263
|
-
}
|
|
264
|
-
exports.SCHEDULER = new NullScheduler();
|
|
265
|
-
const setScheduler = (s) => (exports.SCHEDULER = s);
|
|
266
|
-
|
|
267
|
-
const wrapper = (update) => (tag, attribs, body) => ({
|
|
268
|
-
el: undefined,
|
|
269
|
-
async mount(parent, index, state) {
|
|
270
|
-
this.el = $el(tag, attribs, null, parent, index);
|
|
271
|
-
update(this.el, state != null ? state : body);
|
|
272
|
-
return this.el;
|
|
273
|
-
},
|
|
274
|
-
async unmount() {
|
|
275
|
-
$remove(this.el);
|
|
276
|
-
this.el = undefined;
|
|
277
|
-
},
|
|
278
|
-
update(body) {
|
|
279
|
-
exports.SCHEDULER.add(this, () => this.el && update(this.el, body));
|
|
280
|
-
},
|
|
281
|
-
});
|
|
282
|
-
const $wrapText = wrapper($text);
|
|
283
|
-
const $wrapHtml = wrapper($html);
|
|
284
|
-
|
|
285
|
-
function $sub(src, tag, attribs) {
|
|
286
|
-
return (src.subscribe(new $Sub(checks.isString(tag) ? $wrapText(tag, attribs) : tag)));
|
|
287
|
-
}
|
|
288
|
-
class $Sub extends rstream.Subscription {
|
|
289
|
-
constructor(inner) {
|
|
290
|
-
super(undefined, { id: `rdom$sub-${rstream.nextID()}` });
|
|
291
|
-
this.inner = inner;
|
|
292
|
-
}
|
|
293
|
-
async mount(parent, index = -1) {
|
|
294
|
-
return (this.el = await this.inner.mount(parent, index, this.parent.deref()));
|
|
295
|
-
}
|
|
296
|
-
async unmount() {
|
|
297
|
-
this.unsubscribe();
|
|
298
|
-
exports.SCHEDULER.cancel(this);
|
|
299
|
-
this.el = undefined;
|
|
300
|
-
await this.inner.unmount();
|
|
301
|
-
}
|
|
302
|
-
update(x) {
|
|
303
|
-
this.next(x);
|
|
304
|
-
}
|
|
305
|
-
next(x) {
|
|
306
|
-
exports.SCHEDULER.add(this, () => this.el && this.inner.update(x));
|
|
307
|
-
}
|
|
308
|
-
}
|
|
309
|
-
class $SubA extends rstream.Subscription {
|
|
310
|
-
constructor(comp, path) {
|
|
311
|
-
super(undefined, { id: `rdom$sub-${rstream.nextID()}` });
|
|
312
|
-
this.comp = comp;
|
|
313
|
-
this.attr = {};
|
|
314
|
-
this.setter = paths.defSetterUnsafe(path);
|
|
315
|
-
}
|
|
316
|
-
next(a) {
|
|
317
|
-
const $ = this.comp;
|
|
318
|
-
exports.SCHEDULER.add($, () => $.el && $attribs($.el, this.setter(this.attr, a)));
|
|
319
|
-
}
|
|
320
|
-
}
|
|
321
|
-
|
|
322
|
-
const $compile = (tree) => checks.isArray(tree)
|
|
323
|
-
? isComplexComponent(tree)
|
|
324
|
-
? complexComponent(tree)
|
|
325
|
-
: basicComponent(tree)
|
|
326
|
-
: isComponent(tree)
|
|
327
|
-
? tree
|
|
328
|
-
: isSubscribable(tree)
|
|
329
|
-
? $sub(tree, "span")
|
|
330
|
-
: $wrapText("span", null, tree);
|
|
331
|
-
const walk = (f, x, path = []) => {
|
|
332
|
-
if (checks.isPlainObject(x)) {
|
|
333
|
-
for (const k in x) {
|
|
334
|
-
walk(f, x[k], [...path, k]);
|
|
335
|
-
}
|
|
336
|
-
}
|
|
337
|
-
f(x, path);
|
|
338
|
-
};
|
|
339
|
-
const isComplexComponent = (x) => {
|
|
340
|
-
if (checks.isPlainObject(x)) {
|
|
341
|
-
for (const k in x) {
|
|
342
|
-
if (isComplexComponent(x[k]))
|
|
343
|
-
return true;
|
|
344
|
-
}
|
|
345
|
-
}
|
|
346
|
-
else if (checks.isArray(x)) {
|
|
347
|
-
for (let i = 0, n = x.length; i < n; i++) {
|
|
348
|
-
if (isComplexComponent(x[i]))
|
|
349
|
-
return true;
|
|
350
|
-
}
|
|
351
|
-
}
|
|
352
|
-
return isSubscribable(x) || isComponent(x);
|
|
353
|
-
};
|
|
354
|
-
const complexComponent = (tree) => ({
|
|
355
|
-
async mount(parent, index = -1) {
|
|
356
|
-
this.subs = [];
|
|
357
|
-
walk((x, path) => {
|
|
358
|
-
isSubscribable(x) &&
|
|
359
|
-
this.subs.push(x.subscribe(new $SubA(this, path)));
|
|
360
|
-
}, tree[1]);
|
|
361
|
-
this.children = [];
|
|
362
|
-
this.el = $el(tree[0], tree[1], null, parent, index);
|
|
363
|
-
for (let i = 2; i < tree.length; i++) {
|
|
364
|
-
const child = $compile(tree[i]);
|
|
365
|
-
child.mount(this.el, i - 2);
|
|
366
|
-
this.children.push(child);
|
|
367
|
-
}
|
|
368
|
-
return this.el;
|
|
369
|
-
},
|
|
370
|
-
async unmount() {
|
|
371
|
-
exports.SCHEDULER.cancel(this);
|
|
372
|
-
if (this.children) {
|
|
373
|
-
for (let c of this.children) {
|
|
374
|
-
await c.unmount();
|
|
375
|
-
}
|
|
376
|
-
}
|
|
377
|
-
this.subs && this.subs.forEach((s) => s.unsubscribe());
|
|
378
|
-
$remove(this.el);
|
|
379
|
-
this.children = undefined;
|
|
380
|
-
this.subs = undefined;
|
|
381
|
-
this.el = undefined;
|
|
382
|
-
},
|
|
383
|
-
update() { },
|
|
384
|
-
});
|
|
385
|
-
const basicComponent = (tree) => ({
|
|
386
|
-
async mount(parent, index = -1) {
|
|
387
|
-
return (this.el = await $tree(tree, parent, index));
|
|
388
|
-
},
|
|
389
|
-
async unmount() {
|
|
390
|
-
$remove(this.el);
|
|
391
|
-
this.el = undefined;
|
|
392
|
-
},
|
|
393
|
-
update() { },
|
|
394
|
-
});
|
|
395
|
-
|
|
396
|
-
class Component {
|
|
397
|
-
async unmount() {
|
|
398
|
-
this.$remove();
|
|
399
|
-
this.el = undefined;
|
|
400
|
-
}
|
|
401
|
-
update(state) { }
|
|
402
|
-
$el(tag, attribs, body, parent = this.el, idx) {
|
|
403
|
-
return $el(tag, attribs, body, parent, idx);
|
|
404
|
-
}
|
|
405
|
-
$clear(el = this.el) {
|
|
406
|
-
return $clear(el);
|
|
407
|
-
}
|
|
408
|
-
$compile(tree) {
|
|
409
|
-
return $compile(tree);
|
|
410
|
-
}
|
|
411
|
-
$tree(tree, root = this.el, index) {
|
|
412
|
-
return $tree(tree, root, index);
|
|
413
|
-
}
|
|
414
|
-
$text(body) {
|
|
415
|
-
this.el && $text(this.el, body);
|
|
416
|
-
}
|
|
417
|
-
$html(body) {
|
|
418
|
-
this.el && $html(this.el, body);
|
|
419
|
-
}
|
|
420
|
-
$attribs(attribs, el = this.el) {
|
|
421
|
-
$attribs(el, attribs);
|
|
422
|
-
}
|
|
423
|
-
$style(rules, el = this.el) {
|
|
424
|
-
$style(el, rules);
|
|
425
|
-
}
|
|
426
|
-
$remove(el = this.el) {
|
|
427
|
-
$remove(el);
|
|
428
|
-
}
|
|
429
|
-
$moveTo(newParent, el = this.el, idx) {
|
|
430
|
-
$moveTo(newParent, el, idx);
|
|
431
|
-
}
|
|
432
|
-
}
|
|
433
|
-
|
|
434
|
-
const $input = (stream) => (e) => stream.next(e.target.value);
|
|
435
|
-
const $inputNum = (stream, fallback = 0) => (e) => stream.next(strings.maybeParseFloat(e.target.value, fallback));
|
|
436
|
-
const $inputCheckbox = (stream) => (e) => stream.next(e.target.checked);
|
|
437
|
-
const $inputTrigger = (stream) => () => stream.next(true);
|
|
438
|
-
const $inputFile = (stream) => (e) => stream.next(e.target.files[0]);
|
|
439
|
-
const $inputFiles = (stream) => (e) => stream.next(e.target.files);
|
|
440
|
-
|
|
441
|
-
const $klist = (src, tag, attribs, childCtor, keyFn) => $sub(src, new KList(tag, attribs, childCtor, keyFn));
|
|
442
|
-
class KList extends Component {
|
|
443
|
-
constructor(tag, attribs, ctor, keyFn = (_, i) => i) {
|
|
444
|
-
super();
|
|
445
|
-
this.tag = tag;
|
|
446
|
-
this.attribs = attribs;
|
|
447
|
-
this.ctor = ctor;
|
|
448
|
-
this.keyFn = keyFn;
|
|
449
|
-
this.items = [];
|
|
450
|
-
}
|
|
451
|
-
async mount(parent, index, state) {
|
|
452
|
-
this.items = [];
|
|
453
|
-
this.cache = new Map();
|
|
454
|
-
this.el = this.$el(this.tag, this.attribs, null, parent, index);
|
|
455
|
-
this.update(state);
|
|
456
|
-
return this.el;
|
|
457
|
-
}
|
|
458
|
-
async unmount() {
|
|
459
|
-
this.items.forEach((c) => c.v.unmount());
|
|
460
|
-
this.$remove();
|
|
461
|
-
this.el = undefined;
|
|
462
|
-
this.items = undefined;
|
|
463
|
-
this.cache = undefined;
|
|
464
|
-
}
|
|
465
|
-
async update(curr) {
|
|
466
|
-
if (!curr)
|
|
467
|
-
return;
|
|
468
|
-
const { keyFn, items, ctor, cache, el: parent } = this;
|
|
469
|
-
const currItems = [];
|
|
470
|
-
const currCache = new Map();
|
|
471
|
-
const offsets = new Map();
|
|
472
|
-
const deltas = new Map();
|
|
473
|
-
let numPrev = items.length;
|
|
474
|
-
let numCurr = curr.length;
|
|
475
|
-
let i;
|
|
476
|
-
for (i = numPrev; --i >= 0;) {
|
|
477
|
-
offsets.set(items[i].k, i);
|
|
478
|
-
}
|
|
479
|
-
for (i = numCurr; --i >= 0;) {
|
|
480
|
-
const val = curr[i];
|
|
481
|
-
const key = keyFn(val, i);
|
|
482
|
-
let item = cache.get(key);
|
|
483
|
-
item
|
|
484
|
-
? item.v.update(val)
|
|
485
|
-
: (item = {
|
|
486
|
-
k: key,
|
|
487
|
-
v: $compile(ctor(val)),
|
|
488
|
-
});
|
|
489
|
-
currCache.set(key, (currItems[i] = item));
|
|
490
|
-
const off = offsets.get(key);
|
|
491
|
-
off != undefined && deltas.set(key, Math.abs(i - off));
|
|
492
|
-
}
|
|
493
|
-
const willMove = new Set();
|
|
494
|
-
const didMove = new Set();
|
|
495
|
-
let next;
|
|
496
|
-
const insert = async (item) => {
|
|
497
|
-
if (cache.has(item.k)) {
|
|
498
|
-
$moveTo(parent, item.v.el, next);
|
|
499
|
-
next = item.v.el;
|
|
500
|
-
}
|
|
501
|
-
else {
|
|
502
|
-
cache.set(item.k, item);
|
|
503
|
-
next = await item.v.mount(parent, next);
|
|
504
|
-
}
|
|
505
|
-
numCurr--;
|
|
506
|
-
};
|
|
507
|
-
while (numPrev && numCurr) {
|
|
508
|
-
const prevItem = items[numPrev - 1];
|
|
509
|
-
const prevKey = prevItem.k;
|
|
510
|
-
const currItem = currItems[numCurr - 1];
|
|
511
|
-
const currKey = currItem.k;
|
|
512
|
-
if (currItem === prevItem) {
|
|
513
|
-
next = currItem.v.el;
|
|
514
|
-
numPrev--;
|
|
515
|
-
numCurr--;
|
|
516
|
-
}
|
|
517
|
-
else if (!currCache.has(prevKey)) {
|
|
518
|
-
await prevItem.v.unmount();
|
|
519
|
-
cache.delete(prevKey);
|
|
520
|
-
numPrev--;
|
|
521
|
-
}
|
|
522
|
-
else if (!cache.has(currKey) || willMove.has(currKey)) {
|
|
523
|
-
await insert(currItem);
|
|
524
|
-
}
|
|
525
|
-
else if (didMove.has(prevKey)) {
|
|
526
|
-
numPrev--;
|
|
527
|
-
}
|
|
528
|
-
else if (deltas.get(currKey) > deltas.get(prevKey)) {
|
|
529
|
-
await insert(currItem);
|
|
530
|
-
didMove.add(currKey);
|
|
531
|
-
}
|
|
532
|
-
else {
|
|
533
|
-
willMove.add(prevKey);
|
|
534
|
-
numPrev--;
|
|
535
|
-
}
|
|
536
|
-
}
|
|
537
|
-
while (numPrev--) {
|
|
538
|
-
const item = items[numPrev];
|
|
539
|
-
if (!currCache.has(item.k)) {
|
|
540
|
-
await item.v.unmount();
|
|
541
|
-
cache.delete(item.k);
|
|
542
|
-
}
|
|
543
|
-
}
|
|
544
|
-
while (numCurr) {
|
|
545
|
-
await insert(currItems[numCurr - 1]);
|
|
546
|
-
}
|
|
547
|
-
this.items = currItems;
|
|
548
|
-
}
|
|
549
|
-
}
|
|
550
|
-
|
|
551
|
-
const $list = (src, tag, attribs, ctor, equiv) => $sub(src, new List(tag, attribs, ctor, equiv));
|
|
552
|
-
class List extends Component {
|
|
553
|
-
constructor(tag, attribs, ctor, equiv = (a, b) => a === b) {
|
|
554
|
-
super();
|
|
555
|
-
this.tag = tag;
|
|
556
|
-
this.attribs = attribs;
|
|
557
|
-
this.ctor = ctor;
|
|
558
|
-
this.equiv = equiv;
|
|
559
|
-
}
|
|
560
|
-
async mount(parent, index, state) {
|
|
561
|
-
this.prev = [];
|
|
562
|
-
this.items = [];
|
|
563
|
-
this.el = this.$el(this.tag, this.attribs, null, parent, index);
|
|
564
|
-
this.update(state);
|
|
565
|
-
return this.el;
|
|
566
|
-
}
|
|
567
|
-
async unmount() {
|
|
568
|
-
this.items.forEach((c) => c.unmount());
|
|
569
|
-
this.$remove();
|
|
570
|
-
this.el = undefined;
|
|
571
|
-
this.items = undefined;
|
|
572
|
-
this.prev = undefined;
|
|
573
|
-
}
|
|
574
|
-
async update(curr) {
|
|
575
|
-
if (!curr)
|
|
576
|
-
return;
|
|
577
|
-
const { ctor, equiv, items, prev, el: parent } = this;
|
|
578
|
-
const nb = curr.length;
|
|
579
|
-
let na = prev.length;
|
|
580
|
-
let n = Math.min(na, nb);
|
|
581
|
-
for (let i = 0; i < n; i++) {
|
|
582
|
-
if (!equiv(prev[i], curr[i])) {
|
|
583
|
-
await items[i].unmount();
|
|
584
|
-
const val = curr[i];
|
|
585
|
-
const child = $compile(ctor(val));
|
|
586
|
-
await child.mount(parent, i);
|
|
587
|
-
items[i] = child;
|
|
588
|
-
prev[i] = val;
|
|
589
|
-
}
|
|
590
|
-
}
|
|
591
|
-
if (na < nb) {
|
|
592
|
-
for (; n < nb; n++) {
|
|
593
|
-
const val = curr[n];
|
|
594
|
-
const child = $compile(ctor(val));
|
|
595
|
-
await child.mount(parent, -1);
|
|
596
|
-
items[n] = child;
|
|
597
|
-
prev[n] = val;
|
|
598
|
-
}
|
|
599
|
-
}
|
|
600
|
-
else {
|
|
601
|
-
while (--na >= nb) {
|
|
602
|
-
await items[na].unmount();
|
|
603
|
-
items.pop();
|
|
604
|
-
prev.pop();
|
|
605
|
-
}
|
|
606
|
-
}
|
|
607
|
-
}
|
|
608
|
-
}
|
|
609
|
-
|
|
610
|
-
const $object = (src, opts, inner) => new $Object(src, opts, inner);
|
|
611
|
-
const $subObject = (src, opts, inner) => $sub(src, $object(src.deref() || {}, opts, inner));
|
|
612
|
-
class $Object extends Component {
|
|
613
|
-
constructor(src, opts, ctor) {
|
|
614
|
-
super();
|
|
615
|
-
this.ctor = ctor;
|
|
616
|
-
this.obj = rstream.fromObject(src, opts);
|
|
617
|
-
}
|
|
618
|
-
async mount(parent, index = -1, state) {
|
|
619
|
-
state !== undefined && this.obj.next(state);
|
|
620
|
-
this.inner = this.$compile(await this.ctor(this.obj.streams));
|
|
621
|
-
this.el = await this.inner.mount(parent, index);
|
|
622
|
-
return this.el;
|
|
623
|
-
}
|
|
624
|
-
async unmount() {
|
|
625
|
-
this.obj.done();
|
|
626
|
-
await this.inner.unmount();
|
|
627
|
-
this.el = undefined;
|
|
628
|
-
this.inner = undefined;
|
|
629
|
-
}
|
|
630
|
-
update(state) {
|
|
631
|
-
this.obj.next(state);
|
|
632
|
-
}
|
|
633
|
-
}
|
|
634
|
-
|
|
635
|
-
const $promise = (prom, error) => new $Promise(prom, error);
|
|
636
|
-
class $Promise extends Component {
|
|
637
|
-
constructor(promise, error = (e) => e) {
|
|
638
|
-
super();
|
|
639
|
-
this.promise = promise;
|
|
640
|
-
this.error = error;
|
|
641
|
-
}
|
|
642
|
-
async mount(parent, index) {
|
|
643
|
-
try {
|
|
644
|
-
this.inner = this.$compile(await this.promise);
|
|
645
|
-
}
|
|
646
|
-
catch (e) {
|
|
647
|
-
this.inner = this.$compile(this.error(e));
|
|
648
|
-
}
|
|
649
|
-
return (this.el = await this.inner.mount(parent, index));
|
|
650
|
-
}
|
|
651
|
-
async unmount() {
|
|
652
|
-
await this.inner.unmount();
|
|
653
|
-
this.inner = undefined;
|
|
654
|
-
this.el = undefined;
|
|
655
|
-
}
|
|
656
|
-
}
|
|
657
|
-
|
|
658
|
-
const $switch = (src, keyFn, ctors, error, loader) => $sub(src, new Switch(keyFn, ctors, error, loader));
|
|
659
|
-
const $refresh = (src, ctor, error, loader) => $switch(src, () => 0, { 0: ctor }, error, loader);
|
|
660
|
-
class Switch extends Component {
|
|
661
|
-
constructor(keyFn, ctors, error = async (e) => $wrapText("span", {}, e), loader = async () => $wrapText("span", {
|
|
662
|
-
hidden: true,
|
|
663
|
-
})) {
|
|
664
|
-
super();
|
|
665
|
-
this.keyFn = keyFn;
|
|
666
|
-
this.ctors = ctors;
|
|
667
|
-
this.error = error;
|
|
668
|
-
this.loader = loader;
|
|
669
|
-
}
|
|
670
|
-
async mount(parent, index, val) {
|
|
671
|
-
this.parent = parent;
|
|
672
|
-
this.index = index;
|
|
673
|
-
await this.update(val);
|
|
674
|
-
return this.inner.el;
|
|
675
|
-
}
|
|
676
|
-
async unmount() {
|
|
677
|
-
this.inner && (await this.inner.unmount());
|
|
678
|
-
this.val = undefined;
|
|
679
|
-
this.parent = undefined;
|
|
680
|
-
this.inner = undefined;
|
|
681
|
-
}
|
|
682
|
-
async update(val) {
|
|
683
|
-
this.inner && (await this.inner.unmount());
|
|
684
|
-
this.inner = undefined;
|
|
685
|
-
if (val != null) {
|
|
686
|
-
this.val = val;
|
|
687
|
-
let loader;
|
|
688
|
-
if (this.loader) {
|
|
689
|
-
loader = $compile(await this.loader(val));
|
|
690
|
-
await loader.mount(this.parent, this.index);
|
|
691
|
-
}
|
|
692
|
-
try {
|
|
693
|
-
const key = this.keyFn(val);
|
|
694
|
-
const next = this.ctors[key];
|
|
695
|
-
api.assert(!!next, `missing component for key: ${key}`);
|
|
696
|
-
this.inner = $compile(await next(val));
|
|
697
|
-
loader && (await loader.unmount());
|
|
698
|
-
}
|
|
699
|
-
catch (e) {
|
|
700
|
-
if (this.error) {
|
|
701
|
-
this.inner = $compile(await this.error(e));
|
|
702
|
-
loader && (await loader.unmount());
|
|
703
|
-
}
|
|
704
|
-
}
|
|
705
|
-
}
|
|
706
|
-
else {
|
|
707
|
-
this.loader && (this.inner = $compile(await this.loader(val)));
|
|
708
|
-
}
|
|
709
|
-
this.inner && (await this.inner.mount(this.parent, this.index));
|
|
710
|
-
}
|
|
711
|
-
}
|
|
712
|
-
|
|
713
|
-
exports.$Object = $Object;
|
|
714
|
-
exports.$Promise = $Promise;
|
|
715
|
-
exports.$Sub = $Sub;
|
|
716
|
-
exports.$SubA = $SubA;
|
|
717
|
-
exports.$addChild = $addChild;
|
|
718
|
-
exports.$attribs = $attribs;
|
|
719
|
-
exports.$clear = $clear;
|
|
720
|
-
exports.$compile = $compile;
|
|
721
|
-
exports.$el = $el;
|
|
722
|
-
exports.$html = $html;
|
|
723
|
-
exports.$input = $input;
|
|
724
|
-
exports.$inputCheckbox = $inputCheckbox;
|
|
725
|
-
exports.$inputFile = $inputFile;
|
|
726
|
-
exports.$inputFiles = $inputFiles;
|
|
727
|
-
exports.$inputNum = $inputNum;
|
|
728
|
-
exports.$inputTrigger = $inputTrigger;
|
|
729
|
-
exports.$klist = $klist;
|
|
730
|
-
exports.$list = $list;
|
|
731
|
-
exports.$moveTo = $moveTo;
|
|
732
|
-
exports.$object = $object;
|
|
733
|
-
exports.$promise = $promise;
|
|
734
|
-
exports.$refresh = $refresh;
|
|
735
|
-
exports.$remove = $remove;
|
|
736
|
-
exports.$style = $style;
|
|
737
|
-
exports.$sub = $sub;
|
|
738
|
-
exports.$subObject = $subObject;
|
|
739
|
-
exports.$switch = $switch;
|
|
740
|
-
exports.$text = $text;
|
|
741
|
-
exports.$tree = $tree;
|
|
742
|
-
exports.$wrapHtml = $wrapHtml;
|
|
743
|
-
exports.$wrapText = $wrapText;
|
|
744
|
-
exports.Component = Component;
|
|
745
|
-
exports.KList = KList;
|
|
746
|
-
exports.List = List;
|
|
747
|
-
exports.NullScheduler = NullScheduler;
|
|
748
|
-
exports.RAFScheduler = RAFScheduler;
|
|
749
|
-
exports.Switch = Switch;
|
|
750
|
-
exports.isComponent = isComponent;
|
|
751
|
-
exports.isSubscribable = isSubscribable;
|
|
752
|
-
exports.registerPrefix = registerPrefix;
|
|
753
|
-
exports.setScheduler = setScheduler;
|