forked from primus/eventemitter3
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: initial guide and structure for api reference
Signed-off-by: Till Sanders <[email protected]>
- Loading branch information
1 parent
f5202ad
commit 2036d73
Showing
20 changed files
with
220 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# API Reference | ||
|
||
TBD |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# addListener() | ||
|
||
```typescript | ||
instance.addListener('foo', () => { /* [...] */ }) | ||
``` | ||
|
||
## Parameters | ||
|
||
| Parameter | Type | Default | Description | | ||
| --------- | ---- | ------- | ----------- | | ||
| event | string \| symbol | | The event name. Expects an [ESMitterEventName](/api/types#esmittereventname). | | ||
| fn | unknown | | The function that will be called then the event is emitted. | | ||
| context | unknown | undefined | The [context](/context) to invoke the listener with. | | ||
| once | boolean | false | Specify if the listener is a one-time listener. | | ||
|
||
## on() | ||
|
||
The [`on()`](/api/on) method is almost an alias of `addListener()`, the only exception being the | ||
missing `once` parameter. For this, use [`once()`](/api/once) instead. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# emit() | ||
|
||
TBD |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# eventNames() | ||
|
||
TBD |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# listenerCount() | ||
|
||
TBD |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# listeners() | ||
|
||
TBD |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# off() | ||
|
||
TBD |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# on() | ||
|
||
TBD |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# once() | ||
|
||
TBD |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# removeAllListeners() | ||
|
||
TBD |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# removeListener() | ||
|
||
TBD |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# Types | ||
|
||
## ESMitterEventName | ||
|
||
TBD | ||
|
||
## ESMitterEvent | ||
|
||
TBD | ||
|
||
## ESMitterEvents | ||
|
||
TBD |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Context | ||
|
||
TBD |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# Installation | ||
|
||
ESMitter can be installed using your package manager of choice: | ||
|
||
::: code-group | ||
|
||
```sh [npm] | ||
npm install esm-itter | ||
``` | ||
|
||
```sh [yarn] | ||
yarn add esm-itter | ||
``` | ||
|
||
```sh [pnpm] | ||
pnpm add esm-itter | ||
``` | ||
|
||
::: | ||
|
||
## Compatibility | ||
|
||
ESMitter requires Node > v16 or a browser with EcmaScript modules (ESM) support. | ||
|
||
## Import | ||
|
||
ESMitter uses named exports. You can import the base class like this: | ||
|
||
```typescript | ||
import { ESMitter } from 'esm-itter'; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# Setup | ||
|
||
ESMitter is designed to be used as a parent class, so all the relevant methods will be available as | ||
part of your own class. If you're looking for an event system that is standalone and works without | ||
classes, you might want to look at [Mitt](https://github.com/developit/mitt). | ||
|
||
```typescript | ||
import { ESMitter } from 'esm-itter'; | ||
|
||
class MyClass extends ESMitter {} | ||
``` | ||
|
||
Since ESMitter is embracing TypeScript, we need to provide a list of known events as a generic type | ||
to the ESMitter class: | ||
|
||
```typescript | ||
import { ESMitter, type ESMitterEvent } from "esm-itter"; | ||
|
||
class MyClass extends ESMitter<{ | ||
'success': ESMitterEvent<[{ foo: string, bar: string }]>; | ||
'error': ESMitterEvent<[number, string]>; | ||
}> {} | ||
``` | ||
|
||
In the example above, we have now added the ESMitter event system with all it's logic and methods to | ||
our own class (`MyClass`). We have also provided a list of events that we can emit: one is named | ||
`'success'` and it's payload will return an object. The other event is named `'error'` and it's | ||
payload will contain a number as the first parameter and a string as the second. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# Usage | ||
|
||
In the [Setup](/esm-itter/setup), we've added two events to our class (`MyClass`). We're now ready | ||
to attach event listeners and emit events. | ||
|
||
## Adding Listeners | ||
|
||
Listeners can be attached using the `on()`, `once()` and `addListener()` methods: | ||
|
||
```typescript{10-17} | ||
import { ESMitter, type ESMitterEvent } from "esm-itter"; | ||
class MyClass extends ESMitter<{ | ||
'success': ESMitterEvent<[{ foo: string, bar: string }]>; | ||
'error': ESMitterEvent<[number, string]>; | ||
}> {} | ||
const instance = MyClass(); | ||
// Attach event listener using on() | ||
instance.on('success', ({ foo, bar }) => { /* [...] */ }) | ||
// Attach event listener using addListener() | ||
instance.addListener('success', ({ foo, bar }) => { /* [...] */ }) | ||
// Attach event listener using once() | ||
instance.once('success', ({ foo, bar }) => { /* [...] */ }) | ||
``` | ||
|
||
Note that `on()` is merely an alias for `addListener()` and `once()` will make sure a listener is | ||
only called once and the discarded. | ||
|
||
The parameters passed to the listener are typed according to the list of events we provided to the | ||
ESMitter class. | ||
|
||
## Removing Listeners | ||
|
||
Listeners can be removed using the `off()`, `removeListener()` and `removeAllListeners()` methods: | ||
|
||
```typescript | ||
// Remove all listeners for the 'success' event using off() | ||
instance.off('success') | ||
|
||
// Remove all listeners for the 'success' event using removeListener() | ||
instance.removeListener('success') | ||
|
||
// Remove all listeners for the 'success' event using removeAllListeners() | ||
instance.removeAllListeners('success') | ||
|
||
// Remove all listeners for all events (like a reset) using removeAllListeners() | ||
instance.removeAllListeners() | ||
|
||
// Remove a specific listener using off() and referencing the listener itself | ||
const listener = ({ foo, bar }) => { /* [...] */ }; | ||
instance.on('success', listener); | ||
instance.off('success', listener); | ||
``` | ||
|
||
Note that `off()` (like `on()`, respectively) is merely an alias for `removeListener()`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters