Document dataverse API
This commit is contained in:
parent
5763f2bca4
commit
599cc101c9
17 changed files with 555 additions and 19 deletions
|
@ -3,12 +3,19 @@ import Tappable from './Tappable'
|
|||
type Tapper<V> = (v: V) => void
|
||||
type Untap = () => void
|
||||
|
||||
/**
|
||||
* An event emitter. Emit events that others can tap (subscribe to).
|
||||
*/
|
||||
export default class Emitter<V> {
|
||||
private _tappers: Map<any, (v: V) => void>
|
||||
private _lastTapperId: number
|
||||
readonly tappable: Tappable<V>
|
||||
private _onNumberOfTappersChangeListener: undefined | ((n: number) => void)
|
||||
|
||||
/**
|
||||
* The Tappable associated with this emitter. You can use this to tap (subscribe to) events emitted.
|
||||
*/
|
||||
readonly tappable: Tappable<V>
|
||||
|
||||
constructor() {
|
||||
this._lastTapperId = 0
|
||||
this._tappers = new Map()
|
||||
|
@ -39,16 +46,37 @@ export default class Emitter<V> {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Emit a value.
|
||||
*
|
||||
* @param payload The value to be emitted.
|
||||
*/
|
||||
emit(payload: V) {
|
||||
this._tappers.forEach((cb) => {
|
||||
cb(payload)
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether the emitter has tappers (subscribers).
|
||||
*/
|
||||
hasTappers() {
|
||||
return this._tappers.size !== 0
|
||||
}
|
||||
|
||||
/**
|
||||
* Handler to execute when the number of tappers (subscribers) changes.
|
||||
*
|
||||
* @callback Emitter~numberOfTappersChangeHandler
|
||||
*
|
||||
* @param {number} n The current number of tappers (subscribers).
|
||||
*/
|
||||
|
||||
/**
|
||||
* Calls callback when the number of tappers (subscribers) changes.
|
||||
*
|
||||
* @param {Emitter~requestCallback} cb The function to be called.
|
||||
*/
|
||||
onNumberOfTappersChange(cb: (n: number) => void) {
|
||||
this._onNumberOfTappersChangeListener = cb
|
||||
}
|
||||
|
|
|
@ -7,6 +7,9 @@ interface IProps<V> {
|
|||
|
||||
type Listener<V> = ((v: V) => void) | (() => void)
|
||||
|
||||
/**
|
||||
* Represents a data-source that can be tapped (subscribed to).
|
||||
*/
|
||||
export default class Tappable<V> {
|
||||
private _props: IProps<V>
|
||||
private _tappers: Map<number, {bivarianceHack(v: V): void}['bivarianceHack']>
|
||||
|
@ -55,6 +58,11 @@ export default class Tappable<V> {
|
|||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Tap (subscribe to) the data source.
|
||||
*
|
||||
* @param cb The callback to be called on a change.
|
||||
*/
|
||||
tap(cb: Listener<V>): Untap {
|
||||
const tapperId = this._lastTapperId++
|
||||
this._tappers.set(tapperId, cb)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue