@b9g/crank 0.5.0-beta.5 → 0.5.0-beta.7

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/crank.cjs CHANGED
@@ -2,20 +2,1960 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- var core = require('./core.cjs');
6
- var tags = require('./tags.cjs');
5
+ const NOOP = () => { };
6
+ const IDENTITY = (value) => value;
7
+ function wrap(value) {
8
+ return value === undefined ? [] : Array.isArray(value) ? value : [value];
9
+ }
10
+ function unwrap(arr) {
11
+ return arr.length === 0 ? undefined : arr.length === 1 ? arr[0] : arr;
12
+ }
13
+ /**
14
+ * Ensures a value is an array.
15
+ *
16
+ * This function does the same thing as wrap() above except it handles nulls
17
+ * and iterables, so it is appropriate for wrapping user-provided element
18
+ * children.
19
+ */
20
+ function arrayify(value) {
21
+ return value == null
22
+ ? []
23
+ : Array.isArray(value)
24
+ ? value
25
+ : typeof value === "string" ||
26
+ typeof value[Symbol.iterator] !== "function"
27
+ ? [value]
28
+ : // TODO: inference broke in TypeScript 3.9.
29
+ [...value];
30
+ }
31
+ function isIteratorLike(value) {
32
+ return value != null && typeof value.next === "function";
33
+ }
34
+ function isPromiseLike(value) {
35
+ return value != null && typeof value.then === "function";
36
+ }
37
+ /***
38
+ * SPECIAL TAGS
39
+ *
40
+ * Crank provides a couple tags which have special meaning for the renderer.
41
+ ***/
42
+ /**
43
+ * A special tag for grouping multiple children within the same parent.
44
+ *
45
+ * All non-string iterables which appear in the element tree are implicitly
46
+ * wrapped in a fragment element.
47
+ *
48
+ * This tag is just the empty string, and you can use the empty string in
49
+ * createElement calls or transpiler options directly to avoid having to
50
+ * reference this export.
51
+ */
52
+ const Fragment = "";
53
+ // TODO: We assert the following symbol tags as any because TypeScript support
54
+ // for symbol tags in JSX doesn’t exist yet.
55
+ // https://github.com/microsoft/TypeScript/issues/38367
56
+ /**
57
+ * A special tag for rendering into a new root node via a root prop.
58
+ *
59
+ * This tag is useful for creating element trees with multiple roots, for
60
+ * things like modals or tooltips.
61
+ *
62
+ * Renderer.prototype.render() will implicitly wrap top-level element trees in
63
+ * a Portal element.
64
+ */
65
+ const Portal = Symbol.for("crank.Portal");
66
+ /**
67
+ * A special tag which preserves whatever was previously rendered in the
68
+ * element’s position.
69
+ *
70
+ * Copy elements are useful for when you want to prevent a subtree from
71
+ * rerendering as a performance optimization. Copy elements can also be keyed,
72
+ * in which case the previously rendered keyed element will be copied.
73
+ */
74
+ const Copy = Symbol.for("crank.Copy");
75
+ /**
76
+ * A special tag for injecting raw nodes or strings via a value prop.
77
+ *
78
+ * Renderer.prototype.raw() is called with the value prop.
79
+ */
80
+ const Raw = Symbol.for("crank.Raw");
81
+ const ElementSymbol = Symbol.for("crank.Element");
82
+ /**
83
+ * Elements are the basic building blocks of Crank applications. They are
84
+ * JavaScript objects which are interpreted by special classes called renderers
85
+ * to produce and manage stateful nodes.
86
+ *
87
+ * @template {Tag} [TTag=Tag] - The type of the tag of the element.
88
+ *
89
+ * @example
90
+ * // specific element types
91
+ * let div: Element<"div">;
92
+ * let portal: Element<Portal>;
93
+ * let myEl: Element<MyComponent>;
94
+ *
95
+ * // general element types
96
+ * let host: Element<string | symbol>;
97
+ * let component: Element<Component>;
98
+ *
99
+ * Typically, you use a helper function like createElement to create elements
100
+ * rather than instatiating this class directly.
101
+ */
102
+ class Element {
103
+ constructor(tag, props, key, ref, static_) {
104
+ this.tag = tag;
105
+ this.props = props;
106
+ this.key = key;
107
+ this.ref = ref;
108
+ this.static_ = static_;
109
+ }
110
+ }
111
+ // See Element interface
112
+ Element.prototype.$$typeof = ElementSymbol;
113
+ function isElement(value) {
114
+ return value != null && value.$$typeof === ElementSymbol;
115
+ }
116
+ /**
117
+ * Creates an element with the specified tag, props and children.
118
+ *
119
+ * This function is usually used as a transpilation target for JSX transpilers,
120
+ * but it can also be called directly. It additionally extracts special props so
121
+ * they aren’t accessible to renderer methods or components, and assigns the
122
+ * children prop according to any additional arguments passed to the function.
123
+ */
124
+ function createElement(tag, props, ...children) {
125
+ let key;
126
+ let ref;
127
+ let static_ = false;
128
+ const props1 = {};
129
+ if (props != null) {
130
+ for (const name in props) {
131
+ switch (name) {
132
+ case "crank-key":
133
+ case "c-key":
134
+ case "$key":
135
+ // We have to make sure we don’t assign null to the key because we
136
+ // don’t check for null keys in the diffing functions.
137
+ if (props[name] != null) {
138
+ key = props[name];
139
+ }
140
+ break;
141
+ case "crank-ref":
142
+ case "c-ref":
143
+ case "$ref":
144
+ if (typeof props[name] === "function") {
145
+ ref = props[name];
146
+ }
147
+ break;
148
+ case "crank-static":
149
+ case "c-static":
150
+ case "$static":
151
+ static_ = !!props[name];
152
+ break;
153
+ default:
154
+ props1[name] = props[name];
155
+ }
156
+ }
157
+ }
158
+ if (children.length > 1) {
159
+ props1.children = children;
160
+ }
161
+ else if (children.length === 1) {
162
+ props1.children = children[0];
163
+ }
164
+ return new Element(tag, props1, key, ref, static_);
165
+ }
166
+ /** Clones a given element, shallowly copying the props object. */
167
+ function cloneElement(el) {
168
+ if (!isElement(el)) {
169
+ throw new TypeError("Cannot clone non-element");
170
+ }
171
+ return new Element(el.tag, { ...el.props }, el.key, el.ref);
172
+ }
173
+ function narrow(value) {
174
+ if (typeof value === "boolean" || value == null) {
175
+ return undefined;
176
+ }
177
+ else if (typeof value === "string" || isElement(value)) {
178
+ return value;
179
+ }
180
+ else if (typeof value[Symbol.iterator] === "function") {
181
+ return createElement(Fragment, null, value);
182
+ }
183
+ return value.toString();
184
+ }
185
+ /**
186
+ * Takes an array of element values and normalizes the output as an array of
187
+ * nodes and strings.
188
+ *
189
+ * @returns Normalized array of nodes and/or strings.
190
+ *
191
+ * Normalize will flatten only one level of nested arrays, because it is
192
+ * designed to be called once at each level of the tree. It will also
193
+ * concatenate adjacent strings and remove all undefined values.
194
+ */
195
+ function normalize(values) {
196
+ const result = [];
197
+ let buffer;
198
+ for (let i = 0; i < values.length; i++) {
199
+ const value = values[i];
200
+ if (!value) ;
201
+ else if (typeof value === "string") {
202
+ buffer = (buffer || "") + value;
203
+ }
204
+ else if (!Array.isArray(value)) {
205
+ if (buffer) {
206
+ result.push(buffer);
207
+ buffer = undefined;
208
+ }
209
+ result.push(value);
210
+ }
211
+ else {
212
+ // We could use recursion here but it’s just easier to do it inline.
213
+ for (let j = 0; j < value.length; j++) {
214
+ const value1 = value[j];
215
+ if (!value1) ;
216
+ else if (typeof value1 === "string") {
217
+ buffer = (buffer || "") + value1;
218
+ }
219
+ else {
220
+ if (buffer) {
221
+ result.push(buffer);
222
+ buffer = undefined;
223
+ }
224
+ result.push(value1);
225
+ }
226
+ }
227
+ }
228
+ }
229
+ if (buffer) {
230
+ result.push(buffer);
231
+ }
232
+ return result;
233
+ }
234
+ /**
235
+ * @internal
236
+ * The internal nodes which are cached and diffed against new elements when
237
+ * rendering element trees.
238
+ */
239
+ class Retainer {
240
+ constructor(el) {
241
+ this.el = el;
242
+ this.ctx = undefined;
243
+ this.children = undefined;
244
+ this.value = undefined;
245
+ this.cachedChildValues = undefined;
246
+ this.fallbackValue = undefined;
247
+ this.inflightValue = undefined;
248
+ this.onNextValues = undefined;
249
+ }
250
+ }
251
+ /**
252
+ * Finds the value of the element according to its type.
253
+ *
254
+ * @returns The value of the element.
255
+ */
256
+ function getValue(ret) {
257
+ if (typeof ret.fallbackValue !== "undefined") {
258
+ return typeof ret.fallbackValue === "object"
259
+ ? getValue(ret.fallbackValue)
260
+ : ret.fallbackValue;
261
+ }
262
+ else if (ret.el.tag === Portal) {
263
+ return;
264
+ }
265
+ else if (typeof ret.el.tag !== "function" && ret.el.tag !== Fragment) {
266
+ return ret.value;
267
+ }
268
+ return unwrap(getChildValues(ret));
269
+ }
270
+ /**
271
+ * Walks an element’s children to find its child values.
272
+ *
273
+ * @returns A normalized array of nodes and strings.
274
+ */
275
+ function getChildValues(ret) {
276
+ if (ret.cachedChildValues) {
277
+ return wrap(ret.cachedChildValues);
278
+ }
279
+ const values = [];
280
+ const children = wrap(ret.children);
281
+ for (let i = 0; i < children.length; i++) {
282
+ const child = children[i];
283
+ if (child) {
284
+ values.push(typeof child === "string" ? child : getValue(child));
285
+ }
286
+ }
287
+ const values1 = normalize(values);
288
+ const tag = ret.el.tag;
289
+ if (typeof tag === "function" || (tag !== Fragment && tag !== Raw)) {
290
+ ret.cachedChildValues = unwrap(values1);
291
+ }
292
+ return values1;
293
+ }
294
+ const defaultRendererImpl = {
295
+ create() {
296
+ throw new Error("Not implemented");
297
+ },
298
+ hydrate() {
299
+ throw new Error("Not implemented");
300
+ },
301
+ scope: IDENTITY,
302
+ read: IDENTITY,
303
+ text: IDENTITY,
304
+ raw: IDENTITY,
305
+ patch: NOOP,
306
+ arrange: NOOP,
307
+ dispose: NOOP,
308
+ flush: NOOP,
309
+ };
310
+ const _RendererImpl = Symbol.for("crank.RendererImpl");
311
+ /**
312
+ * An abstract class which is subclassed to render to different target
313
+ * environments. This class is responsible for kicking off the rendering
314
+ * process and caching previous trees by root.
315
+ *
316
+ * @template TNode - The type of the node for a rendering environment.
317
+ * @template TScope - Data which is passed down the tree.
318
+ * @template TRoot - The type of the root for a rendering environment.
319
+ * @template TResult - The type of exposed values.
320
+ */
321
+ class Renderer {
322
+ constructor(impl) {
323
+ this.cache = new WeakMap();
324
+ this[_RendererImpl] = {
325
+ ...defaultRendererImpl,
326
+ ...impl,
327
+ };
328
+ }
329
+ /**
330
+ * Renders an element tree into a specific root.
331
+ *
332
+ * @param children - An element tree. You can render null with a previously
333
+ * used root to delete the previously rendered element tree from the cache.
334
+ * @param root - The node to be rendered into. The renderer will cache
335
+ * element trees per root.
336
+ * @param bridge - An optional context that will be the ancestor context of all
337
+ * elements in the tree. Useful for connecting different renderers so that
338
+ * events/provisions properly propagate. The context for a given root must be
339
+ * the same or an error will be thrown.
340
+ *
341
+ * @returns The result of rendering the children, or a possible promise of
342
+ * the result if the element tree renders asynchronously.
343
+ */
344
+ render(children, root, bridge) {
345
+ let ret;
346
+ const ctx = bridge && bridge[_ContextImpl];
347
+ if (typeof root === "object" && root !== null) {
348
+ ret = this.cache.get(root);
349
+ }
350
+ let oldProps;
351
+ if (ret === undefined) {
352
+ ret = new Retainer(createElement(Portal, { children, root }));
353
+ ret.value = root;
354
+ ret.ctx = ctx;
355
+ if (typeof root === "object" && root !== null && children != null) {
356
+ this.cache.set(root, ret);
357
+ }
358
+ }
359
+ else if (ret.ctx !== ctx) {
360
+ throw new Error("Context mismatch");
361
+ }
362
+ else {
363
+ oldProps = ret.el.props;
364
+ ret.el = createElement(Portal, { children, root });
365
+ if (typeof root === "object" && root !== null && children == null) {
366
+ this.cache.delete(root);
367
+ }
368
+ }
369
+ const impl = this[_RendererImpl];
370
+ const childValues = diffChildren(impl, root, ret, ctx, impl.scope(undefined, Portal, ret.el.props), ret, children, undefined);
371
+ // We return the child values of the portal because portal elements
372
+ // themselves have no readable value.
373
+ if (isPromiseLike(childValues)) {
374
+ return childValues.then((childValues) => commitRootRender(impl, root, ctx, ret, childValues, oldProps));
375
+ }
376
+ return commitRootRender(impl, root, ctx, ret, childValues, oldProps);
377
+ }
378
+ hydrate(children, root, bridge) {
379
+ const impl = this[_RendererImpl];
380
+ const ctx = bridge && bridge[_ContextImpl];
381
+ let ret;
382
+ ret = this.cache.get(root);
383
+ if (ret !== undefined) {
384
+ // If there is a retainer for the root, hydration is not necessary.
385
+ return this.render(children, root, bridge);
386
+ }
387
+ let oldProps;
388
+ ret = new Retainer(createElement(Portal, { children, root }));
389
+ ret.value = root;
390
+ if (typeof root === "object" && root !== null && children != null) {
391
+ this.cache.set(root, ret);
392
+ }
393
+ const hydrationData = impl.hydrate(Portal, root, {});
394
+ const childValues = diffChildren(impl, root, ret, ctx, impl.scope(undefined, Portal, ret.el.props), ret, children, hydrationData);
395
+ // We return the child values of the portal because portal elements
396
+ // themselves have no readable value.
397
+ if (isPromiseLike(childValues)) {
398
+ return childValues.then((childValues) => commitRootRender(impl, root, ctx, ret, childValues, oldProps));
399
+ }
400
+ return commitRootRender(impl, root, ctx, ret, childValues, oldProps);
401
+ }
402
+ }
403
+ /*** PRIVATE RENDERER FUNCTIONS ***/
404
+ function commitRootRender(renderer, root, ctx, ret, childValues, oldProps) {
405
+ // element is a host or portal element
406
+ if (root != null) {
407
+ renderer.arrange(Portal, root, ret.el.props, childValues, oldProps, wrap(ret.cachedChildValues));
408
+ flush(renderer, root);
409
+ }
410
+ ret.cachedChildValues = unwrap(childValues);
411
+ if (root == null) {
412
+ unmount(renderer, ret, ctx, ret);
413
+ }
414
+ return renderer.read(ret.cachedChildValues);
415
+ }
416
+ function diffChildren(renderer, root, host, ctx, scope, parent, children, hydrationData) {
417
+ const oldRetained = wrap(parent.children);
418
+ const newRetained = [];
419
+ const newChildren = arrayify(children);
420
+ const values = [];
421
+ let graveyard;
422
+ let childrenByKey;
423
+ let seenKeys;
424
+ let isAsync = false;
425
+ let hydrationBlock;
426
+ let oi = 0;
427
+ let oldLength = oldRetained.length;
428
+ for (let ni = 0, newLength = newChildren.length; ni < newLength; ni++) {
429
+ // length checks to prevent index out of bounds deoptimizations.
430
+ let ret = oi >= oldLength ? undefined : oldRetained[oi];
431
+ let child = narrow(newChildren[ni]);
432
+ {
433
+ // aligning new children with old retainers
434
+ let oldKey = typeof ret === "object" ? ret.el.key : undefined;
435
+ let newKey = typeof child === "object" ? child.key : undefined;
436
+ if (newKey !== undefined && seenKeys && seenKeys.has(newKey)) {
437
+ console.error("Duplicate key", newKey);
438
+ newKey = undefined;
439
+ }
440
+ if (oldKey === newKey) {
441
+ if (childrenByKey !== undefined && newKey !== undefined) {
442
+ childrenByKey.delete(newKey);
443
+ }
444
+ oi++;
445
+ }
446
+ else {
447
+ childrenByKey = childrenByKey || createChildrenByKey(oldRetained, oi);
448
+ if (newKey === undefined) {
449
+ while (ret !== undefined && oldKey !== undefined) {
450
+ oi++;
451
+ ret = oldRetained[oi];
452
+ oldKey = typeof ret === "object" ? ret.el.key : undefined;
453
+ }
454
+ oi++;
455
+ }
456
+ else {
457
+ ret = childrenByKey.get(newKey);
458
+ if (ret !== undefined) {
459
+ childrenByKey.delete(newKey);
460
+ }
461
+ (seenKeys = seenKeys || new Set()).add(newKey);
462
+ }
463
+ }
464
+ }
465
+ // Updating
466
+ let value;
467
+ if (typeof child === "object") {
468
+ if (child.tag === Copy) {
469
+ value = getInflightValue(ret);
470
+ }
471
+ else {
472
+ let oldProps;
473
+ let static_ = false;
474
+ if (typeof ret === "object" && ret.el.tag === child.tag) {
475
+ oldProps = ret.el.props;
476
+ ret.el = child;
477
+ if (child.static_) {
478
+ value = getInflightValue(ret);
479
+ static_ = true;
480
+ }
481
+ }
482
+ else {
483
+ if (typeof ret === "object") {
484
+ (graveyard = graveyard || []).push(ret);
485
+ }
486
+ const fallback = ret;
487
+ ret = new Retainer(child);
488
+ ret.fallbackValue = fallback;
489
+ }
490
+ if (static_) ;
491
+ else if (child.tag === Raw) {
492
+ value = hydrationBlock
493
+ ? hydrationBlock.then(() => updateRaw(renderer, ret, scope, oldProps, hydrationData))
494
+ : updateRaw(renderer, ret, scope, oldProps, hydrationData);
495
+ }
496
+ else if (child.tag === Fragment) {
497
+ value = hydrationBlock
498
+ ? hydrationBlock.then(() => updateFragment(renderer, root, host, ctx, scope, ret, hydrationData))
499
+ : updateFragment(renderer, root, host, ctx, scope, ret, hydrationData);
500
+ }
501
+ else if (typeof child.tag === "function") {
502
+ value = hydrationBlock
503
+ ? hydrationBlock.then(() => updateComponent(renderer, root, host, ctx, scope, ret, oldProps, hydrationData))
504
+ : updateComponent(renderer, root, host, ctx, scope, ret, oldProps, hydrationData);
505
+ }
506
+ else {
507
+ value = hydrationBlock
508
+ ? hydrationBlock.then(() => updateHost(renderer, root, ctx, scope, ret, oldProps, hydrationData))
509
+ : updateHost(renderer, root, ctx, scope, ret, oldProps, hydrationData);
510
+ }
511
+ }
512
+ const ref = child.ref;
513
+ if (isPromiseLike(value)) {
514
+ isAsync = true;
515
+ if (typeof ref === "function") {
516
+ value = value.then((value) => {
517
+ ref(renderer.read(value));
518
+ return value;
519
+ });
520
+ }
521
+ if (hydrationData !== undefined) {
522
+ hydrationBlock = value;
523
+ }
524
+ }
525
+ else {
526
+ if (typeof ref === "function") {
527
+ ref(renderer.read(value));
528
+ }
529
+ }
530
+ }
531
+ else {
532
+ // child is a string or undefined
533
+ if (typeof ret === "object") {
534
+ (graveyard = graveyard || []).push(ret);
535
+ }
536
+ if (typeof child === "string") {
537
+ value = ret = renderer.text(child, scope, hydrationData);
538
+ }
539
+ else {
540
+ ret = undefined;
541
+ }
542
+ }
543
+ values[ni] = value;
544
+ newRetained[ni] = ret;
545
+ }
546
+ // cleanup remaining retainers
547
+ for (; oi < oldLength; oi++) {
548
+ const ret = oldRetained[oi];
549
+ if (typeof ret === "object" && typeof ret.el.key === "undefined") {
550
+ (graveyard = graveyard || []).push(ret);
551
+ }
552
+ }
553
+ if (childrenByKey !== undefined && childrenByKey.size > 0) {
554
+ (graveyard = graveyard || []).push(...childrenByKey.values());
555
+ }
556
+ parent.children = unwrap(newRetained);
557
+ if (isAsync) {
558
+ let childValues1 = Promise.all(values).finally(() => {
559
+ if (graveyard) {
560
+ for (let i = 0; i < graveyard.length; i++) {
561
+ unmount(renderer, host, ctx, graveyard[i]);
562
+ }
563
+ }
564
+ });
565
+ let onChildValues;
566
+ childValues1 = Promise.race([
567
+ childValues1,
568
+ new Promise((resolve) => (onChildValues = resolve)),
569
+ ]);
570
+ if (parent.onNextValues) {
571
+ parent.onNextValues(childValues1);
572
+ }
573
+ parent.onNextValues = onChildValues;
574
+ return childValues1.then((childValues) => {
575
+ parent.inflightValue = parent.fallbackValue = undefined;
576
+ return normalize(childValues);
577
+ });
578
+ }
579
+ else {
580
+ if (graveyard) {
581
+ for (let i = 0; i < graveyard.length; i++) {
582
+ unmount(renderer, host, ctx, graveyard[i]);
583
+ }
584
+ }
585
+ if (parent.onNextValues) {
586
+ parent.onNextValues(values);
587
+ parent.onNextValues = undefined;
588
+ }
589
+ parent.inflightValue = parent.fallbackValue = undefined;
590
+ // We can assert there are no promises in the array because isAsync is false
591
+ return normalize(values);
592
+ }
593
+ }
594
+ function createChildrenByKey(children, offset) {
595
+ const childrenByKey = new Map();
596
+ for (let i = offset; i < children.length; i++) {
597
+ const child = children[i];
598
+ if (typeof child === "object" && typeof child.el.key !== "undefined") {
599
+ childrenByKey.set(child.el.key, child);
600
+ }
601
+ }
602
+ return childrenByKey;
603
+ }
604
+ function getInflightValue(child) {
605
+ if (typeof child !== "object") {
606
+ return child;
607
+ }
608
+ const ctx = typeof child.el.tag === "function" ? child.ctx : undefined;
609
+ if (ctx && ctx.f & IsUpdating && ctx.inflightValue) {
610
+ return ctx.inflightValue;
611
+ }
612
+ else if (child.inflightValue) {
613
+ return child.inflightValue;
614
+ }
615
+ return getValue(child);
616
+ }
617
+ function updateRaw(renderer, ret, scope, oldProps, hydrationData) {
618
+ const props = ret.el.props;
619
+ if (!oldProps || oldProps.value !== props.value) {
620
+ ret.value = renderer.raw(props.value, scope, hydrationData);
621
+ }
622
+ return ret.value;
623
+ }
624
+ function updateFragment(renderer, root, host, ctx, scope, ret, hydrationData) {
625
+ const childValues = diffChildren(renderer, root, host, ctx, scope, ret, ret.el.props.children, hydrationData);
626
+ if (isPromiseLike(childValues)) {
627
+ ret.inflightValue = childValues.then((childValues) => unwrap(childValues));
628
+ return ret.inflightValue;
629
+ }
630
+ return unwrap(childValues);
631
+ }
632
+ function updateHost(renderer, root, ctx, scope, ret, oldProps, hydrationData) {
633
+ const el = ret.el;
634
+ const tag = el.tag;
635
+ let hydrationValue;
636
+ if (el.tag === Portal) {
637
+ root = ret.value = el.props.root;
638
+ }
639
+ else {
640
+ if (hydrationData !== undefined) {
641
+ const value = hydrationData.children.shift();
642
+ hydrationValue = value;
643
+ }
644
+ }
645
+ scope = renderer.scope(scope, tag, el.props);
646
+ let childHydrationData;
647
+ if (hydrationValue != null && typeof hydrationValue !== "string") {
648
+ childHydrationData = renderer.hydrate(tag, hydrationValue, el.props);
649
+ if (childHydrationData === undefined) {
650
+ hydrationValue = undefined;
651
+ }
652
+ }
653
+ const childValues = diffChildren(renderer, root, ret, ctx, scope, ret, ret.el.props.children, childHydrationData);
654
+ if (isPromiseLike(childValues)) {
655
+ ret.inflightValue = childValues.then((childValues) => commitHost(renderer, scope, ret, childValues, oldProps, hydrationValue));
656
+ return ret.inflightValue;
657
+ }
658
+ return commitHost(renderer, scope, ret, childValues, oldProps, hydrationValue);
659
+ }
660
+ function commitHost(renderer, scope, ret, childValues, oldProps, hydrationValue) {
661
+ const tag = ret.el.tag;
662
+ let value = hydrationValue || ret.value;
663
+ let props = ret.el.props;
664
+ let copied;
665
+ if (tag !== Portal) {
666
+ if (value == null) {
667
+ // This assumes that renderer.create does not return nullish values.
668
+ value = ret.value = renderer.create(tag, props, scope);
669
+ }
670
+ for (const propName in { ...oldProps, ...props }) {
671
+ const propValue = props[propName];
672
+ if (propValue === Copy) {
673
+ // TODO: The Copy tag doubles as a way to skip the patching of a prop.
674
+ // Not sure about this feature. Should probably be removed.
675
+ (copied = copied || new Set()).add(propName);
676
+ }
677
+ else if (propName !== "children") {
678
+ renderer.patch(tag, value, propName, propValue, oldProps && oldProps[propName], scope);
679
+ }
680
+ }
681
+ }
682
+ if (copied) {
683
+ props = { ...ret.el.props };
684
+ for (const name of copied) {
685
+ props[name] = oldProps && oldProps[name];
686
+ }
687
+ ret.el = new Element(tag, props, ret.el.key, ret.el.ref);
688
+ }
689
+ renderer.arrange(tag, value, props, childValues, oldProps, wrap(ret.cachedChildValues));
690
+ ret.cachedChildValues = unwrap(childValues);
691
+ if (tag === Portal) {
692
+ flush(renderer, ret.value);
693
+ return;
694
+ }
695
+ return value;
696
+ }
697
+ function flush(renderer, root, initiator) {
698
+ renderer.flush(root);
699
+ if (typeof root !== "object" || root === null) {
700
+ return;
701
+ }
702
+ const flushMap = flushMaps.get(root);
703
+ if (flushMap) {
704
+ if (initiator) {
705
+ const flushMap1 = new Map();
706
+ for (let [ctx, callbacks] of flushMap) {
707
+ if (!ctxContains(initiator, ctx)) {
708
+ flushMap.delete(ctx);
709
+ flushMap1.set(ctx, callbacks);
710
+ }
711
+ }
712
+ if (flushMap1.size) {
713
+ flushMaps.set(root, flushMap1);
714
+ }
715
+ else {
716
+ flushMaps.delete(root);
717
+ }
718
+ }
719
+ else {
720
+ flushMaps.delete(root);
721
+ }
722
+ for (const [ctx, callbacks] of flushMap) {
723
+ const value = renderer.read(getValue(ctx.ret));
724
+ for (const callback of callbacks) {
725
+ callback(value);
726
+ }
727
+ }
728
+ }
729
+ }
730
+ function unmount(renderer, host, ctx, ret) {
731
+ if (typeof ret.el.tag === "function") {
732
+ ctx = ret.ctx;
733
+ unmountComponent(ctx);
734
+ }
735
+ else if (ret.el.tag === Portal) {
736
+ host = ret;
737
+ renderer.arrange(Portal, host.value, host.el.props, [], host.el.props, wrap(host.cachedChildValues));
738
+ flush(renderer, host.value);
739
+ }
740
+ else if (ret.el.tag !== Fragment) {
741
+ if (isEventTarget(ret.value)) {
742
+ const records = getListenerRecords(ctx, host);
743
+ for (let i = 0; i < records.length; i++) {
744
+ const record = records[i];
745
+ ret.value.removeEventListener(record.type, record.callback, record.options);
746
+ }
747
+ }
748
+ renderer.dispose(ret.el.tag, ret.value, ret.el.props);
749
+ host = ret;
750
+ }
751
+ const children = wrap(ret.children);
752
+ for (let i = 0; i < children.length; i++) {
753
+ const child = children[i];
754
+ if (typeof child === "object") {
755
+ unmount(renderer, host, ctx, child);
756
+ }
757
+ }
758
+ }
759
+ /*** CONTEXT FLAGS ***/
760
+ /**
761
+ * A flag which is true when the component is initialized or updated by an
762
+ * ancestor component or the root render call.
763
+ *
764
+ * Used to determine things like whether the nearest host ancestor needs to be
765
+ * rearranged.
766
+ */
767
+ const IsUpdating = 1 << 0;
768
+ /**
769
+ * A flag which is true when the component is synchronously executing.
770
+ *
771
+ * Used to guard against components triggering stack overflow or generator error.
772
+ */
773
+ const IsSyncExecuting = 1 << 1;
774
+ /**
775
+ * A flag which is true when the component is in a for...of loop.
776
+ */
777
+ const IsInForOfLoop = 1 << 2;
778
+ /**
779
+ * A flag which is true when the component is in a for await...of loop.
780
+ */
781
+ const IsInForAwaitOfLoop = 1 << 3;
782
+ /**
783
+ * A flag which is true when the component starts the render loop but has not
784
+ * yielded yet.
785
+ *
786
+ * Used to make sure that components yield at least once per loop.
787
+ */
788
+ const NeedsToYield = 1 << 4;
789
+ /**
790
+ * A flag used by async generator components in conjunction with the
791
+ * onAvailable callback to mark whether new props can be pulled via the context
792
+ * async iterator. See the Symbol.asyncIterator method and the
793
+ * resumeCtxIterator function.
794
+ */
795
+ const PropsAvailable = 1 << 5;
796
+ /**
797
+ * A flag which is set when a component errors.
798
+ *
799
+ * NOTE: This is mainly used to prevent some false positives in component
800
+ * yields or returns undefined warnings. The reason we’re using this versus
801
+ * IsUnmounted is a very troubling test (cascades sync generator parent and
802
+ * sync generator child) where synchronous code causes a stack overflow error
803
+ * in a non-deterministic way. Deeply disturbing stuff.
804
+ */
805
+ const IsErrored = 1 << 6;
806
+ /**
807
+ * A flag which is set when the component is unmounted. Unmounted components
808
+ * are no longer in the element tree and cannot refresh or rerender.
809
+ */
810
+ const IsUnmounted = 1 << 7;
811
+ /**
812
+ * A flag which indicates that the component is a sync generator component.
813
+ */
814
+ const IsSyncGen = 1 << 8;
815
+ /**
816
+ * A flag which indicates that the component is an async generator component.
817
+ */
818
+ const IsAsyncGen = 1 << 9;
819
+ /**
820
+ * A flag which is set while schedule callbacks are called.
821
+ */
822
+ const IsScheduling = 1 << 10;
823
+ /**
824
+ * A flag which is set when a schedule callback calls refresh.
825
+ */
826
+ const IsSchedulingRefresh = 1 << 11;
827
+ const provisionMaps = new WeakMap();
828
+ const scheduleMap = new WeakMap();
829
+ const cleanupMap = new WeakMap();
830
+ // keys are roots
831
+ const flushMaps = new WeakMap();
832
+ /**
833
+ * @internal
834
+ * The internal class which holds context data.
835
+ */
836
+ class ContextImpl {
837
+ constructor(renderer, root, host, parent, scope, ret) {
838
+ this.f = 0;
839
+ this.owner = new Context(this);
840
+ this.renderer = renderer;
841
+ this.root = root;
842
+ this.host = host;
843
+ this.parent = parent;
844
+ this.scope = scope;
845
+ this.ret = ret;
846
+ this.iterator = undefined;
847
+ this.inflightBlock = undefined;
848
+ this.inflightValue = undefined;
849
+ this.enqueuedBlock = undefined;
850
+ this.enqueuedValue = undefined;
851
+ this.onProps = undefined;
852
+ this.onPropsRequested = undefined;
853
+ }
854
+ }
855
+ const _ContextImpl = Symbol.for("crank.ContextImpl");
856
+ /**
857
+ * A class which is instantiated and passed to every component as its this
858
+ * value. Contexts form a tree just like elements and all components in the
859
+ * element tree are connected via contexts. Components can use this tree to
860
+ * communicate data upwards via events and downwards via provisions.
861
+ *
862
+ * @template [TProps=*] - The expected shape of the props passed to the
863
+ * component. Used to strongly type the Context iterator methods.
864
+ * @template [TResult=*] - The readable element value type. It is used in
865
+ * places such as the return value of refresh and the argument passed to
866
+ * schedule and cleanup callbacks.
867
+ */
868
+ class Context {
869
+ // TODO: If we could make the constructor function take a nicer value, it
870
+ // would be useful for testing purposes.
871
+ constructor(impl) {
872
+ this[_ContextImpl] = impl;
873
+ }
874
+ /**
875
+ * The current props of the associated element.
876
+ *
877
+ * Typically, you should read props either via the first parameter of the
878
+ * component or via the context iterator methods. This property is mainly for
879
+ * plugins or utilities which wrap contexts.
880
+ */
881
+ get props() {
882
+ return this[_ContextImpl].ret.el.props;
883
+ }
884
+ // TODO: Should we rename this???
885
+ /**
886
+ * The current value of the associated element.
887
+ *
888
+ * Typically, you should read values via refs, generator yield expressions,
889
+ * or the refresh, schedule, cleanup, or flush methods. This property is
890
+ * mainly for plugins or utilities which wrap contexts.
891
+ */
892
+ get value() {
893
+ return this[_ContextImpl].renderer.read(getValue(this[_ContextImpl].ret));
894
+ }
895
+ *[Symbol.iterator]() {
896
+ const ctx = this[_ContextImpl];
897
+ try {
898
+ ctx.f |= IsInForOfLoop;
899
+ while (!(ctx.f & IsUnmounted)) {
900
+ if (ctx.f & NeedsToYield) {
901
+ throw new Error("Context iterated twice without a yield");
902
+ }
903
+ else {
904
+ ctx.f |= NeedsToYield;
905
+ }
906
+ yield ctx.ret.el.props;
907
+ }
908
+ }
909
+ finally {
910
+ ctx.f &= ~IsInForOfLoop;
911
+ }
912
+ }
913
+ async *[Symbol.asyncIterator]() {
914
+ const ctx = this[_ContextImpl];
915
+ if (ctx.f & IsSyncGen) {
916
+ throw new Error("Use for...of in sync generator components");
917
+ }
918
+ try {
919
+ ctx.f |= IsInForAwaitOfLoop;
920
+ while (!(ctx.f & IsUnmounted)) {
921
+ if (ctx.f & NeedsToYield) {
922
+ throw new Error("Context iterated twice without a yield");
923
+ }
924
+ else {
925
+ ctx.f |= NeedsToYield;
926
+ }
927
+ if (ctx.f & PropsAvailable) {
928
+ ctx.f &= ~PropsAvailable;
929
+ yield ctx.ret.el.props;
930
+ }
931
+ else {
932
+ const props = await new Promise((resolve) => (ctx.onProps = resolve));
933
+ if (ctx.f & IsUnmounted) {
934
+ break;
935
+ }
936
+ yield props;
937
+ }
938
+ if (ctx.onPropsRequested) {
939
+ ctx.onPropsRequested();
940
+ ctx.onPropsRequested = undefined;
941
+ }
942
+ }
943
+ }
944
+ finally {
945
+ ctx.f &= ~IsInForAwaitOfLoop;
946
+ if (ctx.onPropsRequested) {
947
+ ctx.onPropsRequested();
948
+ ctx.onPropsRequested = undefined;
949
+ }
950
+ }
951
+ }
952
+ /**
953
+ * Re-executes a component.
954
+ *
955
+ * @returns The rendered value of the component or a promise thereof if the
956
+ * component or its children execute asynchronously.
957
+ *
958
+ * The refresh method works a little differently for async generator
959
+ * components, in that it will resume the Context’s props async iterator
960
+ * rather than resuming execution. This is because async generator components
961
+ * are perpetually resumed independent of updates, and rely on the props
962
+ * async iterator to suspend.
963
+ */
964
+ refresh() {
965
+ const ctx = this[_ContextImpl];
966
+ if (ctx.f & IsUnmounted) {
967
+ console.error("Component is unmounted");
968
+ return ctx.renderer.read(undefined);
969
+ }
970
+ else if (ctx.f & IsSyncExecuting) {
971
+ console.error("Component is already executing");
972
+ return this.value;
973
+ }
974
+ const value = enqueueComponentRun(ctx);
975
+ if (isPromiseLike(value)) {
976
+ return value.then((value) => ctx.renderer.read(value));
977
+ }
978
+ return ctx.renderer.read(value);
979
+ }
980
+ /**
981
+ * Registers a callback which fires when the component commits. Will only
982
+ * fire once per callback and update.
983
+ */
984
+ schedule(callback) {
985
+ const ctx = this[_ContextImpl];
986
+ let callbacks = scheduleMap.get(ctx);
987
+ if (!callbacks) {
988
+ callbacks = new Set();
989
+ scheduleMap.set(ctx, callbacks);
990
+ }
991
+ callbacks.add(callback);
992
+ }
993
+ /**
994
+ * Registers a callback which fires when the component’s children are
995
+ * rendered into the root. Will only fire once per callback and render.
996
+ */
997
+ flush(callback) {
998
+ const ctx = this[_ContextImpl];
999
+ if (typeof ctx.root !== "object" || ctx.root === null) {
1000
+ return;
1001
+ }
1002
+ let flushMap = flushMaps.get(ctx.root);
1003
+ if (!flushMap) {
1004
+ flushMap = new Map();
1005
+ flushMaps.set(ctx.root, flushMap);
1006
+ }
1007
+ let callbacks = flushMap.get(ctx);
1008
+ if (!callbacks) {
1009
+ callbacks = new Set();
1010
+ flushMap.set(ctx, callbacks);
1011
+ }
1012
+ callbacks.add(callback);
1013
+ }
1014
+ /**
1015
+ * Registers a callback which fires when the component unmounts. Will only
1016
+ * fire once per callback.
1017
+ */
1018
+ cleanup(callback) {
1019
+ const ctx = this[_ContextImpl];
1020
+ let callbacks = cleanupMap.get(ctx);
1021
+ if (!callbacks) {
1022
+ callbacks = new Set();
1023
+ cleanupMap.set(ctx, callbacks);
1024
+ }
1025
+ callbacks.add(callback);
1026
+ }
1027
+ consume(key) {
1028
+ for (let ctx = this[_ContextImpl].parent; ctx !== undefined; ctx = ctx.parent) {
1029
+ const provisions = provisionMaps.get(ctx);
1030
+ if (provisions && provisions.has(key)) {
1031
+ return provisions.get(key);
1032
+ }
1033
+ }
1034
+ }
1035
+ provide(key, value) {
1036
+ const ctx = this[_ContextImpl];
1037
+ let provisions = provisionMaps.get(ctx);
1038
+ if (!provisions) {
1039
+ provisions = new Map();
1040
+ provisionMaps.set(ctx, provisions);
1041
+ }
1042
+ provisions.set(key, value);
1043
+ }
1044
+ addEventListener(type, listener, options) {
1045
+ const ctx = this[_ContextImpl];
1046
+ let listeners;
1047
+ if (!isListenerOrListenerObject(listener)) {
1048
+ return;
1049
+ }
1050
+ else {
1051
+ const listeners1 = listenersMap.get(ctx);
1052
+ if (listeners1) {
1053
+ listeners = listeners1;
1054
+ }
1055
+ else {
1056
+ listeners = [];
1057
+ listenersMap.set(ctx, listeners);
1058
+ }
1059
+ }
1060
+ options = normalizeListenerOptions(options);
1061
+ let callback;
1062
+ if (typeof listener === "object") {
1063
+ callback = () => listener.handleEvent.apply(listener, arguments);
1064
+ }
1065
+ else {
1066
+ callback = listener;
1067
+ }
1068
+ const record = { type, listener, callback, options };
1069
+ if (options.once) {
1070
+ record.callback = function () {
1071
+ const i = listeners.indexOf(record);
1072
+ if (i !== -1) {
1073
+ listeners.splice(i, 1);
1074
+ }
1075
+ return callback.apply(this, arguments);
1076
+ };
1077
+ }
1078
+ if (listeners.some((record1) => record.type === record1.type &&
1079
+ record.listener === record1.listener &&
1080
+ !record.options.capture === !record1.options.capture)) {
1081
+ return;
1082
+ }
1083
+ listeners.push(record);
1084
+ // TODO: is it possible to separate out the EventTarget delegation logic
1085
+ for (const value of getChildValues(ctx.ret)) {
1086
+ if (isEventTarget(value)) {
1087
+ value.addEventListener(record.type, record.callback, record.options);
1088
+ }
1089
+ }
1090
+ }
1091
+ removeEventListener(type, listener, options) {
1092
+ const ctx = this[_ContextImpl];
1093
+ const listeners = listenersMap.get(ctx);
1094
+ if (listeners == null || !isListenerOrListenerObject(listener)) {
1095
+ return;
1096
+ }
1097
+ const options1 = normalizeListenerOptions(options);
1098
+ const i = listeners.findIndex((record) => record.type === type &&
1099
+ record.listener === listener &&
1100
+ !record.options.capture === !options1.capture);
1101
+ if (i === -1) {
1102
+ return;
1103
+ }
1104
+ const record = listeners[i];
1105
+ listeners.splice(i, 1);
1106
+ // TODO: is it possible to separate out the EventTarget delegation logic
1107
+ for (const value of getChildValues(ctx.ret)) {
1108
+ if (isEventTarget(value)) {
1109
+ value.removeEventListener(record.type, record.callback, record.options);
1110
+ }
1111
+ }
1112
+ }
1113
+ dispatchEvent(ev) {
1114
+ const ctx = this[_ContextImpl];
1115
+ const path = [];
1116
+ for (let parent = ctx.parent; parent !== undefined; parent = parent.parent) {
1117
+ path.push(parent);
1118
+ }
1119
+ // We patch the stopImmediatePropagation method because ev.cancelBubble
1120
+ // only informs us if stopPropagation was called and there are no
1121
+ // properties which inform us if stopImmediatePropagation was called.
1122
+ let immediateCancelBubble = false;
1123
+ const stopImmediatePropagation = ev.stopImmediatePropagation;
1124
+ setEventProperty(ev, "stopImmediatePropagation", () => {
1125
+ immediateCancelBubble = true;
1126
+ return stopImmediatePropagation.call(ev);
1127
+ });
1128
+ setEventProperty(ev, "target", ctx.owner);
1129
+ // The only possible errors in this block are errors thrown by callbacks,
1130
+ // and dispatchEvent will only log these errors rather than throwing
1131
+ // them. Therefore, we place all code in a try block, log errors in the
1132
+ // catch block, and use an unsafe return statement in the finally block.
1133
+ //
1134
+ // Each early return within the try block returns true because while the
1135
+ // return value is overridden in the finally block, TypeScript
1136
+ // (justifiably) does not recognize the unsafe return statement.
1137
+ try {
1138
+ setEventProperty(ev, "eventPhase", CAPTURING_PHASE);
1139
+ for (let i = path.length - 1; i >= 0; i--) {
1140
+ const target = path[i];
1141
+ const listeners = listenersMap.get(target);
1142
+ if (listeners) {
1143
+ setEventProperty(ev, "currentTarget", target.owner);
1144
+ for (const record of listeners) {
1145
+ if (record.type === ev.type && record.options.capture) {
1146
+ try {
1147
+ record.callback.call(target.owner, ev);
1148
+ }
1149
+ catch (err) {
1150
+ console.error(err);
1151
+ }
1152
+ if (immediateCancelBubble) {
1153
+ return true;
1154
+ }
1155
+ }
1156
+ }
1157
+ }
1158
+ if (ev.cancelBubble) {
1159
+ return true;
1160
+ }
1161
+ }
1162
+ {
1163
+ setEventProperty(ev, "eventPhase", AT_TARGET);
1164
+ setEventProperty(ev, "currentTarget", ctx.owner);
1165
+ const propCallback = ctx.ret.el.props["on" + ev.type];
1166
+ if (propCallback != null) {
1167
+ propCallback(ev);
1168
+ if (immediateCancelBubble || ev.cancelBubble) {
1169
+ return true;
1170
+ }
1171
+ }
1172
+ const listeners = listenersMap.get(ctx);
1173
+ if (listeners) {
1174
+ for (const record of listeners) {
1175
+ if (record.type === ev.type) {
1176
+ try {
1177
+ record.callback.call(ctx.owner, ev);
1178
+ }
1179
+ catch (err) {
1180
+ console.error(err);
1181
+ }
1182
+ if (immediateCancelBubble) {
1183
+ return true;
1184
+ }
1185
+ }
1186
+ }
1187
+ if (ev.cancelBubble) {
1188
+ return true;
1189
+ }
1190
+ }
1191
+ }
1192
+ if (ev.bubbles) {
1193
+ setEventProperty(ev, "eventPhase", BUBBLING_PHASE);
1194
+ for (let i = 0; i < path.length; i++) {
1195
+ const target = path[i];
1196
+ const listeners = listenersMap.get(target);
1197
+ if (listeners) {
1198
+ setEventProperty(ev, "currentTarget", target.owner);
1199
+ for (const record of listeners) {
1200
+ if (record.type === ev.type && !record.options.capture) {
1201
+ try {
1202
+ record.callback.call(target.owner, ev);
1203
+ }
1204
+ catch (err) {
1205
+ console.error(err);
1206
+ }
1207
+ if (immediateCancelBubble) {
1208
+ return true;
1209
+ }
1210
+ }
1211
+ }
1212
+ }
1213
+ if (ev.cancelBubble) {
1214
+ return true;
1215
+ }
1216
+ }
1217
+ }
1218
+ }
1219
+ finally {
1220
+ setEventProperty(ev, "eventPhase", NONE);
1221
+ setEventProperty(ev, "currentTarget", null);
1222
+ // eslint-disable-next-line no-unsafe-finally
1223
+ return !ev.defaultPrevented;
1224
+ }
1225
+ }
1226
+ }
1227
+ /*** PRIVATE CONTEXT FUNCTIONS ***/
1228
+ function ctxContains(parent, child) {
1229
+ for (let current = child; current !== undefined; current = current.parent) {
1230
+ if (current === parent) {
1231
+ return true;
1232
+ }
1233
+ }
1234
+ return false;
1235
+ }
1236
+ function updateComponent(renderer, root, host, parent, scope, ret, oldProps, hydrationData) {
1237
+ let ctx;
1238
+ if (oldProps) {
1239
+ // TODO: we should probably use the existence of ret.ctx
1240
+ if (ret.ctx == null) {
1241
+ throw new Error("Hmmm");
1242
+ }
1243
+ ctx = ret.ctx;
1244
+ if (ctx.f & IsSyncExecuting) {
1245
+ console.error("Component is already executing");
1246
+ return ret.cachedChildValues;
1247
+ }
1248
+ }
1249
+ else {
1250
+ ctx = ret.ctx = new ContextImpl(renderer, root, host, parent, scope, ret);
1251
+ }
1252
+ ctx.f |= IsUpdating;
1253
+ return enqueueComponentRun(ctx, hydrationData);
1254
+ }
1255
+ function updateComponentChildren(ctx, children, hydrationData) {
1256
+ if (ctx.f & IsUnmounted) {
1257
+ return;
1258
+ }
1259
+ else if (ctx.f & IsErrored) {
1260
+ // This branch is necessary for some race conditions where this function is
1261
+ // called after iterator.throw() in async generator components.
1262
+ return;
1263
+ }
1264
+ else if (children === undefined) {
1265
+ console.error("A component has returned or yielded undefined. If this was intentional, return or yield null instead.");
1266
+ }
1267
+ let childValues;
1268
+ try {
1269
+ // TODO: WAT
1270
+ // We set the isExecuting flag in case a child component dispatches an event
1271
+ // which bubbles to this component and causes a synchronous refresh().
1272
+ ctx.f |= IsSyncExecuting;
1273
+ childValues = diffChildren(ctx.renderer, ctx.root, ctx.host, ctx, ctx.scope, ctx.ret, narrow(children), hydrationData);
1274
+ }
1275
+ finally {
1276
+ ctx.f &= ~IsSyncExecuting;
1277
+ }
1278
+ if (isPromiseLike(childValues)) {
1279
+ ctx.ret.inflightValue = childValues.then((childValues) => commitComponent(ctx, childValues));
1280
+ return ctx.ret.inflightValue;
1281
+ }
1282
+ return commitComponent(ctx, childValues);
1283
+ }
1284
+ function commitComponent(ctx, values) {
1285
+ if (ctx.f & IsUnmounted) {
1286
+ return;
1287
+ }
1288
+ const listeners = listenersMap.get(ctx);
1289
+ if (listeners && listeners.length) {
1290
+ for (let i = 0; i < values.length; i++) {
1291
+ const value = values[i];
1292
+ if (isEventTarget(value)) {
1293
+ for (let j = 0; j < listeners.length; j++) {
1294
+ const record = listeners[j];
1295
+ value.addEventListener(record.type, record.callback, record.options);
1296
+ }
1297
+ }
1298
+ }
1299
+ }
1300
+ const oldValues = wrap(ctx.ret.cachedChildValues);
1301
+ let value = (ctx.ret.cachedChildValues = unwrap(values));
1302
+ if (ctx.f & IsScheduling) {
1303
+ ctx.f |= IsSchedulingRefresh;
1304
+ }
1305
+ else if (!(ctx.f & IsUpdating)) {
1306
+ // If we’re not updating the component, which happens when components are
1307
+ // refreshed, or when async generator components iterate, we have to do a
1308
+ // little bit housekeeping when a component’s child values have changed.
1309
+ if (!arrayEqual(oldValues, values)) {
1310
+ const records = getListenerRecords(ctx.parent, ctx.host);
1311
+ if (records.length) {
1312
+ for (let i = 0; i < values.length; i++) {
1313
+ const value = values[i];
1314
+ if (isEventTarget(value)) {
1315
+ for (let j = 0; j < records.length; j++) {
1316
+ const record = records[j];
1317
+ value.addEventListener(record.type, record.callback, record.options);
1318
+ }
1319
+ }
1320
+ }
1321
+ }
1322
+ // rearranging the nearest ancestor host element
1323
+ const host = ctx.host;
1324
+ const oldHostValues = wrap(host.cachedChildValues);
1325
+ invalidate(ctx, host);
1326
+ const hostValues = getChildValues(host);
1327
+ ctx.renderer.arrange(host.el.tag, host.value, host.el.props, hostValues,
1328
+ // props and oldProps are the same because the host isn’t updated.
1329
+ host.el.props, oldHostValues);
1330
+ }
1331
+ flush(ctx.renderer, ctx.root, ctx);
1332
+ }
1333
+ const callbacks = scheduleMap.get(ctx);
1334
+ if (callbacks) {
1335
+ scheduleMap.delete(ctx);
1336
+ ctx.f |= IsScheduling;
1337
+ const value1 = ctx.renderer.read(value);
1338
+ for (const callback of callbacks) {
1339
+ callback(value1);
1340
+ }
1341
+ ctx.f &= ~IsScheduling;
1342
+ // Handles an edge case where refresh() is called during a schedule().
1343
+ if (ctx.f & IsSchedulingRefresh) {
1344
+ ctx.f &= ~IsSchedulingRefresh;
1345
+ value = getValue(ctx.ret);
1346
+ }
1347
+ }
1348
+ ctx.f &= ~IsUpdating;
1349
+ return value;
1350
+ }
1351
+ function invalidate(ctx, host) {
1352
+ for (let parent = ctx.parent; parent !== undefined && parent.host === host; parent = parent.parent) {
1353
+ parent.ret.cachedChildValues = undefined;
1354
+ }
1355
+ host.cachedChildValues = undefined;
1356
+ }
1357
+ function arrayEqual(arr1, arr2) {
1358
+ if (arr1.length !== arr2.length) {
1359
+ return false;
1360
+ }
1361
+ for (let i = 0; i < arr1.length; i++) {
1362
+ const value1 = arr1[i];
1363
+ const value2 = arr2[i];
1364
+ if (value1 !== value2) {
1365
+ return false;
1366
+ }
1367
+ }
1368
+ return true;
1369
+ }
1370
+ /** Enqueues and executes the component associated with the context. */
1371
+ function enqueueComponentRun(ctx, hydrationData) {
1372
+ if (ctx.f & IsAsyncGen && !(ctx.f & IsInForOfLoop)) {
1373
+ if (hydrationData !== undefined) {
1374
+ throw new Error("Hydration error");
1375
+ }
1376
+ // This branch will run for non-initial renders of async generator
1377
+ // components when they are not in for...of loops. When in a for...of loop,
1378
+ // async generator components will behave normally.
1379
+ //
1380
+ // Async gen componennts can be in one of three states:
1381
+ //
1382
+ // 1. propsAvailable flag is true: "available"
1383
+ //
1384
+ // The component is suspended somewhere in the loop. When the component
1385
+ // reaches the bottom of the loop, it will run again with the next props.
1386
+ //
1387
+ // 2. onAvailable callback is defined: "suspended"
1388
+ //
1389
+ // The component has suspended at the bottom of the loop and is waiting
1390
+ // for new props.
1391
+ //
1392
+ // 3. neither 1 or 2: "Running"
1393
+ //
1394
+ // The component is suspended somewhere in the loop. When the component
1395
+ // reaches the bottom of the loop, it will suspend.
1396
+ //
1397
+ // Components will never be both available and suspended at
1398
+ // the same time.
1399
+ //
1400
+ // If the component is at the loop bottom, this means that the next value
1401
+ // produced by the component will have the most up to date props, so we can
1402
+ // simply return the current inflight value. Otherwise, we have to wait for
1403
+ // the bottom of the loop to be reached before returning the inflight
1404
+ // value.
1405
+ const isAtLoopbottom = ctx.f & IsInForAwaitOfLoop && !ctx.onProps;
1406
+ resumePropsIterator(ctx);
1407
+ if (isAtLoopbottom) {
1408
+ if (ctx.inflightBlock == null) {
1409
+ ctx.inflightBlock = new Promise((resolve) => (ctx.onPropsRequested = resolve));
1410
+ }
1411
+ return ctx.inflightBlock.then(() => {
1412
+ ctx.inflightBlock = undefined;
1413
+ return ctx.inflightValue;
1414
+ });
1415
+ }
1416
+ return ctx.inflightValue;
1417
+ }
1418
+ else if (!ctx.inflightBlock) {
1419
+ try {
1420
+ const [block, value] = runComponent(ctx, hydrationData);
1421
+ if (block) {
1422
+ ctx.inflightBlock = block
1423
+ // TODO: there is some fuckery going on here related to async
1424
+ // generator components resuming when they’re meant to be returned.
1425
+ .then((v) => v)
1426
+ .finally(() => advanceComponent(ctx));
1427
+ // stepComponent will only return a block if the value is asynchronous
1428
+ ctx.inflightValue = value;
1429
+ }
1430
+ return value;
1431
+ }
1432
+ catch (err) {
1433
+ if (!(ctx.f & IsUpdating)) {
1434
+ return propagateError(ctx.parent, err);
1435
+ }
1436
+ throw err;
1437
+ }
1438
+ }
1439
+ else if (!ctx.enqueuedBlock) {
1440
+ if (hydrationData !== undefined) {
1441
+ throw new Error("Hydration error");
1442
+ }
1443
+ // We need to assign enqueuedBlock and enqueuedValue synchronously, hence
1444
+ // the Promise constructor call here.
1445
+ let resolveEnqueuedBlock;
1446
+ ctx.enqueuedBlock = new Promise((resolve) => (resolveEnqueuedBlock = resolve));
1447
+ ctx.enqueuedValue = ctx.inflightBlock.then(() => {
1448
+ try {
1449
+ const [block, value] = runComponent(ctx);
1450
+ if (block) {
1451
+ resolveEnqueuedBlock(block.finally(() => advanceComponent(ctx)));
1452
+ }
1453
+ return value;
1454
+ }
1455
+ catch (err) {
1456
+ if (!(ctx.f & IsUpdating)) {
1457
+ return propagateError(ctx.parent, err);
1458
+ }
1459
+ throw err;
1460
+ }
1461
+ });
1462
+ }
1463
+ return ctx.enqueuedValue;
1464
+ }
1465
+ /** Called when the inflight block promise settles. */
1466
+ function advanceComponent(ctx) {
1467
+ if (ctx.f & IsAsyncGen && !(ctx.f & IsInForOfLoop)) {
1468
+ return;
1469
+ }
1470
+ ctx.inflightBlock = ctx.enqueuedBlock;
1471
+ ctx.inflightValue = ctx.enqueuedValue;
1472
+ ctx.enqueuedBlock = undefined;
1473
+ ctx.enqueuedValue = undefined;
1474
+ }
1475
+ /**
1476
+ * This function is responsible for executing the component and handling all
1477
+ * the different component types. We cannot identify whether a component is a
1478
+ * generator or async without calling it and inspecting the return value.
1479
+ *
1480
+ * @returns {[block, value]} A tuple where
1481
+ * block - A possible promise which represents the duration during which the
1482
+ * component is blocked from updating.
1483
+ * value - A possible promise resolving to the rendered value of children.
1484
+ *
1485
+ * Each component type will block according to the type of the component.
1486
+ * - Sync function components never block and will transparently pass updates
1487
+ * to children.
1488
+ * - Async function components and async generator components block while
1489
+ * executing itself, but will not block for async children.
1490
+ * - Sync generator components block while any children are executing, because
1491
+ * they are expected to only resume when they’ve actually rendered.
1492
+ */
1493
+ function runComponent(ctx, hydrationData) {
1494
+ const ret = ctx.ret;
1495
+ const initial = !ctx.iterator;
1496
+ if (initial) {
1497
+ resumePropsIterator(ctx);
1498
+ ctx.f |= IsSyncExecuting;
1499
+ clearEventListeners(ctx);
1500
+ let result;
1501
+ try {
1502
+ result = ret.el.tag.call(ctx.owner, ret.el.props);
1503
+ }
1504
+ catch (err) {
1505
+ ctx.f |= IsErrored;
1506
+ throw err;
1507
+ }
1508
+ finally {
1509
+ ctx.f &= ~IsSyncExecuting;
1510
+ }
1511
+ if (isIteratorLike(result)) {
1512
+ ctx.iterator = result;
1513
+ }
1514
+ else if (isPromiseLike(result)) {
1515
+ // async function component
1516
+ const result1 = result instanceof Promise ? result : Promise.resolve(result);
1517
+ const value = result1.then((result) => updateComponentChildren(ctx, result, hydrationData), (err) => {
1518
+ ctx.f |= IsErrored;
1519
+ throw err;
1520
+ });
1521
+ return [result1.catch(NOOP), value];
1522
+ }
1523
+ else {
1524
+ // sync function component
1525
+ return [
1526
+ undefined,
1527
+ updateComponentChildren(ctx, result, hydrationData),
1528
+ ];
1529
+ }
1530
+ }
1531
+ else if (hydrationData !== undefined) {
1532
+ throw new Error("Hydration error");
1533
+ }
1534
+ let iteration;
1535
+ if (initial) {
1536
+ try {
1537
+ ctx.f |= IsSyncExecuting;
1538
+ iteration = ctx.iterator.next();
1539
+ }
1540
+ catch (err) {
1541
+ ctx.f |= IsErrored;
1542
+ throw err;
1543
+ }
1544
+ finally {
1545
+ ctx.f &= ~IsSyncExecuting;
1546
+ }
1547
+ if (isPromiseLike(iteration)) {
1548
+ ctx.f |= IsAsyncGen;
1549
+ }
1550
+ else {
1551
+ ctx.f |= IsSyncGen;
1552
+ }
1553
+ }
1554
+ if (ctx.f & IsSyncGen) {
1555
+ ctx.f &= ~NeedsToYield;
1556
+ // sync generator component
1557
+ if (!initial) {
1558
+ try {
1559
+ ctx.f |= IsSyncExecuting;
1560
+ iteration = ctx.iterator.next(ctx.renderer.read(getValue(ret)));
1561
+ }
1562
+ catch (err) {
1563
+ ctx.f |= IsErrored;
1564
+ throw err;
1565
+ }
1566
+ finally {
1567
+ ctx.f &= ~IsSyncExecuting;
1568
+ }
1569
+ }
1570
+ if (isPromiseLike(iteration)) {
1571
+ throw new Error("Mixed generator component");
1572
+ }
1573
+ if (iteration.done) {
1574
+ ctx.f &= ~IsSyncGen;
1575
+ ctx.iterator = undefined;
1576
+ }
1577
+ let value;
1578
+ try {
1579
+ value = updateComponentChildren(ctx,
1580
+ // Children can be void so we eliminate that here
1581
+ iteration.value, hydrationData);
1582
+ if (isPromiseLike(value)) {
1583
+ value = value.catch((err) => handleChildError(ctx, err));
1584
+ }
1585
+ }
1586
+ catch (err) {
1587
+ value = handleChildError(ctx, err);
1588
+ }
1589
+ const block = isPromiseLike(value) ? value.catch(NOOP) : undefined;
1590
+ return [block, value];
1591
+ }
1592
+ else if (ctx.f & IsInForOfLoop) {
1593
+ // TODO: does this need to be done async?
1594
+ ctx.f &= ~NeedsToYield;
1595
+ // we are in a for...of loop for async generator
1596
+ if (!initial) {
1597
+ try {
1598
+ ctx.f |= IsSyncExecuting;
1599
+ iteration = ctx.iterator.next(ctx.renderer.read(getValue(ret)));
1600
+ }
1601
+ catch (err) {
1602
+ ctx.f |= IsErrored;
1603
+ throw err;
1604
+ }
1605
+ finally {
1606
+ ctx.f &= ~IsSyncExecuting;
1607
+ }
1608
+ }
1609
+ if (!isPromiseLike(iteration)) {
1610
+ throw new Error("Mixed generator component");
1611
+ }
1612
+ const block = iteration.catch(NOOP);
1613
+ const value = iteration.then((iteration) => {
1614
+ let value;
1615
+ if (!(ctx.f & IsInForOfLoop)) {
1616
+ runAsyncGenComponent(ctx, Promise.resolve(iteration), hydrationData);
1617
+ }
1618
+ try {
1619
+ value = updateComponentChildren(ctx,
1620
+ // Children can be void so we eliminate that here
1621
+ iteration.value, hydrationData);
1622
+ if (isPromiseLike(value)) {
1623
+ value = value.catch((err) => handleChildError(ctx, err));
1624
+ }
1625
+ }
1626
+ catch (err) {
1627
+ value = handleChildError(ctx, err);
1628
+ }
1629
+ return value;
1630
+ }, (err) => {
1631
+ ctx.f |= IsErrored;
1632
+ throw err;
1633
+ });
1634
+ return [block, value];
1635
+ }
1636
+ else {
1637
+ runAsyncGenComponent(ctx, iteration, hydrationData);
1638
+ // async generator component
1639
+ return [ctx.inflightBlock, ctx.inflightValue];
1640
+ }
1641
+ }
1642
+ async function runAsyncGenComponent(ctx, iterationP, hydrationData) {
1643
+ let done = false;
1644
+ try {
1645
+ while (!done) {
1646
+ if (ctx.f & IsInForOfLoop) {
1647
+ break;
1648
+ }
1649
+ // inflightValue must be set synchronously.
1650
+ let onValue;
1651
+ ctx.inflightValue = new Promise((resolve) => (onValue = resolve));
1652
+ if (ctx.f & IsUpdating) {
1653
+ // We should not swallow unhandled promise rejections if the component is
1654
+ // updating independently.
1655
+ // TODO: Does this handle this.refresh() calls?
1656
+ ctx.inflightValue.catch(NOOP);
1657
+ }
1658
+ let iteration;
1659
+ try {
1660
+ iteration = await iterationP;
1661
+ }
1662
+ catch (err) {
1663
+ done = true;
1664
+ ctx.f |= IsErrored;
1665
+ onValue(Promise.reject(err));
1666
+ break;
1667
+ }
1668
+ finally {
1669
+ ctx.f &= ~NeedsToYield;
1670
+ if (!(ctx.f & IsInForAwaitOfLoop)) {
1671
+ ctx.f &= ~PropsAvailable;
1672
+ }
1673
+ }
1674
+ done = !!iteration.done;
1675
+ let value;
1676
+ try {
1677
+ value = updateComponentChildren(ctx, iteration.value, hydrationData);
1678
+ hydrationData = undefined;
1679
+ if (isPromiseLike(value)) {
1680
+ value = value.catch((err) => handleChildError(ctx, err));
1681
+ }
1682
+ }
1683
+ catch (err) {
1684
+ // Do we need to catch potential errors here in the case of unhandled
1685
+ // promise rejections?
1686
+ value = handleChildError(ctx, err);
1687
+ }
1688
+ finally {
1689
+ onValue(value);
1690
+ }
1691
+ // TODO: this can be done more elegantly
1692
+ let oldValue;
1693
+ if (ctx.ret.inflightValue) {
1694
+ // The value passed back into the generator as the argument to the next
1695
+ // method is a promise if an async generator component has async
1696
+ // children. Sync generator components only resume when their children
1697
+ // have fulfilled so the element’s inflight child values will never be
1698
+ // defined.
1699
+ oldValue = ctx.ret.inflightValue.then((value) => ctx.renderer.read(value), () => ctx.renderer.read(undefined));
1700
+ }
1701
+ else {
1702
+ oldValue = ctx.renderer.read(getValue(ctx.ret));
1703
+ }
1704
+ if (ctx.f & IsUnmounted) {
1705
+ if (ctx.f & IsInForAwaitOfLoop) {
1706
+ try {
1707
+ ctx.f |= IsSyncExecuting;
1708
+ iterationP = ctx.iterator.next(oldValue);
1709
+ }
1710
+ finally {
1711
+ ctx.f &= ~IsSyncExecuting;
1712
+ }
1713
+ }
1714
+ else {
1715
+ returnComponent(ctx);
1716
+ break;
1717
+ }
1718
+ }
1719
+ else if (!done && !(ctx.f & IsInForOfLoop)) {
1720
+ try {
1721
+ ctx.f |= IsSyncExecuting;
1722
+ iterationP = ctx.iterator.next(oldValue);
1723
+ }
1724
+ finally {
1725
+ ctx.f &= ~IsSyncExecuting;
1726
+ }
1727
+ }
1728
+ }
1729
+ }
1730
+ finally {
1731
+ if (done) {
1732
+ ctx.f &= ~IsAsyncGen;
1733
+ ctx.iterator = undefined;
1734
+ }
1735
+ }
1736
+ }
1737
+ /**
1738
+ * Called to resume the props async iterator for async generator components.
1739
+ */
1740
+ function resumePropsIterator(ctx) {
1741
+ if (ctx.onProps) {
1742
+ ctx.onProps(ctx.ret.el.props);
1743
+ ctx.onProps = undefined;
1744
+ ctx.f &= ~PropsAvailable;
1745
+ }
1746
+ else {
1747
+ ctx.f |= PropsAvailable;
1748
+ }
1749
+ }
1750
+ // TODO: async unmounting
1751
+ function unmountComponent(ctx) {
1752
+ clearEventListeners(ctx);
1753
+ const callbacks = cleanupMap.get(ctx);
1754
+ if (callbacks) {
1755
+ cleanupMap.delete(ctx);
1756
+ const value = ctx.renderer.read(getValue(ctx.ret));
1757
+ for (const callback of callbacks) {
1758
+ callback(value);
1759
+ }
1760
+ }
1761
+ ctx.f |= IsUnmounted;
1762
+ if (ctx.iterator) {
1763
+ if (ctx.f & IsSyncGen) {
1764
+ let value;
1765
+ if (ctx.f & IsInForOfLoop) {
1766
+ value = enqueueComponentRun(ctx);
1767
+ }
1768
+ if (isPromiseLike(value)) {
1769
+ value.then(() => {
1770
+ if (ctx.f & IsInForOfLoop) {
1771
+ unmountComponent(ctx);
1772
+ }
1773
+ else {
1774
+ returnComponent(ctx);
1775
+ }
1776
+ }, (err) => {
1777
+ propagateError(ctx.parent, err);
1778
+ });
1779
+ }
1780
+ else {
1781
+ if (ctx.f & IsInForOfLoop) {
1782
+ unmountComponent(ctx);
1783
+ }
1784
+ else {
1785
+ returnComponent(ctx);
1786
+ }
1787
+ }
1788
+ }
1789
+ else if (ctx.f & IsAsyncGen) {
1790
+ if (ctx.f & IsInForOfLoop) {
1791
+ const value = enqueueComponentRun(ctx);
1792
+ value.then(() => {
1793
+ if (ctx.f & IsInForOfLoop) {
1794
+ unmountComponent(ctx);
1795
+ }
1796
+ else {
1797
+ returnComponent(ctx);
1798
+ }
1799
+ }, (err) => {
1800
+ propagateError(ctx.parent, err);
1801
+ });
1802
+ }
1803
+ else {
1804
+ // The logic for unmounting async generator components is in the
1805
+ // runAsyncGenComponent function.
1806
+ resumePropsIterator(ctx);
1807
+ }
1808
+ }
1809
+ }
1810
+ }
1811
+ function returnComponent(ctx) {
1812
+ resumePropsIterator(ctx);
1813
+ if (ctx.iterator && typeof ctx.iterator.return === "function") {
1814
+ try {
1815
+ ctx.f |= IsSyncExecuting;
1816
+ const iteration = ctx.iterator.return();
1817
+ if (isPromiseLike(iteration)) {
1818
+ iteration.catch((err) => propagateError(ctx.parent, err));
1819
+ }
1820
+ }
1821
+ finally {
1822
+ ctx.f &= ~IsSyncExecuting;
1823
+ }
1824
+ }
1825
+ }
1826
+ /*** EVENT TARGET UTILITIES ***/
1827
+ // EVENT PHASE CONSTANTS
1828
+ // https://developer.mozilla.org/en-US/docs/Web/API/Event/eventPhase
1829
+ const NONE = 0;
1830
+ const CAPTURING_PHASE = 1;
1831
+ const AT_TARGET = 2;
1832
+ const BUBBLING_PHASE = 3;
1833
+ const listenersMap = new WeakMap();
1834
+ function isListenerOrListenerObject(value) {
1835
+ return (typeof value === "function" ||
1836
+ (value !== null &&
1837
+ typeof value === "object" &&
1838
+ typeof value.handleEvent === "function"));
1839
+ }
1840
+ function normalizeListenerOptions(options) {
1841
+ if (typeof options === "boolean") {
1842
+ return { capture: options };
1843
+ }
1844
+ else if (options == null) {
1845
+ return {};
1846
+ }
1847
+ return options;
1848
+ }
1849
+ function isEventTarget(value) {
1850
+ return (value != null &&
1851
+ typeof value.addEventListener === "function" &&
1852
+ typeof value.removeEventListener === "function" &&
1853
+ typeof value.dispatchEvent === "function");
1854
+ }
1855
+ function setEventProperty(ev, key, value) {
1856
+ Object.defineProperty(ev, key, { value, writable: false, configurable: true });
1857
+ }
1858
+ // TODO: Maybe we can pass in the current context directly, rather than
1859
+ // starting from the parent?
1860
+ /**
1861
+ * A function to reconstruct an array of every listener given a context and a
1862
+ * host element.
1863
+ *
1864
+ * This function exploits the fact that contexts retain their nearest ancestor
1865
+ * host element. We can determine all the contexts which are directly listening
1866
+ * to an element by traversing up the context tree and checking that the host
1867
+ * element passed in matches the parent context’s host element.
1868
+ */
1869
+ function getListenerRecords(ctx, ret) {
1870
+ let listeners = [];
1871
+ while (ctx !== undefined && ctx.host === ret) {
1872
+ const listeners1 = listenersMap.get(ctx);
1873
+ if (listeners1) {
1874
+ listeners = listeners.concat(listeners1);
1875
+ }
1876
+ ctx = ctx.parent;
1877
+ }
1878
+ return listeners;
1879
+ }
1880
+ function clearEventListeners(ctx) {
1881
+ const listeners = listenersMap.get(ctx);
1882
+ if (listeners && listeners.length) {
1883
+ for (const value of getChildValues(ctx.ret)) {
1884
+ if (isEventTarget(value)) {
1885
+ for (const record of listeners) {
1886
+ value.removeEventListener(record.type, record.callback, record.options);
1887
+ }
1888
+ }
1889
+ }
1890
+ listeners.length = 0;
1891
+ }
1892
+ }
1893
+ /*** ERROR HANDLING UTILITIES ***/
1894
+ function handleChildError(ctx, err) {
1895
+ if (!ctx.iterator || typeof ctx.iterator.throw !== "function") {
1896
+ throw err;
1897
+ }
1898
+ resumePropsIterator(ctx);
1899
+ let iteration;
1900
+ try {
1901
+ ctx.f |= IsSyncExecuting;
1902
+ iteration = ctx.iterator.throw(err);
1903
+ }
1904
+ catch (err) {
1905
+ ctx.f |= IsErrored;
1906
+ throw err;
1907
+ }
1908
+ finally {
1909
+ ctx.f &= ~IsSyncExecuting;
1910
+ }
1911
+ if (isPromiseLike(iteration)) {
1912
+ return iteration.then((iteration) => {
1913
+ if (iteration.done) {
1914
+ ctx.f &= ~IsAsyncGen;
1915
+ ctx.iterator = undefined;
1916
+ }
1917
+ return updateComponentChildren(ctx, iteration.value);
1918
+ }, (err) => {
1919
+ ctx.f |= IsErrored;
1920
+ throw err;
1921
+ });
1922
+ }
1923
+ if (iteration.done) {
1924
+ ctx.f &= ~IsSyncGen;
1925
+ ctx.f &= ~IsAsyncGen;
1926
+ ctx.iterator = undefined;
1927
+ }
1928
+ return updateComponentChildren(ctx, iteration.value);
1929
+ }
1930
+ function propagateError(ctx, err) {
1931
+ if (ctx === undefined) {
1932
+ throw err;
1933
+ }
1934
+ let result;
1935
+ try {
1936
+ result = handleChildError(ctx, err);
1937
+ }
1938
+ catch (err) {
1939
+ return propagateError(ctx.parent, err);
1940
+ }
1941
+ if (isPromiseLike(result)) {
1942
+ return result.catch((err) => propagateError(ctx.parent, err));
1943
+ }
1944
+ return result;
1945
+ }
1946
+ // Some JSX transpilation tools expect these functions to be defined on the
1947
+ // default export. Prefer named exports when importing directly.
1948
+ var crank = { createElement, Fragment };
7
1949
 
8
-
9
-
10
- exports.Context = core.Context;
11
- exports.Copy = core.Copy;
12
- exports.Element = core.Element;
13
- exports.Fragment = core.Fragment;
14
- exports.Portal = core.Portal;
15
- exports.Raw = core.Raw;
16
- exports.Renderer = core.Renderer;
17
- exports.cloneElement = core.cloneElement;
18
- exports.createElement = core.createElement;
19
- exports.isElement = core.isElement;
20
- exports.jsx = tags.jsx;
1950
+ exports.Context = Context;
1951
+ exports.Copy = Copy;
1952
+ exports.Element = Element;
1953
+ exports.Fragment = Fragment;
1954
+ exports.Portal = Portal;
1955
+ exports.Raw = Raw;
1956
+ exports.Renderer = Renderer;
1957
+ exports.cloneElement = cloneElement;
1958
+ exports.createElement = createElement;
1959
+ exports.default = crank;
1960
+ exports.isElement = isElement;
21
1961
  //# sourceMappingURL=crank.cjs.map