-
-
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.
* external weakmapped eventbus * updated package version * updated dependencies + added engines specs * updated readme
- Loading branch information
1 parent
48b96c3
commit c4d0ec5
Showing
8 changed files
with
183 additions
and
187 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
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,22 +1,18 @@ | ||
// TBD: Events clean up needed? | ||
import { Mutable } from './types'; | ||
|
||
export type MutationCallback<Value = any> = (newVal: Value, oldVal: Value) => void; | ||
type Callback<T> = (newVal: T, oldVal: T) => void; | ||
|
||
function eventBus() { | ||
const refs: MutationCallback[] = []; | ||
const eventBus = new WeakMap<object, Callback<any>[]>(); | ||
|
||
const change: MutationCallback = (...data) => { | ||
refs.forEach((fn) => fn(...data)); | ||
}; | ||
export function listen<T>(mutable: Mutable<T>, callback: Callback<T>) { | ||
const listeners = eventBus.get(mutable) || []; | ||
listeners.push(callback); | ||
|
||
const changeHandler = (callback: MutationCallback) => { | ||
refs.push(callback); | ||
}; | ||
|
||
return { | ||
change, | ||
changeHandler, | ||
}; | ||
eventBus.set(mutable, listeners); | ||
} | ||
|
||
export default eventBus; | ||
export function emit<T>(mutable: Mutable<T>, newVal: T, oldVal?: T) { | ||
eventBus.get(mutable)?.forEach((fn) => { | ||
fn(newVal, oldVal); | ||
}); | ||
} |
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
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
Oops, something went wrong.