Skip to content

Commit fc7e9cc

Browse files
committed
feat(mount)!: add effect handling in mounting
1 parent d029370 commit fc7e9cc

File tree

10 files changed

+51
-43
lines changed

10 files changed

+51
-43
lines changed

src/index.ts

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Signal, signal } from "@preact/signals-core";
1+
import { effect, Signal, signal } from "@preact/signals-core";
22
import { reconcile } from "./reconciler/index";
33
import { ShadowCache } from "./reconciler/utils";
44
import type {
@@ -20,19 +20,22 @@ import {
2020
export type { ShadowElement } from "./types";
2121
export { active, connectedCallback, disconnectedCallback } from "./utils";
2222

23-
export function mount(parent: HTMLElement, JSXElement: ShadowElement) {
23+
export function mount(
24+
render: () => ShadowElement,
25+
parent: HTMLElement,
26+
): () => void {
2427
const shadowResult: ShadowCache = new ShadowCache(false);
25-
26-
active.parentElement = parent;
27-
reconcile({
28-
parentElement: parent,
29-
previousSibling: parent.lastElementChild,
30-
shadowCache: shadowResult,
31-
shadowElement: JSXElement,
32-
getParentOverwrite: null,
28+
return effect(() => {
29+
active.parentElement = parent;
30+
31+
reconcile({
32+
parentElement: parent,
33+
previousSibling: parent.lastElementChild,
34+
shadowCache: shadowResult,
35+
shadowElement: render(),
36+
getParentOverwrite: null,
37+
});
3338
});
34-
35-
return shadowResult.node;
3639
}
3740

3841
export function createComponent<

src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ export namespace JSX {
8888
? Tag
8989
: `svg:${Tag}`]: { [key: string]: any } & {
9090
children?: ShadowElement;
91-
className: string;
91+
className?: string;
9292
onplusnewerror?: (evt: PlusnewErrorEvent) => void;
9393
};
9494
};

test/async.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ describe("webcomponent", () => {
4242
},
4343
);
4444

45-
mount(container, <Component onfoo={() => promise} />);
45+
mount(() => <Component onfoo={() => promise} />, container);
4646

4747
expect(container.childNodes.length).to.equal(1);
4848

test/base.test.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ describe("webcomponent", () => {
3838
// @ts-expect-error component with excessive property baz, should be an error
3939
<Component foo="bar" baz="mep" />;
4040

41-
mount(container, <Component foo="mep" className="some-class" />);
41+
mount(() => <Component foo="mep" className="some-class" />, container);
4242

4343
expect(container.childNodes.length).to.equal(1);
4444

@@ -67,7 +67,7 @@ describe("webcomponent", () => {
6767
},
6868
);
6969

70-
mount(container, <Component amount={0} />);
70+
mount(() => <Component amount={0} />, container);
7171

7272
expect(container.childNodes.length).to.equal(1);
7373

@@ -117,7 +117,7 @@ describe("webcomponent", () => {
117117
},
118118
);
119119

120-
mount(container, <Component show={false} />);
120+
mount(() => <Component show={false} />, container);
121121

122122
expect(container.childNodes.length).to.equal(1);
123123

@@ -139,7 +139,7 @@ describe("webcomponent", () => {
139139
expect(component.shadowRoot?.childNodes.length).to.equal(0);
140140

141141
// Mounting same component a second time, should not interfer with the first
142-
mount(container, <Component show={true} />);
142+
mount(() => <Component show={true} />, container);
143143

144144
expect(component.shadowRoot?.childNodes.length).to.equal(0);
145145
});
@@ -169,7 +169,7 @@ describe("webcomponent", () => {
169169
},
170170
);
171171

172-
mount(container, <Component />);
172+
mount(() => <Component />, container);
173173

174174
expect(container.childNodes.length).to.equal(1);
175175

@@ -234,7 +234,7 @@ describe("webcomponent", () => {
234234
},
235235
);
236236

237-
mount(container, <Component />);
237+
mount(() => <Component />, container);
238238

239239
expect(container.childNodes.length).to.equal(1);
240240

test/context.test.tsx

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,12 @@ describe("webcomponent", () => {
4141

4242
it("finds context", () => {
4343
mount(
44+
() => (
45+
<Provider>
46+
<Consumer />
47+
</Provider>
48+
),
4449
container,
45-
<Provider>
46-
<Consumer />
47-
</Provider>,
4850
);
4951

5052
expect(container.childNodes.length).to.equal(1);
@@ -78,7 +80,7 @@ describe("webcomponent", () => {
7880
},
7981
);
8082

81-
mount(container, <Component />);
83+
mount(() => <Component />, container);
8284

8385
expect(container.childNodes.length).to.equal(1);
8486

@@ -97,7 +99,7 @@ describe("webcomponent", () => {
9799
});
98100

99101
it("no context", () => {
100-
mount(container, <Consumer />);
102+
mount(() => <Consumer />, container);
101103

102104
expect(container.childNodes.length).to.equal(1);
103105
expect((container.childNodes[0] as HTMLElement).tagName).to.equal(
@@ -123,10 +125,12 @@ describe("webcomponent", () => {
123125
);
124126

125127
mount(
128+
() => (
129+
<Injection>
130+
<Consumer />
131+
</Injection>
132+
),
126133
container,
127-
<Injection>
128-
<Consumer />
129-
</Injection>,
130134
);
131135

132136
expect(container.childNodes.length).to.equal(1);

test/error.test.tsx

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ describe("webcomponent", () => {
4040
},
4141
);
4242

43-
mount(container, <Component />);
43+
mount(() => <Component />, container);
4444

4545
expect(container.childNodes.length).to.equal(1);
4646

@@ -66,11 +66,12 @@ describe("webcomponent", () => {
6666
// );
6767

6868
// mount(
69-
// container,
70-
// <ErrorBoundary>
71-
// <Component />
72-
// <span slot="errored">error</span>
73-
// </ErrorBoundary>,
69+
// () =>
70+
// <ErrorBoundary>
71+
// <Component />
72+
// <span slot="errored">error</span>
73+
// </ErrorBoundary>,
74+
// container
7475
// );
7576

7677
// expect(container.childNodes.length).to.equal(1);

test/events.test.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ describe("webcomponent", () => {
5151
},
5252
);
5353

54-
mount(container, <Component />);
54+
mount(() => <Component />, container);
5555

5656
const component = container.childNodes[0] as HTMLElement;
5757
(
@@ -107,7 +107,7 @@ describe("webcomponent", () => {
107107
},
108108
);
109109

110-
mount(container, <Component />);
110+
mount(() => <Component />, container);
111111

112112
const component = container.childNodes[0] as HTMLElement;
113113
const nestedComponent = component.shadowRoot?.childNodes[0] as HTMLElement;

test/fragment.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ describe("fragment", () => {
3232
},
3333
);
3434

35-
mount(container, <Component foo="mep" className="some-class" />);
35+
mount(() => <Component foo="mep" className="some-class" />, container);
3636

3737
expect(container.childNodes.length).to.equal(1);
3838

test/host.test.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ describe("webcomponent", () => {
2525
}
2626
},
2727
);
28-
mount(container, <Component />);
28+
mount(() => <Component />, container);
2929

3030
expect(container.childNodes.length).to.equal(1);
3131

@@ -59,7 +59,7 @@ describe("webcomponent", () => {
5959
},
6060
);
6161

62-
mount(container, <Component />);
62+
mount(() => <Component />, container);
6363

6464
expect(container.childNodes.length).to.equal(1);
6565

@@ -125,7 +125,7 @@ describe("webcomponent", () => {
125125
},
126126
);
127127

128-
mount(container, <Component />);
128+
mount(() => <Component />, container);
129129

130130
expect(container.childNodes.length).to.equal(1);
131131

@@ -169,7 +169,7 @@ describe("webcomponent", () => {
169169
},
170170
);
171171

172-
mount(container, <Component />);
172+
mount(() => <Component />, container);
173173

174174
expect(container.childNodes.length).to.equal(1);
175175

@@ -225,7 +225,7 @@ describe("webcomponent", () => {
225225
},
226226
);
227227

228-
mount(container, <Component />);
228+
mount(() => <Component />, container);
229229

230230
expect(container.childNodes.length).to.equal(1);
231231

test/svg.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ describe("svg", () => {
2727
},
2828
);
2929

30-
mount(container, <Component />);
30+
mount(() => <Component />, container);
3131

3232
expect(container.childNodes.length).to.equal(1);
3333

0 commit comments

Comments
 (0)