Skip to content

Commit 0a687e5

Browse files
committed
Added support for custom eventTarget
1 parent 87bd62b commit 0a687e5

5 files changed

Lines changed: 42 additions & 6 deletions

File tree

.changeset/flat-chefs-double.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,6 @@
22
"@forge42/web-events": patch
33
---
44

5-
Added support for custom event target
5+
Added support for custom event target in the `registerReactEvent` function.
6+
7+
Made the registered events more performant by reusing the same event target for all events.

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,18 @@ const unsubscribe = listener((data) => {
8989
unsubscribe()
9090
```
9191

92+
You can also pass in a third optional parameter to the `registerEvent` function to specify the event init options, such as `bubbles`, `cancelable`, or `composed`. This allows you to control the behavior of the event in the DOM.
93+
94+
It also takes in an optional property `emitter` which can be used to specify the `EventTarget` that will emit the event. This is useful if you want to use a custom event target instead of the default one.
95+
96+
```ts
97+
const [dispatch, listener] = registerEvent("user:logged-in", UserLoggedIn, {
98+
bubbles: true,
99+
composed: true,
100+
emitter: document // or any other EventTarget
101+
})
102+
```
103+
92104
### React
93105

94106
You can also use `@forge42/web-events` in a React application. It comes with a React specific API that allows you to register and listen to events in a more React-friendly way.

packages/web-events/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,18 @@ const unsubscribe = listener((data) => {
8989
unsubscribe()
9090
```
9191

92+
You can also pass in a third optional parameter to the `registerEvent` function to specify the event init options, such as `bubbles`, `cancelable`, or `composed`. This allows you to control the behavior of the event in the DOM.
93+
94+
It also takes in an optional property `emitter` which can be used to specify the `EventTarget` that will emit the event. This is useful if you want to use a custom event target instead of the default one.
95+
96+
```ts
97+
const [dispatch, listener] = registerEvent("user:logged-in", UserLoggedIn, {
98+
bubbles: true,
99+
composed: true,
100+
emitter: document // or any other EventTarget
101+
})
102+
```
103+
92104
### React
93105

94106
You can also use `@forge42/web-events` in a React application. It comes with a React specific API that allows you to register and listen to events in a more React-friendly way.

packages/web-events/src/index.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,18 @@ import { standardValidate } from "./validate"
33

44
export type EventCallback<T extends StandardSchemaV1> = (data: StandardSchemaV1.InferOutput<T>) => void
55

6-
export const registerEvent = <T extends StandardSchemaV1>(name: string, schema: T, eventInit?: CustomEventInit) => {
7-
const emitter = new EventTarget()
6+
const defaultEmitter = new EventTarget()
7+
8+
export interface ExtendedEventEmit extends EventInit {
9+
emitter?: EventTarget
10+
}
11+
12+
export const registerEvent = <T extends StandardSchemaV1>(name: string, schema: T, eventInit?: ExtendedEventEmit) => {
13+
const { emitter = defaultEmitter, ...rest } = eventInit || {}
814
const dispatch = async (data: StandardSchemaV1.InferInput<typeof schema>) => {
915
const result = await standardValidate<T>(schema, data)
1016
const event = new CustomEvent(name, {
11-
...eventInit,
17+
...rest,
1218
detail: result,
1319
})
1420
emitter.dispatchEvent(event)

packages/web-events/src/react.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
import type { StandardSchemaV1 } from "@standard-schema/spec"
22
import { useEffect, useState } from "react"
3-
import { type EventCallback, registerEvent } from "."
3+
import { type EventCallback, type ExtendedEventEmit, registerEvent } from "."
44

5-
export const registerReactEvent = <T extends StandardSchemaV1>(name: string, schema: T, eventInit?: EventInit) => {
5+
export const registerReactEvent = <T extends StandardSchemaV1>(
6+
name: string,
7+
schema: T,
8+
eventInit?: ExtendedEventEmit
9+
) => {
610
const [dispatch, listener] = registerEvent(name, schema, eventInit)
711

812
const useEventListener = (args?: { onEvent?: EventCallback<T> }) => {

0 commit comments

Comments
 (0)