-
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.
- Loading branch information
Showing
1 changed file
with
41 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,43 @@ | ||
# @hmans/signal | ||
|
||
A super duper simple signal implementation. | ||
## A super duper simple signal implementation. | ||
|
||
### tl;dr | ||
|
||
```tsx | ||
import { Signal } from "@hmans/signal" | ||
|
||
const signal = new Signal<number>() | ||
signal.add((n) => console.log(n)) | ||
signal.emit() | ||
``` | ||
|
||
Callbacks are added through `add` and removed through `remove`. | ||
|
||
```tsx | ||
const callback = (n) => console.log(n) | ||
signal.add(callback) | ||
signal.remove(callback) | ||
``` | ||
|
||
`clear` discards all registered listeners: | ||
|
||
```tsx | ||
signal.clear() | ||
``` | ||
|
||
Signals optionally accept a listener through their constructor (just a bit of syntactical sugar for convenience): | ||
|
||
```tsx | ||
const signal = new Signal(() => console.log("I've been signalled!")) | ||
signal.emit() | ||
``` | ||
|
||
Interactions with `Signal` instances can be chained: | ||
|
||
```tsx | ||
new Signal<string>() | ||
.add((name) => console.log(`Hello ${name}!`)) | ||
.add((name) => console.log(`Hi again ${name}!`)) | ||
.emit() | ||
``` |