Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/new-bobcats-cry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@lynx-js/react": patch
---

Fixed a race condition when updating states and GlobalProps simultaneously.

This fix prevents the "Attempt to render more than one `<page />`" error from occurring during normal application usage.
23 changes: 17 additions & 6 deletions packages/react/runtime/src/lynx/runWithForce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@ import { options } from 'preact';
import type { VNode } from 'preact';
import { COMPONENT, DIFF, DIFFED, FORCE } from '../renderToOpcodes/constants.js';

const sForcedVNode = Symbol('FORCE');

type PatchedVNode = VNode & { [sForcedVNode]?: true };

export function runWithForce(cb: () => void): void {
// save vnode and its `_component` in WeakMap
const m = new WeakMap<VNode, any>();

const oldDiff = options[DIFF];

options[DIFF] = (vnode: VNode) => {
options[DIFF] = (vnode: PatchedVNode) => {
if (oldDiff) {
oldDiff(vnode);
}
Expand All @@ -28,19 +32,26 @@ export function runWithForce(cb: () => void): void {
return m.get(vnode);
},
});
vnode[sForcedVNode] = true;
};

const oldDiffed = options[DIFFED];

options[DIFFED] = (vnode: VNode) => {
options[DIFFED] = (vnode: PatchedVNode) => {
if (oldDiffed) {
oldDiffed(vnode);
}

// delete is a reverse operation of previous `Object.defineProperty`
delete vnode[COMPONENT];
// restore
vnode[COMPONENT] = m.get(vnode);
// There would be cases when `options[DIFF]` has been reset while options[DIFFED] is not,
// so we need to check if `vnode` is patched by `options[DIFF]`.
// We only want to change the patched vnode
if (vnode[sForcedVNode]) {
// delete is a reverse operation of previous `Object.defineProperty`
delete vnode[COMPONENT];
delete vnode[sForcedVNode];
// restore
vnode[COMPONENT] = m.get(vnode);
}
};

try {
Expand Down
Loading