bndr-js - v0.18.0 / Emitter

Class: Emitter<T>

A foundational value of the library, an instance representing a single event emitter. This could be user input from a mouse, keyboard, MIDI controller, gamepad etc., or the result of filtering or composing these inputs. Various operations can be attached by method chaining.

Type parameters

NameType
Tany

Hierarchy

Constructors

constructor

new Emitter<T>(options?)

Type parameters

NameType
Tany

Parameters

NameType
optionsEmitterOptions<T>

Defined in

src/Emitter.ts:37open in new window

Common Filters

change

change(equalFn?): Emitter<T>

Emits only when the value is changed

Parameters

NameTypeDefault valueDescription
equalFn(a: T, b: T) => booleanisEqualA comparator function. The event will be emitted when the function returns falsy value.

Returns

Emitter<T>

Defined in

src/Emitter.ts:305open in new window


constant

constant<U>(value): Emitter<U>

Creates an emitter that emits a constant value every time the current emitter is emitted.

Type parameters

Name
U

Parameters

NameType
valueU

Returns

Emitter<U>

See

https://lodash.com/docs/4.17.15#throttleopen in new window

Defined in

src/Emitter.ts:393open in new window


debounce

debounce(wait, options): Emitter<unknown>

Creates debounced version of the current emitter.

Parameters

NameTypeDescription
waitnumberMilliseconds to wait.
optionsDebounceSettings

Returns

Emitter<unknown>

A new emitter

Defined in

src/Emitter.ts:435open in new window


delay

delay(wait): Emitter<unknown>

Creates delayed version of the current emitter.

Parameters

NameTypeDescription
waitnumberMilliseconds to wait.

Returns

Emitter<unknown>

A new emitter

Defined in

src/Emitter.ts:460open in new window


delta

delta<U>(fn): Emitter<U>

Creates an emitter that emits the ‘difference’ between the current value and the previous value.

Type parameters

Name
U

Parameters

NameTypeDescription
fn(prev: T, curt: T) => UA function to calculate the difference

Returns

Emitter<U>

A new emitter

Defined in

src/Emitter.ts:679open in new window


down

down(): Emitter<true>

Creates an emitter that emits at the moment the current value changes from falsy to truthy.

Returns

Emitter<true>

Defined in

src/Emitter.ts:268open in new window


filter

filter(fn): Emitter<T>

Filters events with the given predicate function

Parameters

NameTypeDescription
fn(value: T) => anyReturn truthy value to pass events

Returns

Emitter<T>

A new emitter

Defined in

src/Emitter.ts:237open in new window


filterMap

filterMap<U>(fn, initialValue?): Emitter<U>

Maps the current value to another type of value, and emits the mapped value only when the mapped value is not undefined.

Type parameters

Name
U

Parameters

NameTypeDescription
fn(value: T) => UA function to map the current value. Return undefined to skip emitting.
initialValue?U-

Returns

Emitter<U>

Defined in

src/Emitter.ts:252open in new window


fold

fold<U>(fn, initialState): Emitter<U>

Initializes with an initialState value. On each emitted event, calculates a new state based on the previous state and the current value, and emits this new state.

Type parameters

Name
U

Parameters

NameTypeDescription
fn(prev: U, value: T) => UA function to calculate a new state
initialStateUAn initial state value

Returns

Emitter<U>

A new emitter

Defined in

src/Emitter.ts:633open in new window


interval

interval(ms?, immediate?): Emitter<any>

Creates an emitter that keeps to emit the last value of the current emitter at the specified interval.

Parameters

NameTypeDefault valueDescription
msnumber0The interval in milliseconds. Set 0 to use requestAnimationFrame.
immediatebooleanfalseIf set to false, the new emitter waits to emit until the current emitter emits any value.

Returns

Emitter<any>

A new emitter.

Defined in

src/Emitter.ts:702open in new window


lerp

lerp(lerp, rate, threshold?): Emitter<T>

Smoothen the change rate of the input value.

Parameters

NameTypeDefault valueDescription
lerpLerp<T>undefinedA function to interpolate the current value and the target value.
ratenumberundefinedThe ratio of linear interpolation from the current value to the target value with each update.
thresholdnumber1e-4The threshold to determine whether the current value is close enough to the target value. If the difference between the current value and the target value is less than this value, the target value will be used as the current value and the interpolation will be stopped.

Returns

Emitter<T>

A new emitter

Defined in

src/Emitter.ts:506open in new window


longPress

longPress(wait): Object

Parameters

NameType
waitnumber

Returns

Object

NameType
pressedEmitter<unknown>

Defined in

src/Emitter.ts:476open in new window


map

map<U>(fn, initialValue?): Emitter<U>

Transforms the payload of event with the given function.

Type parameters

Name
U

Parameters

NameTypeDescription
fn(value: T) => UA function to transform the payload
initialValue?U-

Returns

Emitter<U>

A new emitter

Defined in

src/Emitter.ts:224open in new window


not

not(): Emitter<boolean>

Creates an emitter whose payload is negated.

Returns

Emitter<boolean>

Defined in

src/Emitter.ts:294open in new window


resetBy

resetBy(emitter, emitOnReset?): Emitter<T>

Reset the state of current emitter emitter when the given event is fired.

Parameters

NameTypeDefault valueDescription
emitterEmitter<any>undefinedThe emitter that triggers the current emitter to be reset.
emitOnResetbooleantrueIf set to true, the current emitter will be triggered when it is reset.

