Skip to content

Commit

Permalink
Merge ff3436d into 1142fff
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewiggins committed Nov 2, 2019
2 parents 1142fff + ff3436d commit d9afb19
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 43 deletions.
7 changes: 4 additions & 3 deletions src/component.js
Expand Up @@ -165,9 +165,10 @@ let q = [];

/**
* Asynchronously schedule a callback
* @type {(cb) => void}
* @type {(cb: () => void) => void}
*/
/* istanbul ignore next */
// Note the following line isn't tree-shaken by rollup cuz of rollup/rollup#2566
const defer =
typeof Promise == 'function'
? Promise.prototype.then.bind(Promise.resolve())
Expand All @@ -182,7 +183,7 @@ const defer =
* * [Callbacks synchronous and asynchronous](https://blog.ometer.com/2011/07/24/callbacks-synchronous-and-asynchronous/)
*/

let prevDebounce = options.debounceRendering;
let prevDebounce;

/**
* Enqueue a rerender of a component
Expand All @@ -194,7 +195,7 @@ export function enqueueRender(c) {
prevDebounce !== options.debounceRendering
) {
prevDebounce = options.debounceRendering;
(options.debounceRendering || defer)(process);
(prevDebounce || defer)(process);
}
}

Expand Down
37 changes: 37 additions & 0 deletions src/diff/catch-error.js
@@ -0,0 +1,37 @@
import { enqueueRender } from '../component';

/**
* Find the closest error boundary to a thrown error and call it
* @param {object} error The thrown value
* @param {import('../internal').VNode} vnode The vnode that threw
* the error that was caught (except for unmounting when this parameter
* is the highest parent that was being unmounted)
*/
export function _catchError(error, vnode) {
/** @type {import('../internal').Component} */
let component;

for (; (vnode = vnode._parent); ) {
if ((component = vnode._component) && !component._processingException) {
try {
if (
component.constructor &&
component.constructor.getDerivedStateFromError != null
) {
component.setState(
component.constructor.getDerivedStateFromError(error)
);
} else if (component.componentDidCatch != null) {
component.componentDidCatch(error);
} else {
continue;
}
return enqueueRender((component._pendingError = component));
} catch (e) {
error = e;
}
}
}

throw error;
}
40 changes: 1 addition & 39 deletions src/diff/index.js
@@ -1,5 +1,5 @@
import { EMPTY_OBJ, EMPTY_ARR } from '../constants';
import { Component, enqueueRender } from '../component';
import { Component } from '../component';
import { Fragment } from '../create-element';
import { diffChildren, toChildArray } from './children';
import { diffProps } from './props';
Expand Down Expand Up @@ -441,41 +441,3 @@ export function unmount(vnode, parentVNode, skipRemove) {
function doRender(props, state, context) {
return this.constructor(props, context);
}

/**
* Find the closest error boundary to a thrown error and call it
* @param {object} error The thrown value
* @param {import('../internal').VNode} vnode The vnode that threw
* the error that was caught (except for unmounting when this parameter
* is the highest parent that was being unmounted)
* @param {import('../internal').VNode} oldVNode The oldVNode of the vnode
* that threw, if this VNode threw while diffing
*/
options._catchError = function(error, vnode, oldVNode) {
/** @type {import('../internal').Component} */
let component;

for (; (vnode = vnode._parent); ) {
if ((component = vnode._component) && !component._processingException) {
try {
if (
component.constructor &&
component.constructor.getDerivedStateFromError != null
) {
component.setState(
component.constructor.getDerivedStateFromError(error)
);
} else if (component.componentDidCatch != null) {
component.componentDidCatch(error);
} else {
continue;
}
return enqueueRender((component._pendingError = component));
} catch (e) {
error = e;
}
}
}

throw error;
};
6 changes: 5 additions & 1 deletion src/options.js
@@ -1,4 +1,8 @@
import { _catchError } from './diff/catch-error';

/** @type {import('./internal').Options} */
const options = {};
const options = {
_catchError
};

export default options;

0 comments on commit d9afb19

Please sign in to comment.