Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(tiny-react): handle children re-insertion in fragment reconcilation #169

Open
wants to merge 15 commits into
base: refactor-tiny-react-reverse-insert-order
Choose a base branch
from
Open
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: 1 addition & 4 deletions packages/tiny-react/src/hooks.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { tinyassert } from "@hiogawa/utils";
import { isEqualShallow } from "./utils";

// not particularly intentional but this hook module doesn't depend on any of reconciler/virtual-dom logic,
// which tells that the hook idea itself is a general concept applicable to functional api...?
Expand Down Expand Up @@ -216,7 +217,3 @@ function defineHook<T>(implement: (ctx: HookContext) => T): T {
},
}) as T;
}

function isEqualShallow(xs: unknown[], ys: unknown[]) {
return xs.length === ys.length && xs.every((x, i) => x === ys[i]);
}
79 changes: 56 additions & 23 deletions packages/tiny-react/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
Fragment,
createElement,
createVNode,
getBNodeSlot,
getBNodeRange,
} from "./virtual-dom";

describe(render, () => {
Expand Down Expand Up @@ -82,8 +82,15 @@ describe(render, () => {
},
},
],
"hrange": [
hello,
<span
class="text-red"
>
world
</span>,
],
"parent": [Circular],
"slot": hello,
"type": "fragment",
"vnode": {
"children": [
Expand Down Expand Up @@ -234,10 +241,13 @@ describe(render, () => {
},
},
],
"hrange": [
<span>
hello
</span>,
world,
],
"parent": [Circular],
"slot": <span>
hello
</span>,
"type": "fragment",
"vnode": {
"children": [
Expand Down Expand Up @@ -300,13 +310,21 @@ describe(render, () => {
world
</div>
</main>,
"hrange": [
<div>
<span>
hello
</span>
world
</div>,
<div>
<span>
hello
</span>
world
</div>,
],
"parent": null,
"slot": <div>
<span>
hello
</span>
world
</div>,
"type": "custom",
"vnode": {
"key": undefined,
Expand Down Expand Up @@ -728,10 +746,15 @@ describe(updateCustomNode, () => {
</span>
</main>
`);
expect(getBNodeSlot(bnode)).toMatchInlineSnapshot(`
<div>
x
</div>
expect(getBNodeRange(bnode)).toMatchInlineSnapshot(`
[
<div>
x
</div>,
<span>
y
</span>,
]
`);

update1("p");
Expand All @@ -745,10 +768,15 @@ describe(updateCustomNode, () => {
</span>
</main>
`);
expect(getBNodeSlot(bnode)).toMatchInlineSnapshot(`
<span>
y
</span>
expect(getBNodeRange(bnode)).toMatchInlineSnapshot(`
[
<span>
y
</span>,
<p>
x
</p>,
]
`);

update2("b");
Expand All @@ -762,10 +790,15 @@ describe(updateCustomNode, () => {
</b>
</main>
`);
expect(getBNodeSlot(bnode)).toMatchInlineSnapshot(`
<b>
y
</b>
expect(getBNodeRange(bnode)).toMatchInlineSnapshot(`
[
<b>
y
</b>,
<p>
x
</p>,
]
`);
});
});
Expand Down
Loading