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
Name | Type |
---|---|
T | any |
Hierarchy
Emitter
Constructors
constructor
• new Emitter<T
>(options?
)
Type parameters
Name | Type |
---|---|
T | any |
Parameters
Name | Type |
---|---|
options | EmitterOptions <T > |
Defined in
Common Filters
change
▸ change(equalFn?
): Emitter
<T
>
Emits only when the value is changed
Parameters
Name | Type | Default value | Description |
---|---|---|---|
equalFn | (a : T , b : T ) => boolean | isEqual | A comparator function. The event will be emitted when the function returns falsy value. |
Returns
Emitter
<T
>
Defined in
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
Name | Type |
---|---|
value | U |
Returns
Emitter
<U
>
See
https://lodash.com/docs/4.17.15#throttle
Defined in
debounce
▸ debounce(wait
, options
): Emitter
<unknown
>
Creates debounced version of the current emitter.
Parameters
Name | Type | Description |
---|---|---|
wait | number | Milliseconds to wait. |
options | DebounceSettings |
Returns
Emitter
<unknown
>
A new emitter
Defined in
delay
▸ delay(wait
): Emitter
<unknown
>
Creates delayed version of the current emitter.
Parameters
Name | Type | Description |
---|---|---|
wait | number | Milliseconds to wait. |
Returns
Emitter
<unknown
>
A new emitter
Defined in
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
Name | Type | Description |
---|---|---|
fn | (prev : T , curt : T ) => U | A function to calculate the difference |
Returns
Emitter
<U
>
A new emitter
Defined in
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
filter
▸ filter(fn
): Emitter
<T
>
Filters events with the given predicate function
Parameters
Name | Type | Description |
---|---|---|
fn | (value : T ) => any | Return truthy value to pass events |
Returns
Emitter
<T
>
A new emitter
Defined in
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
Name | Type | Description |
---|---|---|
fn | (value : T ) => U | A function to map the current value. Return undefined to skip emitting. |
initialValue? | U | - |
Returns
Emitter
<U
>
Defined in
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
Name | Type | Description |
---|---|---|
fn | (prev : U , value : T ) => U | A function to calculate a new state |
initialState | U | An initial state value |
Returns
Emitter
<U
>
A new emitter
Defined in
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
Name | Type | Default value | Description |
---|---|---|---|
ms | number | 0 | The interval in milliseconds. Set 0 to use requestAnimationFrame . |
immediate | boolean | false | If set to false , the new emitter waits to emit until the current emitter emits any value. |
Returns
Emitter
<any
>
A new emitter.
Defined in
lerp
▸ lerp(lerp
, rate
, threshold?
): Emitter
<T
>
Smoothen the change rate of the input value.
Parameters
Name | Type | Default value | Description |
---|---|---|---|
lerp | Lerp <T > | undefined | A function to interpolate the current value and the target value. |
rate | number | undefined | The ratio of linear interpolation from the current value to the target value with each update. |
threshold | number | 1e-4 | The 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
longPress
▸ longPress(wait
): Object
Parameters
Name | Type |
---|---|
wait | number |
Returns
Object
Name | Type |
---|---|
pressed | Emitter <unknown > |
Defined in
map
▸ map<U
>(fn
, initialValue?
): Emitter
<U
>
Transforms the payload of event with the given function.
Type parameters
Name |
---|
U |
Parameters
Name | Type | Description |
---|---|---|
fn | (value : T ) => U | A function to transform the payload |
initialValue? | U | - |
Returns
Emitter
<U
>
A new emitter
Defined in
not
▸ not(): Emitter
<boolean
>
Creates an emitter whose payload is negated.
Returns
Emitter
<boolean
>
Defined in
resetBy
▸ resetBy(emitter
, emitOnReset?
): Emitter
<T
>
Reset the state of current emitter emitter when the given event is fired.
Parameters
Name | Type | Default value | Description |
---|---|---|---|
emitter | Emitter <any > | undefined | The emitter that triggers the current emitter to be reset. |
emitOnReset | boolean | true | If set to true , the current emitter will be triggered when it is reset. |
Returns
Emitter
<T
>
The current emitter emitter
Defined in
stash
▸ stash(...triggers
): Emitter
<any
>
Creates an emitter that emits the current value when one of the given events is fired.
Parameters
Name | Type | Description |
---|---|---|
...triggers | Emitter <any >[] | Emitters to trigger the current emitter to emit. |
Returns
Emitter
<any
>
A new emitter
Defined in
throttle
▸ throttle(wait
, options?
): Emitter
<T
>
Creates throttled version of the current emitter.
Parameters
Name | Type | Description |
---|---|---|
wait | number | Milliseconds to wait. |
options? | ThrottleSettings |
Returns
Emitter
<T
>
See
https://lodash.com/docs/4.17.15#debounced
Defined in
trail
▸ trail(count?
, emitAtCount?
): Emitter
<T
[]>
Emits an array caching a specified number of values that were emitted in the past.
Parameters
Name | Type | Default value | Description |
---|---|---|---|
count | number | 2 | The number of cache frames. Set 0 to store caches infinitely. |
emitAtCount | boolean | true | When set to true , events will not be emitted until the count of cache reaches to count . |
Returns
Emitter
<T
[]>
Defined in
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
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
Name | Type | Default value | Description |
---|---|---|---|
emitter | Emitter <boolean > | undefined | An emitter to filter the events |
resetOnDown | boolean | true | If set to true , the returned emitter will be reset when the given emitter is down. |
Returns
Emitter
<T
>
Defined in
Methods
createDerived
▸ createDerived<U
>(options
): Emitter
<U
>
Type parameters
Name |
---|
U |
Parameters
Name | Type |
---|---|
options | EmitterOptions <U > & { propagate : (e : T , emit : (v : U ) => void ) => void } |
Returns
Emitter
<U
>
Defined in
registerDerived
▸ registerDerived(emitter
, listener
): void
Parameters
Name | Type |
---|---|
emitter | Emitter <any > |
listener | Listener <T > |
Returns
void
Defined in
tween
▸ tween(lerp
, durationMs
): Emitter
<T
>
Parameters
Name | Type |
---|---|
lerp | Lerp <T > |
durationMs | number |
Returns
Emitter
<T
>
Defined in
Properties
_disposed
• Protected
_disposed: boolean
= false
Defined in
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
icon
• Optional
icon: IconSequence
Defined in
stateful
• get
stateful(): boolean
Returns true
if the emitter has a state and can be reset.
Returns
boolean
Defined in
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
Accessors
disposed
• get
disposed(): boolean
Returns
boolean
Defined in
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
Name | Type | Default value | Description |
---|---|---|---|
emitter | Emitter <any > | undefined | An emitter to filter the current event |
count | number | undefined | The number of emitters to be created. |
resetOnSwitch | boolean | true | If set to true , the corresponding emitter will be reset when the index of current emitter is switched. |
Returns
Emitter
<T
>[]
Defined in
Event Handlers
dispose
▸ dispose(): void
Disposes the emitter immediately and prevent to emit any value in the future
Returns
void
Defined in
emit
▸ emit(value
): void
Manually emits the event.
Parameters
Name | Type |
---|---|
value | T |
Returns
void
Defined in
log
▸ log(message?
): Emitter
<T
>
Parameters
Name | Type | Default value |
---|---|---|
message | string | 'Bndr' |
Returns
Emitter
<T
>
Defined in
off
▸ off(listener
): void
Removes the listener
function from the event.
Parameters
Name | Type |
---|---|
listener | Listener <T > |
Returns
void
Defined in
on
▸ on(listener
): Emitter
<T
>
Adds the listener
function for the event.
Parameters
Name | Type | Description |
---|---|---|
listener | Listener <T > | The callback function |
Returns
Emitter
<T
>
Defined in
once
▸ once(listener
): void
Adds a one-time listener
function for the event
Parameters
Name | Type |
---|---|
listener | Listener <T > |
Returns
void
Defined in
removeAllListeners
▸ removeAllListeners(): void
Removes all listeners.
Returns
void
Defined in
reset
▸ reset(): void
Resets the state of the emitter.
Returns
void