Skip to content

Commit

Permalink
Merge branch 'main' into only-check-for-dom-nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
JoviDeCroock authored Jun 28, 2024
2 parents bd29648 + 76f5d66 commit 75f1b71
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 15 deletions.
16 changes: 4 additions & 12 deletions hooks/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ let currentHook = 0;
/** @type {Array<import('./internal').Component>} */
let afterPaintEffects = [];

let EMPTY = [];

// Cast to use internal Options type
const options = /** @type {import('./internal').Options} */ (_options);

Expand Down Expand Up @@ -60,8 +58,7 @@ options._render = vnode => {
if (hookItem._nextValue) {
hookItem._value = hookItem._nextValue;
}
hookItem._pendingValue = EMPTY;
hookItem._nextValue = hookItem._pendingArgs = undefined;
hookItem._pendingArgs = hookItem._nextValue = undefined;
});
} else {
hooks._pendingEffects.forEach(invokeCleanup);
Expand All @@ -84,11 +81,7 @@ options.diffed = vnode => {
if (hookItem._pendingArgs) {
hookItem._args = hookItem._pendingArgs;
}
if (hookItem._pendingValue !== EMPTY) {
hookItem._value = hookItem._pendingValue;
}
hookItem._pendingArgs = undefined;
hookItem._pendingValue = EMPTY;
});
}
previousComponent = currentComponent = null;
Expand Down Expand Up @@ -159,7 +152,7 @@ function getHookState(index, type) {
});

if (index >= hooks._list.length) {
hooks._list.push({ _pendingValue: EMPTY });
hooks._list.push({});
}

return hooks._list[index];
Expand Down Expand Up @@ -350,10 +343,9 @@ export function useMemo(factory, args) {
/** @type {import('./internal').MemoHookState<T>} */
const state = getHookState(currentIndex++, 7);
if (argsChanged(state._args, args)) {
state._pendingValue = factory();
state._pendingArgs = args;
state._value = factory();
state._args = args;
state._factory = factory;
return state._pendingValue;
}

return state._value;
Expand Down
40 changes: 40 additions & 0 deletions hooks/test/browser/combinations.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -477,4 +477,44 @@ describe('combinations', () => {
'anchor effect'
]);
});

it('should not loop infinitely', () => {
const actions = [];
let toggle;
function App() {
const [value, setValue] = useState(false);

const data = useMemo(() => {
actions.push('memo');
return {};
}, [value]);

const [prevData, setPreviousData] = useState(data);
if (prevData !== data) {
setPreviousData(data);
}

actions.push('render');
toggle = () => setValue(!value);
return <div>Value: {JSON.stringify(value)}</div>;
}

act(() => {
render(<App />, scratch);
});
expect(actions).to.deep.equal(['memo', 'render']);
expect(scratch.innerHTML).to.deep.equal('<div>Value: false</div>');

act(() => {
toggle();
});
expect(actions).to.deep.equal([
'memo',
'render',
'memo',
'render',
'render'
]);
expect(scratch.innerHTML).to.deep.equal('<div>Value: true</div>');
});
});
5 changes: 3 additions & 2 deletions hooks/test/browser/useMemo.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,12 +151,13 @@ describe('useMemo', () => {
act(() => {
set('bye');
});
expect(calls.length).to.equal(5);
expect(calls.length).to.equal(6);
expect(calls).to.deep.equal([
'doing memo',
'render hi',
'doing memo',
'render bye', // We expect a missing "doing memo" here because we return to the previous args value
'render bye',
'doing memo',
'render hi'
]);
});
Expand Down
1 change: 0 additions & 1 deletion mangle.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
"$_list": "__",
"$_pendingEffects": "__h",
"$_value": "__",
"$_pendingValue": "__V",
"$_nextValue": "__N",
"$_original": "__v",
"$_args": "__H",
Expand Down

0 comments on commit 75f1b71

Please sign in to comment.