Skip to content

Commit b6de5c0

Browse files
Generate types from JS (#14)
1 parent 6d33c78 commit b6de5c0

File tree

4 files changed

+53
-25
lines changed

4 files changed

+53
-25
lines changed

index.js

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,41 @@
11
import React from 'react'
22
import mitt from 'mitt'
33

4-
export const BusContext = React.createContext(null)
4+
export const BusContext = React.createContext(/** @type {null|import('mitt').Emitter} */ (null))
55
const P = BusContext.Provider
66

7+
/**
8+
* Return the event emitter.
9+
*
10+
* @return {import('mitt').Emitter}
11+
*/
712
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
916
}
1017

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) {
1225
const bus = useBus()
1326
React.useEffect(() => {
14-
bus.on(name, fn)
27+
bus.on(name, listener)
1528
return () => {
16-
bus.off(name, fn)
29+
bus.off(name, listener)
1730
}
18-
}, [bus, name, fn])
31+
}, [bus, name, listener])
1932
}
2033

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+
*/
2139
export function Provider ({ children }) {
2240
const [bus] = React.useState(() => mitt())
2341
return <P value={bus}>{children}</P>

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"url": "https://github.com/goto-bus-stop/react-bus/issues"
99
},
1010
"dependencies": {
11+
"@types/react": "^18.0.8",
1112
"mitt": "^2.1.0"
1213
},
1314
"peerDependencies": {
@@ -18,7 +19,8 @@
1819
"react": "^17.0.0",
1920
"react-test-renderer": "^17.0.0",
2021
"rollup": "^2.38.0",
21-
"tape": "^5.1.1"
22+
"tape": "^5.1.1",
23+
"typescript": "^4.6.4"
2224
},
2325
"homepage": "https://github.com/goto-bus-stop/react-bus#readme",
2426
"keywords": [

tsconfig.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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+
}

types/index.d.ts

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,23 @@
1-
import React, { FC, ReactNode } from 'react';
2-
import { Emitter, EventType, Handler } from 'mitt';
3-
export declare const BusContext: React.Context<Emitter | null>;
41
/**
52
* Return the event emitter.
63
*
7-
* @export
8-
* @return {*}
4+
* @return {import('mitt').Emitter}
95
*/
10-
export declare function useBus(): Emitter;
6+
export function useBus(): import('mitt').Emitter;
117
/**
128
* 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
1712
*/
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;
2214
/**
2315
* Create an event emitter that will be available to all deeply nested child elements using the useBus() hook.
2416
*
25-
* @export
26-
* @param {ProviderProps} { children }
27-
* @return {*}
17+
* @param {{ children?: import('react').ReactNode }} props
2818
*/
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";

0 commit comments

Comments
 (0)