Skip to content

Commit 78b66a0

Browse files
docs: ✏️ move documentation to docblocks
1 parent db8d0df commit 78b66a0

File tree

2 files changed

+55
-45
lines changed

2 files changed

+55
-45
lines changed

packages/emitter/README.md

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -12,51 +12,6 @@ Minimalistic event emitter.
1212
yarn add @pacote/emitter
1313
```
1414

15-
## Usage
16-
17-
```typescript
18-
import { createEmitter } from '@pacote/emitter'
19-
20-
const emitter = createEmitter()
21-
22-
emitter.on('greet', () => console.log('Hello, world!'))
23-
emitter.emit('greet') // => 'Hello, world!'
24-
```
25-
26-
### `createEmitter<Events>(): Emitter<Events>`
27-
28-
`createEmitter()` creates a new instance of an event emitter that provides the following methods:
29-
30-
#### `.on(name: string, fn: (...args: any[]): void): () => void`
31-
32-
Binds the `fn` callback to an event with the supplied name.
33-
34-
The method returns a function that unbinds the callback, preventing it from being called again.
35-
36-
```typescript
37-
const emitter = createEmitter()
38-
39-
const unbind = emitter.on('greet', () => console.log('Hello, world!'))
40-
41-
emitter.emit('greet') // triggers the callback
42-
43-
unbind()
44-
45-
emitter.emit('greet') // no longer triggers the callback
46-
```
47-
48-
#### `.emit(name: string, ...args: any[]): void`
49-
50-
Fires the named event, triggering any subscribers previously bound to it using the `.on()` method. Arguments provided after the name are passed to the callback function.
51-
52-
```typescript
53-
const emitter = createEmitter()
54-
55-
emitter.on('greet', (name) => console.log(`Hello, ${name}!`))
56-
57-
emitter.emit('greet', 'TypeScript') // => 'Hello, TypeScript!'
58-
```
59-
6015
## License
6116

6217
MIT © [Luís Rodrigues](https://goblindegook.com).

packages/emitter/src/index.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,65 @@ interface Events {
55
}
66

77
interface Emitter<E extends Events> {
8+
/**
9+
* Binds a subscriber function to an event with the supplied name.
10+
*
11+
* @param name Event name.
12+
* @param fn Subscriber to invoke when the event is emitted.
13+
*
14+
* @returns Function to unbind the event.
15+
*
16+
* @example
17+
*
18+
* ```typescript
19+
* const emitter = createEmitter()
20+
* const unbind = emitter.on('greet', () => console.log('Hello, world!'))
21+
*
22+
* emitter.emit('greet') // triggers the callback
23+
*
24+
* unbind()
25+
*
26+
* emitter.emit('greet') // no longer triggers the callback
27+
* ```
28+
*/
829
on<K extends keyof E>(name: K, fn: E[K]): () => void
30+
31+
/**
32+
* Fires the named event, triggering any subscribers previously bound to
33+
* it using the `.on()` method. Arguments provided after the name are
34+
* passed to the subscriber function.
35+
*
36+
* @param name Event name.
37+
* @param args Optional subscriber parameters.
38+
*
39+
* @example
40+
*
41+
* ```typescript
42+
* const emitter = createEmitter()
43+
*
44+
* const unbind = emitter.on('greet', (name) => console.log(`Hello, ${name}!`))
45+
*
46+
* emitter.emit('greet', 'Pacote') // => 'Hello, Pacote!'
47+
* ```
48+
*/
949
emit<K extends keyof E>(name: K, ...args: Parameters<E[K]>): void
1050
}
1151

52+
/**
53+
* Creates a new instance of an event emitter.
54+
*
55+
* @returns Event emitter.
56+
*
57+
* @example
58+
*
59+
* ```typescript
60+
* import { createEmitter } from '@pacote/emitter'
61+
*
62+
* const emitter = createEmitter()
63+
* emitter.on('greet', () => console.log('Hello, world!'))
64+
* emitter.emit('greet') // => 'Hello, world!'
65+
* ```
66+
*/
1267
export function createEmitter<E extends Events>(): Emitter<E> {
1368
const events: Partial<{ [K in keyof E]: E[K][] }> = {}
1469

0 commit comments

Comments
 (0)