Skip to content

Commit

Permalink
Reuse renderCallbacks to schedule cDM and cDU (-19 B)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewiggins committed Oct 14, 2019
1 parent 9a31d8f commit 50dcead
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 11 deletions.
15 changes: 6 additions & 9 deletions src/diff/index.js
Expand Up @@ -65,7 +65,6 @@ export function diff(parentDom, newVNode, oldVNode, context, isSvg, excessDomChi
c._context = context;
isNew = c._dirty = true;
c._renderCallbacks = [];
c._pendingLayoutEffects = [];
}

// Invoke getDerivedStateFromProps
Expand All @@ -83,7 +82,7 @@ export function diff(parentDom, newVNode, oldVNode, context, isSvg, excessDomChi
if (isNew) {
if (newType.getDerivedStateFromProps==null && c.componentWillMount!=null) c.componentWillMount();
if (c.componentDidMount!=null) {
c._pendingLayoutEffects.push(() => c.componentDidMount());
c._renderCallbacks.push(() => c.componentDidMount());
}
}
else {
Expand All @@ -109,7 +108,7 @@ export function diff(parentDom, newVNode, oldVNode, context, isSvg, excessDomChi
}

if (c.componentDidUpdate!=null) {
c._pendingLayoutEffects.push(() =>
c._renderCallbacks.push(() =>
c.componentDidUpdate(oldProps, oldState, snapshot)
);
}
Expand Down Expand Up @@ -141,10 +140,7 @@ export function diff(parentDom, newVNode, oldVNode, context, isSvg, excessDomChi

c.base = newVNode._dom;

c._pendingLayoutEffects = c._pendingLayoutEffects.concat(c._renderCallbacks);
c._renderCallbacks = [];

if (c._pendingLayoutEffects.length) {
if (c._renderCallbacks.length) {
mounts.push(c);
}

Expand All @@ -170,8 +166,9 @@ export function diff(parentDom, newVNode, oldVNode, context, isSvg, excessDomChi
export function commitRoot(mounts, root) {
mounts.some(c => {
try {
c._pendingLayoutEffects.some(effect => effect.call(c));
c._pendingLayoutEffects = [];
mounts = c._renderCallbacks;
c._renderCallbacks = [];
mounts.some(effect => effect.call(c));
}
catch (e) {
options._catchError(e, c._vnode);
Expand Down
1 change: 0 additions & 1 deletion src/internal.d.ts
Expand Up @@ -64,7 +64,6 @@ export interface Component<P = {}, S = {}> extends preact.Component<P, S> {

_dirty: boolean;
_force?: boolean | null;
_pendingLayoutEffects: Array<{ _value: () => void }>;
_renderCallbacks: Array<() => void>; // Only class components
_context?: any;
_vnode?: VNode<P> | null;
Expand Down
3 changes: 2 additions & 1 deletion test/browser/components.test.js
Expand Up @@ -159,7 +159,8 @@ describe('Components', () => {
}

render(<Foo />, scratch);
rerender();
rerender(); // First setState
rerender(); // Second setState

let [firstState, secondState, thirdState] = states;
expect(finalState).to.deep.equal({ a: 'c' });
Expand Down

0 comments on commit 50dcead

Please sign in to comment.