Skip to content

Commit bb9cd0a

Browse files
Avoid closing over vnodes in component updater (#973)
* Avoid closing over vnodes in component updater * Fix component updater callback type --------- Co-authored-by: Jovi De Croock <decroockjovi@gmail.com>
1 parent eb92fcf commit bb9cd0a

2 files changed

Lines changed: 14 additions & 5 deletions

File tree

.changeset/crisp-sides-talk.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@preact/signals": patch
3+
---
4+
5+
Avoid closing over VNodes in component updater callbacks.

packages/preact/src/index.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -235,11 +235,7 @@ hook(OptionsTypes.RENDER, (old, vnode) => {
235235
updater = component._updater;
236236
if (updater === undefined) {
237237
component._updater = updater = createUpdater(
238-
() => {
239-
if (DEVTOOLS_ENABLED) updater!._debugCallback?.call(updater);
240-
component._updateFlags |= HAS_PENDING_UPDATE;
241-
component.setState({});
242-
},
238+
createComponentUpdateCallback(component),
243239
typeof vnode.type === "function"
244240
? vnode.type.displayName || vnode.type.name
245241
: ""
@@ -252,6 +248,14 @@ hook(OptionsTypes.RENDER, (old, vnode) => {
252248
}
253249
});
254250

251+
function createComponentUpdateCallback(component: AugmentedComponent) {
252+
return function (this: Effect) {
253+
if (DEVTOOLS_ENABLED) this._debugCallback?.call(this);
254+
component._updateFlags |= HAS_PENDING_UPDATE;
255+
component.setState({});
256+
};
257+
}
258+
255259
/** Finish current updater if a component errors */
256260
hook(OptionsTypes.CATCH_ERROR, (old, error, vnode, oldVNode) => {
257261
setCurrentUpdater();

0 commit comments

Comments
 (0)