@b9g/crank 0.5.1 → 0.5.3

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.d.ts CHANGED
@@ -377,20 +377,20 @@ declare class ContextImpl<TNode = unknown, TScope = unknown, TRoot extends TNode
377
377
  constructor(renderer: RendererImpl<TNode, TScope, TRoot, TResult>, root: TRoot | undefined, host: Retainer<TNode>, parent: ContextImpl<TNode, TScope, TRoot, TResult> | undefined, scope: TScope | undefined, ret: Retainer<TNode>);
378
378
  }
379
379
  declare const _ContextImpl: unique symbol;
380
- type ComponentProps<T> = T extends (props: infer U) => any ? U : T;
380
+ type ComponentProps<T> = T extends () => any ? {} : T extends (props: infer U) => any ? U : T;
381
381
  /**
382
382
  * A class which is instantiated and passed to every component as its this
383
383
  * value. Contexts form a tree just like elements and all components in the
384
384
  * element tree are connected via contexts. Components can use this tree to
385
385
  * communicate data upwards via events and downwards via provisions.
386
386
  *
387
- * @template [TProps=*] - The expected shape of the props passed to the
388
- * component. Used to strongly type the Context iterator methods.
387
+ * @template [T=*] - The expected shape of the props passed to the component,
388
+ * or a component function. Used to strongly type the Context iterator methods.
389
389
  * @template [TResult=*] - The readable element value type. It is used in
390
390
  * places such as the return value of refresh and the argument passed to
391
391
  * schedule and cleanup callbacks.
392
392
  */
