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
5 changes: 5 additions & 0 deletions .changeset/nine-chairs-care.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@lynx-js/react": patch
---

Fix a memory leak when using `<list/>`.
18 changes: 18 additions & 0 deletions packages/react/runtime/src/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type { SnapshotInstance } from './snapshot.js';

export const gSignMap: Record<number, Map<number, SnapshotInstance>> = {};
export const gRecycleMap: Record<number, Map<string, Map<number, SnapshotInstance>>> = {};
const gParentWeakMap: WeakMap<SnapshotInstance, unknown> = new WeakMap();

export function clearListGlobal(): void {
for (const key in gSignMap) {
Expand All @@ -20,6 +21,23 @@ export function componentAtIndexFactory(
ctx: SnapshotInstance[],
hydrateFunction: (before: SnapshotInstance, after: SnapshotInstance) => void,
): [ComponentAtIndexCallback, ComponentAtIndexesCallback] {
// A hack workaround to ensure childCtx has no direct reference through `__parent` to list,
// to avoid memory leak.
// TODO(hzy): make `__parent` a WeakRef or `#__parent` in the future.
ctx.forEach((childCtx) => {
if (gParentWeakMap.has(childCtx)) {
// do it only once
} else {
gParentWeakMap.set(childCtx, childCtx.parentNode!);
Object.defineProperty(childCtx, '__parent', {
get: () => gParentWeakMap.get(childCtx)!,
set: (value: unknown) => {
gParentWeakMap.set(childCtx, value);
Comment thread
Yradex marked this conversation as resolved.
},
});
}
});

const componentAtIndex = (
list: FiberElement,
listID: number,
Expand Down
Loading