Skip to content

Commit 278dd53

Browse files
committed
fix(dispatchEvent)!: change signature of dispatchEvent
1 parent c0ea215 commit 278dd53

File tree

3 files changed

+19
-8
lines changed

3 files changed

+19
-8
lines changed

src/index.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -180,18 +180,23 @@ export function findParent<T = Element>(
180180
export function dispatchEvent<
181181
T extends HTMLElement,
182182
U extends keyof CustomEvents<T>,
183-
>(target: T, eventName: U, detail: CustomEvents<T>[U]): Promise<unknown>[] {
183+
>(
184+
target: T,
185+
eventName: U,
186+
customEventInit: CustomEventInit<CustomEvents<T>[U]>,
187+
): {
188+
promises: Promise<unknown>[];
189+
customEvent: CustomEvent<CustomEvents<T>[U]>;
190+
} {
184191
const previousEventPromises = active.eventPromises;
185192
const eventPromises: Promise<unknown>[] = [];
186193
active.eventPromises = eventPromises;
187-
188-
target.dispatchEvent(
189-
new CustomEvent(eventName as string, { detail: detail }),
190-
);
194+
const customEvent = new CustomEvent(eventName as string, customEventInit);
195+
target.dispatchEvent(customEvent);
191196

192197
active.eventPromises = previousEventPromises;
193198

194-
return eventPromises;
199+
return { promises: eventPromises, customEvent };
195200
}
196201

197202
export function prop() {

test/async.test.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ describe("webcomponent", () => {
3030
onclick={async () => {
3131
this.#loading.value = true;
3232
try {
33-
await Promise.all(dispatchEvent(this, "foo", null));
33+
await Promise.all(
34+
dispatchEvent(this, "foo", { detail: null }).promises,
35+
);
3436
} catch (_err) {}
3537
this.#loading.value = false;
3638
}}

test/events.test.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,11 @@ describe("webcomponent", () => {
3636
class NestedComponent extends HTMLElement {
3737
onfoo: (evt: CustomEvent<string>) => void;
3838
render(this: NestedComponent) {
39-
return <button onclick={() => dispatchEvent(this, "foo", "mep")} />;
39+
return (
40+
<button
41+
onclick={() => dispatchEvent(this, "foo", { detail: "mep" })}
42+
/>
43+
);
4044
}
4145
},
4246
);

0 commit comments

Comments
 (0)