393
- export declare class Context<TProps = any, TResult = any> implements EventTarget {
393
+ export declare class Context<T = any, TResult = any> implements EventTarget {
394
394
  /**
395
395
  * @internal
396
396
  */
@@ -403,7 +403,7 @@ export declare class Context<TProps = any, TResult = any> implements EventTarget
403
403
  * component or via the context iterator methods. This property is mainly for
404
404
  * plugins or utilities which wrap contexts.
405
405
  */
406
- get props(): ComponentProps<TProps>;
406
+ get props(): ComponentProps<T>;
407
407
  /**
408
408
  * The current value of the associated element.
409
409
  *
@@ -412,8 +412,8 @@ export declare class Context<TProps = any, TResult = any> implements EventTarget
412
412
  * mainly for plugins or utilities which wrap contexts.
413
413
  */
414
414
  get value(): TResult;
415
- [Symbol.iterator](): Generator<ComponentProps<TProps>>;
416
- [Symbol.asyncIterator](): AsyncGenerator<ComponentProps<TProps>>;
415
+ [Symbol.iterator](): Generator<ComponentProps<T>>;
416
+ [Symbol.asyncIterator](): AsyncGenerator<ComponentProps<T>>;
417
417
  /**
418
418
  * Re-executes a component.
419
419
  *
package/crank.js CHANGED
@@ -859,8 +859,8 @@ const _ContextImpl = Symbol.for("crank.ContextImpl");
859
859
  * element tree are connected via contexts. Components can use this tree to
860
860
  * communicate data upwards via events and downwards via provisions.
861
861
  *
862
- * @template [TProps=*] - The expected shape of the props passed to the
863
- * component. Used to strongly type the Context iterator methods.
862
+ * @template [T=*] - The expected shape of the props passed to the component,
863
+ * or a component function. Used to strongly type the Context iterator methods.
864
864
  * @template [TResult=*] - The readable element value type. It is used in
865
865
  * places such as the return value of refresh and the argument passed to
866
866
  * schedule and cleanup callbacks.
@@ -1017,6 +1017,11 @@ class Context {
1017
1017
  */
1018
1018
  cleanup(callback) {
1019
1019
  const ctx = this[_ContextImpl];
1020
+ if (ctx.f & IsUnmounted) {
1021
+ const value = ctx.renderer.read(getValue(ctx.ret));
1022
+ callback(value);
1023
+ return;
1024
+ }
1020
1025
  let callbacks = cleanupMap.get(ctx);
1021
1026
  if (!callbacks) {
1022
1027
  callbacks = new Set();
@@ -1427,6 +1432,9 @@ function enqueueComponentRun(ctx, hydrationData) {
1427
1432
  }
1428
1433
  catch (err) {
1429
1434
  if (!(ctx.f & IsUpdating)) {
1435
+ if (!ctx.parent) {
1436
+ throw err;
1437
+ }
1430
1438
  return propagateError(ctx.parent, err);
1431
1439
  }
1432
1440
  throw err;
@@ -1450,6 +1458,9 @@ function enqueueComponentRun(ctx, hydrationData) {
1450
1458
  }
1451
1459
  catch (err) {
1452
1460
  if (!(ctx.f & IsUpdating)) {
1461
+ if (!ctx.parent) {
1462
+ throw err;
1463
+ }
1453
1464
  return propagateError(ctx.parent, err);
1454
1465
  }
1455
1466
  throw err;
@@ -1692,7 +1703,16 @@ async function runAsyncGenComponent(ctx, iterationP, hydrationData) {
1692
1703
  // children. Sync generator components only resume when their children
1693
1704
  // have fulfilled so the element’s inflight child values will never be
1694
1705
  // defined.
1695
- oldValue = ctx.ret.inflightValue.then((value) => ctx.renderer.read(value), () => ctx.renderer.read(undefined));
1706
+ oldValue = ctx.ret.inflightValue.then((value) => ctx.renderer.read(value));
1707
+ oldValue.catch((err) => {
1708
+ if (ctx.f & IsUpdating) {
1709
+ return;
1710
+ }
1711
+ if (!ctx.parent) {
1712
+ throw err;
1713
+ }
1714
+ return propagateError(ctx.parent, err);
1715
+ });
1696
1716
  }
1697
1717
  else {
1698
1718
  oldValue = ctx.renderer.read(getValue(ctx.ret));
@@ -1770,7 +1790,10 @@ function unmountComponent(ctx) {
1770
1790
  returnComponent(ctx);
1771
1791
  }
1772
1792
  }, (err) => {
1773
- propagateError(ctx.parent, err);
1793
+ if (!ctx.parent) {
1794
+ throw err;
1795
+ }
1796
+ return propagateError(ctx.parent, err);
1774
1797
  });
1775
1798
  }
1776
1799
  else {
@@ -1793,7 +1816,10 @@ function unmountComponent(ctx) {
1793
1816
  returnComponent(ctx);
1794
1817
  }
1795
1818
  }, (err) => {
1796
- propagateError(ctx.parent, err);
1819
+ if (!ctx.parent) {
1820
+ throw err;
1821
+ }
1822
+ return propagateError(ctx.parent, err);
1797
1823
  });
1798
1824
  }
1799
1825
  else {
@@ -1811,7 +1837,12 @@ function returnComponent(ctx) {
1811
1837
  ctx.f |= IsSyncExecuting;
1812
1838
  const iteration = ctx.iterator.return();
1813
1839
  if (isPromiseLike(iteration)) {
1814
- iteration.catch((err) => propagateError(ctx.parent, err));
1840
+ iteration.catch((err) => {
1841
+ if (!ctx.parent) {
1842
+ throw err;
1843
+ }
1844
+ return propagateError(ctx.parent, err);
1845
+ });
1815
1846
  }
1816
1847
  }
1817
1848
  finally {
@@ -1924,18 +1955,23 @@ function handleChildError(ctx, err) {
1924
1955
  return updateComponentChildren(ctx, iteration.value);
1925
1956
  }
1926
1957
  function propagateError(ctx, err) {
1927
- if (ctx === undefined) {
1928
- throw err;
1929
- }
1930
1958
  let result;
1931
1959
  try {
1932
1960
  result = handleChildError(ctx, err);
1933
1961
  }
1934
1962
  catch (err) {
1963
+ if (!ctx.parent) {
1964
+ throw err;
1965
+ }
1935
1966
  return propagateError(ctx.parent, err);
1936
1967
  }
1937
1968
  if (isPromiseLike(result)) {
1938
- return result.catch((err) => propagateError(ctx.parent, err));
1969
+ return result.catch((err) => {
1970
+ if (!ctx.parent) {
1971
+ throw err;
1972
+ }
1973
+ return propagateError(ctx.parent, err);
1974
+ });
1939
1975
  }
1940
1976
  return result;
1941
1977
  }