1
1
import React from 'react'
2
- import mitt from 'mitt'
2
+ import mitt , { type Emitter , type EventType , type Handler } from 'mitt'
3
3
4
- export const BusContext = React . createContext ( /** @type { null|import('mitt').Emitter } */ ( null ) )
4
+ export const BusContext = React . createContext < Emitter | null > ( null )
5
5
const P = BusContext . Provider
6
6
7
- /**
8
- * Return the event emitter.
9
- *
10
- * @return {import('mitt').Emitter }
11
- */
7
+ /** Return the event emitter. */
12
8
export function useBus ( ) {
13
9
const bus = React . useContext ( BusContext )
14
10
if ( ! bus ) throw new Error ( 'useBus: missing context' )
15
11
return bus
16
12
}
17
13
18
14
/**
19
- * Attach an event listener to the bus while this component is mounted. Adds the listener after mount, and removes it before unmount.
20
-
21
- * @param {import('mitt').EventType } name
22
- * @param {import('mitt').Handler } listener
15
+ * Attach an event listener to the bus while this component is mounted.
16
+ *
17
+ * Adds the listener after mount, and removes it before unmount.
23
18
*/
24
- export function useListener ( name , listener ) {
19
+ export function useListener ( name : EventType , listener : Handler ) {
25
20
const bus = useBus ( )
26
21
React . useEffect ( ( ) => {
27
22
bus . on ( name , listener )
@@ -33,10 +28,8 @@ export function useListener (name, listener) {
33
28
34
29
/**
35
30
* Create an event emitter that will be available to all deeply nested child elements using the useBus() hook.
36
- *
37
- * @param {{ children?: import('react').ReactNode } } props
38
31
*/
39
- export function Provider ( { children } ) {
32
+ export function Provider ( { children } : { children : React . ReactNode } ) {
40
33
const [ bus ] = React . useState ( ( ) => mitt ( ) )
41
34
return < P value = { bus } > { children } </ P >
42
35
}
0 commit comments