Add initial tools for managing derivations and React compatibility (#202)
Co-authored-by: Cole Lawrence <cole@colelawrence.com> Co-authored-by: Elliot <key.draw@gmail.com> Co-authored-by: Aria <aria.minaei@gmail.com>
This commit is contained in:
parent
bebf281517
commit
f1844952ea
12 changed files with 320 additions and 30 deletions
|
@ -20,17 +20,14 @@ export default class Emitter<V> {
|
|||
this._lastTapperId = 0
|
||||
this._tappers = new Map()
|
||||
this.tappable = new Tappable({
|
||||
tapToSource: (cb: Tapper<V>) => {
|
||||
return this._tap(cb)
|
||||
},
|
||||
tapToSource: (cb: Tapper<V>) => this._tap(cb),
|
||||
})
|
||||
}
|
||||
|
||||
_tap(cb: Tapper<V>): Untap {
|
||||
const tapperId = this._lastTapperId++
|
||||
this._tappers.set(tapperId, cb)
|
||||
this._onNumberOfTappersChangeListener &&
|
||||
this._onNumberOfTappersChangeListener(this._tappers.size)
|
||||
this._onNumberOfTappersChangeListener?.(this._tappers.size)
|
||||
return () => {
|
||||
this._removeTapperById(tapperId)
|
||||
}
|
||||
|
@ -41,8 +38,7 @@ export default class Emitter<V> {
|
|||
this._tappers.delete(id)
|
||||
const newSize = this._tappers.size
|
||||
if (oldSize !== newSize) {
|
||||
this._onNumberOfTappersChangeListener &&
|
||||
this._onNumberOfTappersChangeListener(this._tappers.size)
|
||||
this._onNumberOfTappersChangeListener?.(newSize)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -52,9 +48,9 @@ export default class Emitter<V> {
|
|||
* @param payload - The value to be emitted.
|
||||
*/
|
||||
emit(payload: V) {
|
||||
this._tappers.forEach((cb) => {
|
||||
for (const cb of this._tappers.values()) {
|
||||
cb(payload)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -53,9 +53,9 @@ export default class Tappable<V> {
|
|||
}
|
||||
|
||||
private _cb: any = (arg: any): void => {
|
||||
this._tappers.forEach((cb) => {
|
||||
for (const cb of this._tappers.values()) {
|
||||
cb(arg)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue