@b9g/crank 0.7.0 → 0.7.1

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
@@ -3,103 +3,7 @@
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var eventTarget = require('./event-target.cjs');
6
-
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
- : [...value];
29
- }
30
- function isIteratorLike(value) {
31
- return value != null && typeof value.next === "function";
32
- }
33
- function isPromiseLike(value) {
34
- return value != null && typeof value.then === "function";
35
- }
36
- function createRaceRecord(contender) {
37
- const deferreds = new Set();
38
- const record = { deferreds, settled: false };
39
- // This call to `then` happens once for the lifetime of the value.
40
- Promise.resolve(contender).then((value) => {
41
- for (const { resolve } of deferreds) {
42
- resolve(value);
43
- }
44
- deferreds.clear();
45
- record.settled = true;
46
- }, (err) => {
47
- for (const { reject } of deferreds) {
48
- reject(err);
49
- }
50
- deferreds.clear();
51
- record.settled = true;
52
- });
53
- return record;
54
- }
55
- // Promise.race is memory unsafe. This is alternative which is. See:
56
- // https://github.com/nodejs/node/issues/17469#issuecomment-685235106
57
- // Keys are the values passed to race.
58
- // Values are a record of data containing a set of deferreds and whether the
59
- // value has settled.
60
- const wm = new WeakMap();
61
- function safeRace(contenders) {
62
- let deferred;
63
- const result = new Promise((resolve, reject) => {
64
- deferred = { resolve, reject };
65
- for (const contender of contenders) {
66
- if (!isPromiseLike(contender)) {
67
- // If the contender is a not a then-able, attempting to use it as a key
68
- // in the weakmap would throw an error. Luckily, it is safe to call
69
- // `Promise.resolve(contender).then` on regular values multiple
70
- // times because the promise fulfills immediately.
71
- Promise.resolve(contender).then(resolve, reject);
72
- continue;
73
- }
74
- let record = wm.get(contender);
75
- if (record === undefined) {
76
- record = createRaceRecord(contender);
77
- record.deferreds.add(deferred);
78
- wm.set(contender, record);
79
- }
80
- else if (record.settled) {
81
- // If the value has settled, it is safe to call
82
- // `Promise.resolve(contender).then` on it.
83
- Promise.resolve(contender).then(resolve, reject);
84
- }
85
- else {
86
- record.deferreds.add(deferred);
87
- }
88
- }
89
- });
90
- // The finally callback executes when any value settles, preventing any of
91
- // the unresolved values from retaining a reference to the resolved value.
92
- return result.finally(() => {
93
- for (const contender of contenders) {
94
- if (isPromiseLike(contender)) {
95
- const record = wm.get(contender);
96
- if (record) {
97
- record.deferreds.delete(deferred);
98
- }
99
- }
100
- }
101
- });
102
- }
6
+ var _utils = require('./_utils.cjs');
103
7
 
104
8
  const NOOP = () => { };
105
9
  function getTagName(tag) {
@@ -331,7 +235,7 @@ function getValue(ret, isNested = false, index) {
331
235
  if (index != null && ret.ctx) {
332
236
  ret.ctx.index = index;
333
237
  }
334
- return unwrap(getChildValues(ret, index));
238
+ return _utils.unwrap(getChildValues(ret, index));
335
239
  }
336
240
  return ret.value;
337
241
  }
@@ -346,7 +250,7 @@ function getValue(ret, isNested = false, index) {
346
250
  function getChildValues(ret, startIndex) {
347
251
  const values = [];
348
252
  const lingerers = ret.lingerers;
349
- const children = wrap(ret.children);
253
+ const children = _utils.wrap(ret.children);
350
254
  let currentIndex = startIndex;
351
255
  for (let i = 0; i < children.length; i++) {
352
256
  if (lingerers != null && lingerers[i] != null) {
@@ -516,7 +420,7 @@ function getRootRetainer(renderer, bridge, { children, root, hydrate, }) {
516
420
  function renderRoot(adapter, root, ret, children) {
517
421
  const diff = diffChildren(adapter, root, ret, ret.ctx, ret.scope, ret, children);
518
422
  const schedulePromises = [];
519
- if (isPromiseLike(diff)) {
423
+ if (_utils.isPromiseLike(diff)) {
520
424
  return diff.then(() => {
521
425
  commit(adapter, ret, ret, ret.ctx, ret.scope, 0, schedulePromises, undefined);
522
426
  if (schedulePromises.length > 0) {
@@ -524,13 +428,13 @@ function renderRoot(adapter, root, ret, children) {
524
428
  if (typeof root !== "object" || root === null) {
525
429
  unmount(adapter, ret, ret.ctx, ret, false);
526
430
  }
527
- return adapter.read(unwrap(getChildValues(ret)));
431
+ return adapter.read(_utils.unwrap(getChildValues(ret)));
528
432
  });
529
433
  }
530
434
  if (typeof root !== "object" || root === null) {
531
435
  unmount(adapter, ret, ret.ctx, ret, false);
532
436
  }
533
- return adapter.read(unwrap(getChildValues(ret)));
437
+ return adapter.read(_utils.unwrap(getChildValues(ret)));
534
438
  });
535
439
  }
536
440
  commit(adapter, ret, ret, ret.ctx, ret.scope, 0, schedulePromises, undefined);
@@ -539,18 +443,18 @@ function renderRoot(adapter, root, ret, children) {
539
443
  if (typeof root !== "object" || root === null) {
540
444
  unmount(adapter, ret, ret.ctx, ret, false);
541
445
  }
542
- return adapter.read(unwrap(getChildValues(ret)));
446
+ return adapter.read(_utils.unwrap(getChildValues(ret)));
543
447
  });
544
448
  }
545
449
  if (typeof root !== "object" || root === null) {
546
450
  unmount(adapter, ret, ret.ctx, ret, false);
547
451
  }
548
- return adapter.read(unwrap(getChildValues(ret)));
452
+ return adapter.read(_utils.unwrap(getChildValues(ret)));
549
453
  }
550
454
  function diffChildren(adapter, root, host, ctx, scope, parent, newChildren) {
551
- const oldRetained = wrap(parent.children);
455
+ const oldRetained = _utils.wrap(parent.children);
552
456
  const newRetained = [];
553
- const newChildren1 = arrayify(newChildren);
457
+ const newChildren1 = _utils.arrayify(newChildren);
554
458
  const diffs = [];
555
459
  let childrenByKey;
556
460
  let seenKeys;
@@ -669,7 +573,7 @@ function diffChildren(adapter, root, host, ctx, scope, parent, newChildren) {
669
573
  setFlag(ret, IsCopied, false);
670
574
  }
671
575
  }
672
- if (isPromiseLike(diff)) {
576
+ if (_utils.isPromiseLike(diff)) {
673
577
  isAsync = true;
674
578
  }
675
579
  }
@@ -709,7 +613,7 @@ function diffChildren(adapter, root, host, ctx, scope, parent, newChildren) {
709
613
  graveyard.push(ret);
710
614
  }
711
615
  }
712
- parent.children = unwrap(newRetained);
616
+ parent.children = _utils.unwrap(newRetained);
713
617
  if (isAsync) {
714
618
  const diffs1 = Promise.all(diffs)
715
619
  .then(() => undefined)
@@ -727,7 +631,7 @@ function diffChildren(adapter, root, host, ctx, scope, parent, newChildren) {
727
631
  }
728
632
  });
729
633
  let onNextDiffs;
730
- const diffs2 = (parent.pendingDiff = safeRace([
634
+ const diffs2 = (parent.pendingDiff = _utils.safeRace([
731
635
  diffs1,
732
636
  new Promise((resolve) => (onNextDiffs = resolve)),
733
637
  ]));
@@ -847,7 +751,7 @@ function commit(adapter, host, ret, ctx, scope, index, schedulePromises, hydrati
847
751
  }
848
752
  }
849
753
  if (skippedHydrationNodes) {
850
- skippedHydrationNodes.splice(0, wrap(value).length);
754
+ skippedHydrationNodes.splice(0, _utils.wrap(value).length);
851
755
  }
852
756
  if (!getFlag(ret, DidCommit)) {
853
757
  setFlag(ret, DidCommit);
@@ -862,7 +766,7 @@ function commit(adapter, host, ret, ctx, scope, index, schedulePromises, hydrati
862
766
  }
863
767
  function commitChildren(adapter, host, ctx, scope, parent, index, schedulePromises, hydrationNodes) {
864
768
  let values = [];
865
- for (let i = 0, children = wrap(parent.children); i < children.length; i++) {
769
+ for (let i = 0, children = _utils.wrap(parent.children); i < children.length; i++) {
866
770
  let child = children[i];
867
771
  let schedulePromises1;
868
772
  let isSchedulingFallback = false;
@@ -939,7 +843,7 @@ function commitChildren(adapter, host, ctx, scope, parent, index, schedulePromis
939
843
  }
940
844
  }
941
845
  if (schedulePromises1 && schedulePromises1.length > 1) {
942
- schedulePromises.push(safeRace(schedulePromises1));
846
+ schedulePromises.push(_utils.safeRace(schedulePromises1));
943
847
  }
944
848
  if (child) {
945
849
  const value = commit(adapter, host, child, ctx, scope, index, schedulePromises, hydrationNodes);
@@ -981,7 +885,7 @@ function commitText(adapter, ret, el, scope, hydrationNodes) {
981
885
  }
982
886
  function commitRaw(adapter, host, ret, scope, hydrationNodes) {
983
887
  if (!ret.oldProps || ret.oldProps.value !== ret.el.props.value) {
984
- const oldNodes = wrap(ret.value);
888
+ const oldNodes = _utils.wrap(ret.value);
985
889
  for (let i = 0; i < oldNodes.length; i++) {
986
890
  const oldNode = oldNodes[i];
987
891
  adapter.remove({
@@ -1293,7 +1197,7 @@ function unmountChildren(adapter, host, ctx, ret, isNested) {
1293
1197
  }
1294
1198
  ret.graveyard = undefined;
1295
1199
  }
1296
- for (let i = 0, children = wrap(ret.children); i < children.length; i++) {
1200
+ for (let i = 0, children = _utils.wrap(ret.children); i < children.length; i++) {
1297
1201
  const child = children[i];
1298
1202
  if (typeof child === "object") {
1299
1203
  unmount(adapter, host, ctx, child, isNested);
@@ -1446,7 +1350,7 @@ class Context extends eventTarget.CustomEventTarget {
1446
1350
  }
1447
1351
  if (callback) {
1448
1352
  const result = callback();
1449
- if (isPromiseLike(result)) {
1353
+ if (_utils.isPromiseLike(result)) {
1450
1354
  return Promise.resolve(result).then(() => {
1451
1355
  if (!getFlag(ctx.ret, IsUnmounted)) {
1452
1356
  return this.refresh();
@@ -1460,7 +1364,7 @@ class Context extends eventTarget.CustomEventTarget {
1460
1364
  try {
1461
1365
  setFlag(ctx.ret, IsRefreshing);
1462
1366
  diff = enqueueComponent(ctx);
1463
- if (isPromiseLike(diff)) {
1367
+ if (_utils.isPromiseLike(diff)) {
1464
1368
  return diff
1465
1369
  .then(() => ctx.adapter.read(commitComponent(ctx, schedulePromises)))
1466
1370
  .then((result) => {
@@ -1522,7 +1426,7 @@ class Context extends eventTarget.CustomEventTarget {
1522
1426
  return ctx.adapter.read(getValue(ctx.ret));
1523
1427
  }
1524
1428
  finally {
1525
- if (!isPromiseLike(diff)) {
1429
+ if (!_utils.isPromiseLike(diff)) {
1526
1430
  setFlag(ctx.ret, IsRefreshing, false);
1527
1431
  }
1528
1432
  }
@@ -1736,10 +1640,10 @@ function runComponent(ctx) {
1736
1640
  finally {
1737
1641
  setFlag(ctx.ret, IsExecuting, false);
1738
1642
  }
1739
- if (isIteratorLike(returned)) {
1643
+ if (_utils.isIteratorLike(returned)) {
1740
1644
  ctx.iterator = returned;
1741
1645
  }
1742
- else if (!isPromiseLike(returned)) {
1646
+ else if (!_utils.isPromiseLike(returned)) {
1743
1647
  // sync function component
1744
1648
  return [
1745
1649
  undefined,
@@ -1771,7 +1675,7 @@ function runComponent(ctx) {
1771
1675
  finally {
1772
1676
  setFlag(ctx.ret, IsExecuting, false);
1773
1677
  }
1774
- if (isPromiseLike(iteration)) {
1678
+ if (_utils.isPromiseLike(iteration)) {
1775
1679
  setFlag(ctx.ret, IsAsyncGen);
1776
1680
  }
1777
1681
  else {
@@ -1794,7 +1698,7 @@ function runComponent(ctx) {
1794
1698
  setFlag(ctx.ret, IsExecuting, false);
1795
1699
  }
1796
1700
  }
1797
- if (isPromiseLike(iteration)) {
1701
+ if (_utils.isPromiseLike(iteration)) {
1798
1702
  throw new Error("Mixed generator component");
1799
1703
  }
1800
1704
  if (getFlag(ctx.ret, IsInForOfLoop) &&
@@ -1809,7 +1713,7 @@ function runComponent(ctx) {
1809
1713
  ctx.iterator = undefined;
1810
1714
  }
1811
1715
  const diff = diffComponentChildren(ctx, iteration.value, !iteration.done);
1812
- const block = isPromiseLike(diff) ? diff.catch(NOOP) : undefined;
1716
+ const block = _utils.isPromiseLike(diff) ? diff.catch(NOOP) : undefined;
1813
1717
  return [block, diff];
1814
1718
  }
1815
1719
  else {
@@ -1837,7 +1741,7 @@ function runComponent(ctx) {
1837
1741
  setFlag(ctx.ret, IsExecuting, false);
1838
1742
  }
1839
1743
  }
1840
- if (!isPromiseLike(iteration)) {
1744
+ if (!_utils.isPromiseLike(iteration)) {
1841
1745
  throw new Error("Mixed generator component");
1842
1746
  }
1843
1747
  const diff = iteration.then((iteration) => {
@@ -1907,7 +1811,7 @@ async function pullComponent(ctx, iterationP) {
1907
1811
  try {
1908
1812
  let childError;
1909
1813
  while (!done) {
1910
- if (isPromiseLike(iterationP)) {
1814
+ if (_utils.isPromiseLike(iterationP)) {
1911
1815
  ctx.pull.iterationP = iterationP;
1912
1816
  }
1913
1817
  let onDiff;
@@ -1994,7 +1898,7 @@ async function pullComponent(ctx, iterationP) {
1994
1898
  done = !!iteration.done;
1995
1899
  let diff;
1996
1900
  try {
1997
- if (!isPromiseLike(iterationP)) {
1901
+ if (!_utils.isPromiseLike(iterationP)) {
1998
1902
  // if iterationP is an iteration and not a promise, the component was
1999
1903
  // not in a for await...of loop when the iteration started, so we can
2000
1904
  // skip the diffing of children as it is handled elsewhere.
@@ -2098,10 +2002,10 @@ function commitComponent(ctx, schedulePromises, hydrationNodes) {
2098
2002
  scheduleMap.delete(ctx);
2099
2003
  // TODO: think about error handling for schedule callbacks
2100
2004
  setFlag(ctx.ret, IsScheduling);
2101
- const result = ctx.adapter.read(unwrap(values));
2005
+ const result = ctx.adapter.read(_utils.unwrap(values));
2102
2006
  for (const callback of callbacks) {
2103
2007
  const scheduleResult = callback(result);
2104
- if (isPromiseLike(scheduleResult)) {
2008
+ if (_utils.isPromiseLike(scheduleResult)) {
2105
2009
  (schedulePromises1 = schedulePromises1 || []).push(scheduleResult);
2106
2010
  }
2107
2011
  }
@@ -2115,7 +2019,7 @@ function commitComponent(ctx, schedulePromises, hydrationNodes) {
2115
2019
  ctx.ret.fallback = undefined;
2116
2020
  });
2117
2021
  let onAbort;
2118
- const scheduleP = safeRace([
2022
+ const scheduleP = _utils.safeRace([
2119
2023
  scheduleCallbacksP,
2120
2024
  new Promise((resolve) => (onAbort = resolve)),
2121
2025
  ]).finally(() => {
@@ -2180,7 +2084,7 @@ async function unmountComponent(ctx, isNested) {
2180
2084
  cleanupMap.delete(ctx);
2181
2085
  for (const callback of callbacks) {
2182
2086
  const cleanup = callback(oldResult);
2183
- if (isPromiseLike(cleanup)) {
2087
+ if (_utils.isPromiseLike(cleanup)) {
2184
2088
  (cleanupPromises = cleanupPromises || []).push(cleanup);
2185
2089
  }
2186
2090
  }
@@ -2243,7 +2147,7 @@ async function unmountComponent(ctx, isNested) {
2243
2147
  setFlag(ctx.ret, IsExecuting);
2244
2148
  const oldResult = ctx.adapter.read(getValue(ctx.ret));
2245
2149
  const iterationP = ctx.iterator.next(oldResult);
2246
- if (isPromiseLike(iterationP)) {
2150
+ if (_utils.isPromiseLike(iterationP)) {
2247
2151
  if (!getFlag(ctx.ret, IsAsyncGen)) {
2248
2152
  throw new Error("Mixed generator component");
2249
2153
  }
@@ -2270,7 +2174,7 @@ async function unmountComponent(ctx, isNested) {
2270
2174
  try {
2271
2175
  setFlag(ctx.ret, IsExecuting);
2272
2176
  const iterationP = ctx.iterator.return();
2273
- if (isPromiseLike(iterationP)) {
2177
+ if (_utils.isPromiseLike(iterationP)) {
2274
2178
  if (!getFlag(ctx.ret, IsAsyncGen)) {
2275
2179
  throw new Error("Mixed generator component");
2276
2180
  }
@@ -2319,7 +2223,7 @@ function handleChildError(ctx, err) {
2319
2223
  finally {
2320
2224
  setFlag(ctx.ret, IsExecuting, false);
2321
2225
  }
2322
- if (isPromiseLike(iteration)) {
2226
+ if (_utils.isPromiseLike(iteration)) {
2323
2227
  return iteration.then((iteration) => {
2324
2228
  if (iteration.done) {
2325
2229
  setFlag(ctx.ret, IsSyncGen, false);
@@ -2358,7 +2262,7 @@ function propagateError(ctx, err, schedulePromises) {
2358
2262
  catch (err) {
2359
2263
  return propagateError(parent, err, schedulePromises);
2360
2264
  }
2361
- if (isPromiseLike(diff)) {
2265
+ if (_utils.isPromiseLike(diff)) {
2362
2266
  return diff.then(() => void commitComponent(parent, schedulePromises), (err) => propagateError(parent, err, schedulePromises));
2363
2267
  }
2364
2268
  commitComponent(parent, schedulePromises);