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
3 changes: 3 additions & 0 deletions .changeset/few-shirts-laugh.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---

---
97 changes: 97 additions & 0 deletions packages/react/runtime/__test__/basic.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,103 @@ describe('insertBefore', () => {
`);
});

it('keeps __current_slot_index stable when removing content inside slot wrappers', async function() {
const snapshot1 = __SNAPSHOT__(
<view>
<text>
{HOLE}!!!{HOLE}
</text>
</view>,
);

const snapshot2 = __SNAPSHOT__(
<view>
<text>Hello World</text>
</view>,
);

const a = new SnapshotInstance(snapshot1);
a.ensureElements();

// Insert two wrappers for stable slot index
const b = new SnapshotInstance('wrapper');
const c = new SnapshotInstance('wrapper');
expect(a.__current_slot_index).toBe(0);
a.insertBefore(b);
expect(a.__current_slot_index).toBe(1);
a.insertBefore(c);
expect(a.__current_slot_index).toBe(2);
expect(a.__element_root).toMatchInlineSnapshot(`
<view>
<text>
<wrapper />
<raw-text
text="!!!"
/>
<wrapper />
</text>
</view>
`);

const d = new SnapshotInstance(snapshot2);
const e = new SnapshotInstance(snapshot2);

b.insertBefore(d);
c.insertBefore(e);

expect(a.__current_slot_index).toBe(2);
expect(a.__element_root).toMatchInlineSnapshot(`
<view>
<text>
<wrapper>
<view>
<text>
<raw-text
text="Hello World"
/>
</text>
</view>
</wrapper>
<raw-text
text="!!!"
/>
<wrapper>
<view>
<text>
<raw-text
text="Hello World"
/>
</text>
</view>
</wrapper>
</text>
</view>
`);

b.removeChild(d);

expect(a.__current_slot_index).toBe(2);
expect(a.__element_root).toMatchInlineSnapshot(`
<view>
<text>
<wrapper />
<raw-text
text="!!!"
/>
<wrapper>
<view>
<text>
<raw-text
text="Hello World"
/>
</text>
</view>
</wrapper>
</text>
</view>
`);
});

it('snapshot slot count = 2 - delayed ensureElements', async function() {
const snapshot1 = __SNAPSHOT__(
<view>
Expand Down
4 changes: 4 additions & 0 deletions packages/react/runtime/src/snapshot/snapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ export class SnapshotInstance {
__elements?: FiberElement[] | undefined;
__element_root?: FiberElement | undefined;
__values?: unknown[] | undefined;
// current slot index for dynamic parts
// only increment when inserting dynamic parts
// when removing dynamic parts, the slot index will not change
// cause there would be a wrapper to keep the slot index stable
__current_slot_index = 0;
Comment thread
HuJean marked this conversation as resolved.
__worklet_ref_set?: Set<WorkletRefImpl<any> | Worklet>;
__listItemPlatformInfo?: PlatformInfo;
Expand Down
Loading