File tree Expand file tree Collapse file tree 4 files changed +53
-25
lines changed Expand file tree Collapse file tree 4 files changed +53
-25
lines changed Original file line number Diff line number Diff line change 1
1
import React from 'react'
2
2
import mitt from 'mitt'
3
3
4
- export const BusContext = React . createContext ( null )
4
+ export const BusContext = React . createContext ( /** @type { null|import('mitt').Emitter } */ ( null ) )
5
5
const P = BusContext . Provider
6
6
7
+ /**
8
+ * Return the event emitter.
9
+ *
10
+ * @return {import('mitt').Emitter }
11
+ */
7
12
export function useBus ( ) {
8
- return React . useContext ( BusContext )
13
+ const bus = React . useContext ( BusContext )
14
+ if ( ! bus ) throw new Error ( 'useBus: missing context' )
15
+ return bus
9
16
}
10
17
11
- export function useListener ( name , fn ) {
18
+ /**
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
23
+ */
24
+ export function useListener ( name , listener ) {
12
25
const bus = useBus ( )
13
26
React . useEffect ( ( ) => {
14
- bus . on ( name , fn )
27
+ bus . on ( name , listener )
15
28
return ( ) => {
16
- bus . off ( name , fn )
29
+ bus . off ( name , listener )
17
30
}
18
- } , [ bus , name , fn ] )
31
+ } , [ bus , name , listener ] )
19
32
}
20
33
34
+ /**
35
+ * 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
+ */
21
39
export function Provider ( { children } ) {
22
40
const [ bus ] = React . useState ( ( ) => mitt ( ) )
23
41
return < P value = { bus } > { children } </ P >
Original file line number Diff line number Diff line change 8
8
"url" : " https://github.com/goto-bus-stop/react-bus/issues"
9
9
},
10
10
"dependencies" : {
11
+ "@types/react" : " ^18.0.8" ,
11
12
"mitt" : " ^2.1.0"
12
13
},
13
14
"peerDependencies" : {
18
19
"react" : " ^17.0.0" ,
19
20
"react-test-renderer" : " ^17.0.0" ,
20
21
"rollup" : " ^2.38.0" ,
21
- "tape" : " ^5.1.1"
22
+ "tape" : " ^5.1.1" ,
23
+ "typescript" : " ^4.6.4"
22
24
},
23
25
"homepage" : " https://github.com/goto-bus-stop/react-bus#readme" ,
24
26
"keywords" : [
Original file line number Diff line number Diff line change
1
+ {
2
+ "compilerOptions" : {
3
+ "lib" : [" es2015" ],
4
+ "allowJs" : true ,
5
+ "checkJs" : true ,
6
+ "declaration" : true ,
7
+ "emitDeclarationOnly" : true ,
8
+ "jsx" : " preserve" ,
9
+ "strict" : true ,
10
+ "esModuleInterop" : true ,
11
+ "outDir" : " types"
12
+ },
13
+ "files" : [" index.js" ]
14
+ }
Original file line number Diff line number Diff line change 1
- import React , { FC , ReactNode } from 'react' ;
2
- import { Emitter , EventType , Handler } from 'mitt' ;
3
- export declare const BusContext : React . Context < Emitter | null > ;
4
1
/**
5
2
* Return the event emitter.
6
3
*
7
- * @export
8
- * @return {* }
4
+ * @return {import('mitt').Emitter }
9
5
*/
10
- export declare function useBus ( ) : Emitter ;
6
+ export function useBus ( ) : import ( 'mitt' ) . Emitter ;
11
7
/**
12
8
* Attach an event listener to the bus while this component is mounted. Adds the listener after mount, and removes it before unmount.
13
- *
14
- * @export
15
- * @param {string } name
16
- * @param {Handler } fn
9
+
10
+ * @param {import('mitt').EventType } name
11
+ * @param {import('mitt').Handler } listener
17
12
*/
18
- export declare function useListener < T = any > ( name : EventType , fn : Handler < T > ) : void ;
19
- export interface ProviderProps {
20
- children ?: ReactNode ;
21
- }
13
+ export function useListener ( name : import ( 'mitt' ) . EventType , listener : import ( 'mitt' ) . Handler ) : void ;
22
14
/**
23
15
* Create an event emitter that will be available to all deeply nested child elements using the useBus() hook.
24
16
*
25
- * @export
26
- * @param {ProviderProps } { children }
27
- * @return {* }
17
+ * @param {{ children?: import('react').ReactNode } } props
28
18
*/
29
- export declare const Provider : FC < ProviderProps > ;
19
+ export function Provider ( { children } : {
20
+ children ?: import ( 'react' ) . ReactNode ;
21
+ } ) : JSX . Element ;
22
+ export const BusContext : React . Context < import ( "mitt" ) . Emitter | null > ;
23
+ import React from "react" ;
You can’t perform that action at this time.
0 commit comments