Skip to content

Commit 57b17df

Browse files
committed
minor code improvements
1 parent ca9befe commit 57b17df

2 files changed

Lines changed: 38 additions & 32 deletions

File tree

  • examples/kitchen-sink/app/host
  • packages/compat/source/adapter

examples/kitchen-sink/app/host/state.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ const ALLOWED_EXAMPLE_VALUES = new Set<RenderExample>([
1212
'htm',
1313
'preact',
1414
'react',
15-
'react-mutations',
1615
'svelte',
1716
'vue',
1817
'react-mutations-1',

packages/compat/source/adapter/host.ts

Lines changed: 38 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -70,23 +70,31 @@ export function adaptToLegacyRemoteChannel(
7070
connection: RemoteConnection,
7171
options?: LegacyRemoteChannelOptions,
7272
): LegacyRemoteChannel {
73+
type TreeEntry = {id: string; slot?: string};
74+
7375
// child node list of a given parent
74-
const tree = new Map<string, {id: string; slot?: string}[]>();
76+
const tree = new Map<string, TreeEntry[]>();
77+
78+
function getSiblings(parentId: string) {
79+
const siblings = tree.get(parentId);
80+
81+
if (siblings === undefined) {
82+
const newSiblings: TreeEntry[] = [];
83+
tree.set(parentId, newSiblings);
84+
return newSiblings;
85+
}
86+
87+
return siblings;
88+
}
7589

7690
function mutate(records: RemoteMutationRecord[]) {
7791
for (const record of records) {
7892
const [mutationType, parentId] = record;
7993

8094
switch (mutationType) {
8195
case MUTATION_TYPE_INSERT_CHILD: {
82-
const parentId = record[1];
83-
const node = record[2];
84-
const nextSiblingId = record[3];
85-
if (!tree.has(parentId)) {
86-
tree.set(parentId, []);
87-
}
88-
89-
const siblings = tree.get(parentId)!;
96+
const [, parentId, node, nextSiblingId] = record;
97+
const siblings = getSiblings(parentId);
9098

9199
if (siblings.some((existing) => existing.id === node.id)) {
92100
return;
@@ -100,8 +108,8 @@ export function adaptToLegacyRemoteChannel(
100108
break;
101109
}
102110
case MUTATION_TYPE_REMOVE_CHILD: {
103-
const index = record[2];
104-
removeNode(parentId, index);
111+
const id = record[2];
112+
removeNode(parentId, id);
105113
break;
106114
}
107115
}
@@ -115,10 +123,7 @@ export function adaptToLegacyRemoteChannel(
115123
node: RemoteNodeSerialization,
116124
index: number,
117125
) {
118-
if (!tree.has(parentId)) {
119-
tree.set(parentId, []);
120-
}
121-
const siblings = tree.get(parentId)!;
126+
const siblings = getSiblings(parentId);
122127
siblings.splice(index, 0, {
123128
id: node.id,
124129
slot: 'attributes' in node ? node.attributes?.slot : undefined,
@@ -132,12 +137,11 @@ export function adaptToLegacyRemoteChannel(
132137
}
133138

134139
function removeNode(parentId: string, id: string) {
135-
const siblings = tree.get(parentId);
136-
if (!siblings) {
140+
const siblings = getSiblings(parentId);
141+
const index = siblings.findIndex((child) => child.id === id);
142+
if (index === -1) {
137143
return;
138144
}
139-
const index = siblings?.findIndex((child) => child.id === id);
140-
if (index === -1) return;
141145

142146
siblings.splice(index, 1);
143147
cleanupNode(id);
@@ -185,24 +189,27 @@ export function adaptToLegacyRemoteChannel(
185189

186190
const records = [];
187191

188-
const siblings = tree.get(parentId) ?? [];
189-
const relevantSiblings = [...siblings];
192+
const siblings = getSiblings(parentId);
190193

191-
const existingChildIndex = relevantSiblings.findIndex(
194+
const existingChildIndex = siblings.findIndex(
192195
({id}) => id === child.id,
193196
);
194197

198+
let nextSiblingIndex = index;
199+
195200
if (existingChildIndex >= 0) {
196201
records.push([
197202
MUTATION_TYPE_REMOVE_CHILD,
198203
parentId,
199204
child.id,
200205
] satisfies RemoteMutationRecord);
201206

202-
relevantSiblings.splice(existingChildIndex, 1);
207+
if (existingChildIndex <= index) {
208+
nextSiblingIndex++;
209+
}
203210
}
204211

205-
const nextSibling = relevantSiblings[index];
212+
const nextSibling = siblings[nextSiblingIndex];
206213

207214
records.push([
208215
MUTATION_TYPE_INSERT_CHILD,
@@ -238,41 +245,41 @@ export function adaptToLegacyRemoteChannel(
238245
}
239246

240247
case LEGACY_ACTION_UPDATE_PROPS: {
241-
const [id, props] =
248+
const [parentId, props] =
242249
payload as LegacyActionArgumentMap[typeof LEGACY_ACTION_UPDATE_PROPS];
243-
const siblings = tree.get(id);
250+
const siblings = getSiblings(parentId);
244251

245252
const records = [];
246253

247254
for (const [key, value] of Object.entries(props)) {
248-
const slotNodeId = siblings?.find(({slot}) => slot === key)?.id;
255+
const slotNodeId = siblings.find(({slot}) => slot === key)?.id;
249256

250257
if (isFragment(value)) {
251258
if (slotNodeId !== undefined) {
252259
records.push([
253260
MUTATION_TYPE_REMOVE_CHILD,
254-
id,
261+
parentId,
255262
slotNodeId,
256263
] satisfies RemoteMutationRecord);
257264
}
258265

259266
records.push([
260267
MUTATION_TYPE_INSERT_CHILD,
261-
id,
268+
parentId,
262269
adaptLegacyPropFragmentSerialization(key, value, options),
263270
undefined,
264271
] satisfies RemoteMutationRecord);
265272
} else {
266273
if (slotNodeId !== undefined) {
267274
records.push([
268275
MUTATION_TYPE_REMOVE_CHILD,
269-
id,
276+
parentId,
270277
slotNodeId,
271278
] satisfies RemoteMutationRecord);
272279
} else {
273280
records.push([
274281
MUTATION_TYPE_UPDATE_PROPERTY,
275-
id,
282+
parentId,
276283
key,
277284
value,
278285
] satisfies RemoteMutationRecord);

0 commit comments

Comments
 (0)