-
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.
Signals are now classes. For easier typing.
- Loading branch information
Showing
2 changed files
with
22 additions
and
18 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,22 @@ | ||
export function Signal<Payload extends any = void>() { | ||
type Listener = (payload: Payload) => void | ||
export class Signal<Payload = void, Listener extends Function = (payload: Payload) => void> { | ||
private listeners = new Array<Listener>() | ||
|
||
const listeners = new Array<Listener>() | ||
|
||
function add(fn: Listener) { | ||
listeners.push(fn) | ||
add(fn: Listener) { | ||
this.listeners.push(fn) | ||
} | ||
|
||
function remove(fn: Listener) { | ||
const pos = listeners.indexOf(fn, 0) | ||
if (pos > -1) listeners.splice(pos, 1) | ||
remove(fn: Listener) { | ||
const pos = this.listeners.indexOf(fn, 0) | ||
if (pos > -1) this.listeners.splice(pos, 1) | ||
} | ||
|
||
function clear() { | ||
listeners.length = 0 | ||
clear() { | ||
this.listeners.length = 0 | ||
} | ||
|
||
function emit(payload: Payload) { | ||
for (const listener of listeners) { | ||
emit(payload: Payload) { | ||
for (const listener of this.listeners) { | ||
listener(payload) | ||
} | ||
} | ||
|
||
return { add, remove, clear, emit } | ||
} |
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