Returns

Emitter<T>

The current emitter emitter

Defined in

src/Emitter.ts:610open in new window


stash

stash(...triggers): Emitter<any>

Creates an emitter that emits the current value when one of the given events is fired.

Parameters

NameTypeDescription
...triggersEmitter<any>[]Emitters to trigger the current emitter to emit.

Returns

Emitter<any>

A new emitter

Defined in

src/Emitter.ts:659open in new window


throttle

throttle(wait, options?): Emitter<T>

Creates throttled version of the current emitter.

Parameters

NameTypeDescription
waitnumberMilliseconds to wait.
options?ThrottleSettings

Returns

Emitter<T>

See

https://lodash.com/docs/4.17.15#debouncedopen in new window

Defined in

src/Emitter.ts:410open in new window


trail

trail(count?, emitAtCount?): Emitter<T[]>

Emits an array caching a specified number of values that were emitted in the past.

Parameters

NameTypeDefault valueDescription
countnumber2The number of cache frames. Set 0 to store caches infinitely.
emitAtCountbooleantrueWhen set to true, events will not be emitted until the count of cache reaches to count.

Returns

Emitter<T[]>

Defined in

src/Emitter.ts:739open in new window


up

up(): Emitter<true>

Creates an emitter that emits at the moment the current value changes from falsy to truthy.

Returns

Emitter<true>

Defined in

src/Emitter.ts:281open in new window


while

while(emitter, resetOnDown?): Emitter<T>

Emits while the given event is truthy. The event will be also emitted when the given emitter is changed from falsy to truthy when the resetOnDown flag is set to true.

Parameters

NameTypeDefault valueDescription
emitterEmitter<boolean>undefinedAn emitter to filter the events
resetOnDownbooleantrueIf set to true, the returned emitter will be reset when the given emitter is down.

Returns

Emitter<T>

Defined in

src/Emitter.ts:326open in new window

Methods

createDerived

createDerived<U>(options): Emitter<U>

Type parameters

Name
U

Parameters

NameType
optionsEmitterOptions<U> & { propagate: (e: T, emit: (v: U) => void) => void }

Returns

Emitter<U>

Defined in

src/Emitter.ts:80open in new window


registerDerived

registerDerived(emitter, listener): void

Parameters

NameType
emitterEmitter<any>
listenerListener<T>

Returns

void

Defined in

src/Emitter.ts:72open in new window


tween

tween(lerp, durationMs): Emitter<T>

Parameters

NameType
lerpLerp<T>
durationMsnumber

Returns

Emitter<T>

Defined in

src/Emitter.ts:560open in new window

Properties

_disposed

Protected _disposed: boolean = false

Defined in

src/Emitter.ts:61open in new window


derivedEmitters

Protected Readonly derivedEmitters: Map<Emitter<any>, Listener<T>>

Stores all deviced events and their listeners. They will not be unregistered by removeAllListeners.

Defined in

src/Emitter.ts:59open in new window


icon

Optional icon: IconSequence

Defined in

src/Emitter.ts:49open in new window


stateful

get stateful(): boolean

Returns true if the emitter has a state and can be reset.

Returns

boolean

Defined in

src/Emitter.ts:137open in new window


value

get value(): T

The latest value emitted from the emitter. If the emitter has never fired before, it just returns undefined.

Returns

T

Defined in

src/Emitter.ts:162open in new window

Accessors

disposed

get disposed(): boolean

Returns

boolean

Defined in

src/Emitter.ts:63open in new window

Commom Filters

split

split(emitter, count, resetOnSwitch?): Emitter<T>[]

Splits the current emitter into multiple emitters. Each emitter emits only when the given emitter is changed to the corresponding index.

Parameters

NameTypeDefault valueDescription
emitterEmitter<any>undefinedAn emitter to filter the current event
countnumberundefinedThe number of emitters to be created.
resetOnSwitchbooleantrueIf set to true, the corresponding emitter will be reset when the index of current emitter is switched.

Returns

Emitter<T>[]

Defined in

src/Emitter.ts:355open in new window

Event Handlers

dispose

dispose(): void

Disposes the emitter immediately and prevent to emit any value in the future

Returns

void

Defined in

src/Emitter.ts:119open in new window


emit

emit(value): void

Manually emits the event.

Parameters

NameType
valueT

Returns

void

Defined in

src/Emitter.ts:190open in new window


log

log(message?): Emitter<T>

Parameters

NameTypeDefault value
messagestring'Bndr'

Returns

Emitter<T>

Defined in

src/Emitter.ts:757open in new window


off

off(listener): void

Removes the listener function from the event.

Parameters

NameType
listenerListener<T>

Returns

void

Defined in

src/Emitter.ts:181open in new window


on

on(listener): Emitter<T>

Adds the listener function for the event.

Parameters

NameTypeDescription
listenerListener<T>The callback function

Returns

Emitter<T>

Defined in

src/Emitter.ts:171open in new window


once

once(listener): void

Adds a one-time listener function for the event

Parameters

NameType
listenerListener<T>

Returns

void

Defined in

src/Emitter.ts:202open in new window


removeAllListeners

removeAllListeners(): void

Removes all listeners.

Returns

void

Defined in

src/Emitter.ts:214open in new window


reset

reset(): void

Resets the state of the emitter.

Returns

void

Defined in

src/Emitter.ts:145open in new window