7.0.0
The Bus
class represents a special type of Signal
that can broadcast
values to its observers. You can connect other signals to a bus, as well as
manually emit values and errors.
Extends Signal
import { Bus, Signal } from 'bulb'
const bus = new Bus()
// Subscribe to the bus and log emitted values to the console.
bus.subscribe(console.log)
// Emit a value on the bus.
bus.next(0)
// Connect a signal to the bus.
const s = Signal.of(1, 2, 3)
bus.connect(s)
Emits the given value to the observers.
(any)
The value to emit.
Emits the given error to the observers.
(any)
The error to emit.
Completes the bus. All observers will be completed, and any further calls
to next
or error
will be ignored.
Connects the bus to the given signal. Any values emitted by the signal will be forwarded to the bus.
(any)
Subscription
:
A subscription handle.
The Signal
class represents a time-varying source of values – for example,
the value of a text input, a periodic timer, or even the position of the
mouse pointer in the browser window.
When creating a new signal you must provide a mount
function, which when
called will connect the signal to a source of values. This function will
only be called once an observer has subscribed to the signal. This is
because signals are lazy – they don't bother emitting values until an
observer is listening.
The mount
function takes an emit
object as its only argument. This
allows the signal to emit values:
emit.next(a)
- Emits the value a
.emit.error(e)
- Emits the error e
.emit.complete()
- Marks the signal as complete.The mount
function can also optionally return an unmount function, which
when called will disconnect the signal from its source of values. This
function will only be called once all observers have unsubscribed from the
signal.
(Function)
The function that is used to connect the signal with
a source of values. It can optionally return an unmount function.
import { Signal } from 'bulb'
// Create a signal that emits the value 'foo' every second.
const s = new Signal(emit => {
// Start the timer and emit a value whenever the timer fires.
const id = setInterval(() => emit.next('foo'), 1000)
// Return a function to be called when the signal is unmounted.
return () => clearInterval(id)
})
// Subscribe to the signal and log emitted values to the console.
const subscription = s.subscribe(console.log)
// When we are done, we can unsubscribe from the signal.
subscription.unsubscribe()
Concatenates the given signals and emits their values. The returned signal will join the signals, waiting for each one to complete before joining the next, and will complete once all of the signals have completed.
(...Signal)
The signals to concatenate.
Signal
:
A new signal.
import { Signal } from 'bulb'
const s = Signal.of(1, 2, 3)
const t = Signal.of(4, 5, 6)
const u = Signal.concat(s, t)
u.subscribe(console.log) // 1, 2, 3, 4, 5, 6
Creates a signal that immediately emits the values from an iterable
.
The returned signal will complete after the last value in the iterable has
been emitted.
(any)
The iterable that contains the values to be emitted.
Signal
:
A new signal.
import { Signal } from 'bulb'
const s = Signal.from([1, 2, 3])
s.subscribe(console.log) // 1, 2, 3
Creates a signal that wraps a callback.
The executor function f
is passed with a callback
function when the
signal is mounted. The callback
is a standard error-first callback,
which means that if the callback is called with a non-null
first
argument, then the returned signal will emit an error. If the callback is
called with a null
first argument, then the returned signal will emit a
value.
(Function)
The executor function to be passed with a callback
function when the signal is mounted.
Signal
:
A new signal.
import { Signal } from 'bulb'
const s = Signal.fromCallback(callback => {
callback(null, 'foo')
})
s.subscribe(console.log) // 'foo'
Creates a signal that emits events of type
from the
EventTarget
-compatible target
object.
(String)
The event type to listen for.
(EventTarget)
The event target (e.g. a DOM element).
Signal
:
A new signal.
import { Signal } from 'bulb'
Signal.fromEvent('click', document)
Creates a signal that wraps a promise p
. The returned signal will
complete immediately after the promise is resolved.
(Promise)
The promise to wrap.
Signal
:
A new signal.
import { Signal } from 'bulb'
const p = new Promise((resolve, reject) => {
resolve('foo')
})
const s = Signal.fromPromise(p)
s.subscribe(console.log) // 'foo'
Merges the given signals and emits their values. The returned signal will complete once all of the signals have completed.
(...Signal)
The signals to merge.
Signal
:
A new signal.
import { Signal } from 'bulb'
const s = Signal.of(1, 2, 3)
const t = Signal.of(4, 5, 6)
const u = Signal.merge(s, t)
u.subscribe(console.log) // 1, 4, 2, 5, 3, 6
Creates a signal that immediately emits the values
. The returned signal
will complete immediately after the values have been emited.
(...any)
The values to emit.
Signal
:
A new signal.
import { Signal } from 'bulb'
const s = Signal.of(1, 2, 3)
s.subscribe(console.log) // 1, 2, 3
Creates a signal that emits a value every n
milliseconds. The value
emitted starts at zero and increments indefinitely.
(Number)
The number of milliseconds to wait between each value.
Signal
:
A new signal.
import { Signal } from 'bulb'
const s = Signal.periodic(1000)
s.subscribe(console.log) // 0, 1, 2, ...
Creates a signal that emits an error. The returned signal will complete immediately after the error has been emited.
(any)
The error to emit.
Signal
:
A new signal.
import { Signal } from 'bulb'
const s = Signal.error('foo')
s.subscribe({ error: console.error }) // 'foo'
Combines the corresponding values emitted by the given signals into tuples. The returned signal will complete when any of the signals have completed.
(...Signal)
The signals to zip.
Signal
:
A new signal.
import { Signal } from 'bulb'
const s = Signal.of(1, 2, 3)
const t = Signal.of(4, 5, 6)
const u = Signal.zip(s, t)
u.subscribe(console.log) // [1, 4], [2, 5], [3, 6]
Applies the function f
to the corresponding values emitted by the given
signals. The returned signal will complete when any of the signals have
completed.
(Function)
The function to apply to the corresponding values
emitted by the signals.
(...Signal)
The signals to zip.
Signal
:
A new signal.
import { Signal } from 'bulb'
const s = Signal.of(1, 2, 3)
const t = Signal.of(4, 5, 6)
const u = Signal.zipWith((a, b) => a + b, s, t)
u.subscribe(console.log) // 5, 7, 9
Combines the latest values emitted by the given signals into tuples. The returned signal will complete when any of the signals have completed.
(...Signal)
The signals to zip.
Signal
:
A new signal.
import { Signal } from 'bulb'
const s = Signal.of(1, 2, 3)
const t = Signal.of(4, 5, 6)
const u = Signal.zipLatest(s, t)
u.subscribe(console.log) // [1, 4], [2, 5], [3, 6]
Applies the function f
to the latest values emitted by the given signals.
The returned signal will complete when any of the signals have completed.
(Function)
The function to apply to the corresponding values
emitted by the signals.
(...Signal)
The signals to zip.
Signal
:
A new signal.
import { Signal } from 'bulb'
const s = Signal.of(1, 2, 3)
const t = Signal.of(4, 5, 6)
const u = Signal.zipLatestWith((a, b) => a + b, s, t)
u.subscribe(console.log) // 5, 7, 9
Emits true
if all the values emitted by the signal satisfy a predicate
function p
. The returned signal will complete if the signal emits any
value that doesn't satisfy the predictate function.
(Function)
The predicate function to apply to each value emitted
by the signal.
Signal
:
A new signal.
import { Signal } from 'bulb'
const s = Signal
.of(1, 2, 3)
.all(a => a > 0)
s.subscribe(console.log) // true
Emits true
if any of the values emitted by the signal satisfy a
predicate function p
. The returned signal will complete if the signal
emits any value that satisfies the predictate function.
(Function)
The predicate function to apply to each value emitted
by the signal.
Signal
:
A new signal.
import { Signal } from 'bulb'
const s = Signal
.of(1, 2, 3)
.any(a => a < 0)
s.subscribe(console.log) // false
Applies the latest function emitted by the signal to latest values emitted by the given signals. The returned signal will complete when any of the signals have completed.
The latest function will be called with a number of arguments equal to the
number of signals. For example, if the latest function is (a, b) => a + b
,
then you will need to supply two signals.
(...Signal)
The value signals.
Signal
:
A new signal.
import { Signal } from 'bulb'
const s = Signal.of(1, 2, 3)
const t = Signal.of(4, 5, 6)
const u = Signal
.of((a, b) => a + b)
.apply(s, t)
u.subscribe(console.log) // 5, 7, 9
Buffers values emitted by the signal and emits the buffer contents when it is full. The buffer contents will be emitted when the signal completes, regardless of whether the buffer is full.
(Number
= Infinity
)
The size of the buffer. If the size is set to
Infinity
, then the signal will be buffered until it completes.
Signal
:
A new signal.
import { Signal } from 'bulb'
const s = Signal
.of(1, 2, 3, 4)
.buffer(2)
s.subscribe(console.log) // [1, 2], [3, 4], ...
Buffers values emitted by the signal and emits the buffer contents whenever there is an event on the given control signal. The buffer contents will be emitted when the signal completes, regardless of whether the buffer is full.
(Signal)
The control signal.
Signal
:
A new signal.
import { Signal } from 'bulb'
const t = Signal.periodic(1000)
const s = Signal
.of(1, 2, 3, 4)
.bufferWith(t)
s.subscribe(console.log) // [1, 2], [3, 4], ...
Applies a function f
, that returns a Signal
, to the first error
emitted by the signal. The returned signal will emit values from the
signal returned by the function.
(Function)
The function to apply to an error emitted by the
signal. It must also return a
Signal
.
Signal
:
A new signal.
import { Signal } from 'bulb'
const s = Signal
.throwError()
.catchError(e => Signal.of(1))
s.subscribe(console.log) // 1
Concatenates the given signals and emits their values. The returned signal will join the signals, waiting for each one to complete before joining the next, and will complete once all of the signals have completed.
(...Signal)
The signals to concatenate.
Signal
:
A new signal.
import { Signal } from 'bulb'
const s = Signal.of(1, 2, 3)
const t = Signal.of(4, 5, 6)
const u = s.concat(t)
u.subscribe(console.log) // 1, 2, 3, 4, 5, 6
Applies a function f
, that returns a Signal
, to each value emitted by
the signal. The returned signal will join all signals returned by the
function, waiting for each one to complete before merging the next.
(Function)
The function to apply to each value emitted by the
signal. It must also return a
Signal
.
Signal
:
A new signal.
import { Signal } from 'bulb'
const s = Signal
.of(1, 2, 3)
.concatMap(a => Signal.of(a + 1))
s.subscribe(console.log) // 2, 3, 4
Cycles through the given values as values are emitted by the signal.
(...any)
The values to emit.
Signal
:
A new signal.
import { Signal } from 'bulb'
const s = Signal
.periodic(1000)
.cycle(1, 2, 3)
s.subscribe(console.log) // 1, 2, 3, 1, 2, 3, ...
Waits until n
milliseconds after the last burst of values before
emitting the most recent value from the signal.
(Number)
The number of milliseconds to wait between each burst of
values.
Signal
:
A new signal.
import { Mouse } from 'bulb-input'
const s = Mouse
.position(document)
.debounce(1000)
s.subscribe(console.log) // [1, 1], [2, 2], ...
Removes duplicate values emitted by the signal using a comparator function
f
.
(Function)
The comparator function to apply to successive values
emitted by the signal. If the value is distinct from the previous value,
then the comparator function should return
true
, otherwise it should
return
false
.
Signal
:
A new signal.
import { Signal } from 'bulb'
const s = Signal
.of(1, 2, 2, 3, 3, 3)
.dedupeWith((a, b) => a === b)
s.subscribe(console.log) // 1, 2, 3
Drops values emitted by the signal while the predicate function p
is
satisfied. The returned signal will emit values once the predicate
function is not satisfied.
(Function)
The predicate function to apply to each value emitted
by the signal. If it returns
true
, the value will not be emitted,
otherwise the value will be emitted.
Signal
:
A new signal.
import { Signal } from 'bulb'
const s = Signal
.of(1, 2, 3)
.dropWhile(a => a < 2)
s.subscribe(console.log) // 2, 3
Switches between the given signals based on the most recent value emitted by the signal. The values emitted by the signal represent the index of the signal to switch to.
(...Signal)
The signals to encode.
Signal
:
A new signal.
import { Signal } from 'bulb'
const s = Signal.of(1)
const t = Signal.of(2)
const u = Signal
.periodic(1000)
.sequential(0, 1)
.encode(s, t)
u.subscribe(console.log) // 1, 2
Filters the signal by only emitting values that satisfy a predicate
function p
.
(Function)
The predicate function to apply to each value emitted
by the signal. If it returns
true
, the value will be emitted, otherwise
the value will not be emitted.
Signal
:
A new signal.
import { Signal } from 'bulb'
const s = Signal
.of(1, 2, 3)
.filter(a => a > 1)
s.subscribe(console.log) // 2, 3
Applies an accumulator function f
to each value emitted by the signal.
The accumulated value will be emitted when the signal has completed.
(Function)
The accumulator function to apply to each value
emitted by the signal.
(any)
The initial value.
Signal
:
A new signal.
import { Signal } from 'bulb'
const s = Signal
.of(1, 2, 3)
.fold((a, b) => a + b, 0)
s.subscribe(console.log) // 6
Stops emitting values from the signal while the given control signal is truthy.
(Signal)
The control signal.
Signal
:
A new signal.
import { Mouse } from 'bulb-input'
const s = Mouse.position(document)
const t = Mouse.button(document)
const u = s.hold(t)
u.subscribe(console.log) // [1, 1], [2, 2], ...
Merges the given signals and emits their values. The returned signal will complete once all of the signals have completed.
(...Signal)
The signals to merge.
Signal
:
A new signal.
import { Signal } from 'bulb'
const s = Signal.of(1, 2, 3)
const t = Signal.of(4, 5, 6)
const u = s.merge(t)
u.subscribe(console.log) // 1, 4, 2, 5, 3, 6
Emits the given values before any other values are emitted by the signal.
(...any)
The values to prepend.
Signal
:
A new signal.
import { Signal } from 'bulb'
const s = Signal
.of(1, 2, 3)
.prepend(4, 5, 6)
s.subscribe(console.log) // 4, 5, 6, 1, 2, 3
Emits the most recent value from the signal whenever there is an event on the given control signal.
(Signal)
The control signal.
Signal
:
A new signal.
import { Mouse } from 'bulb-input'
import { Signal } from 'bulb'
const s = Mouse.position(document)
const t = Signal.periodic(1000)
const u = s.sample(t)
u.subscribe(console.log) // [1, 1], [2, 2], ...
Applies an accumulator function f
to each value emitted by the signal.
The accumulated value will be emitted for each value emitted by the
signal.
(Function)
The accumulator function to apply to each value
emitted by the signal.
(any)
The initial value.
Signal
:
A new signal.
import { Signal } from 'bulb'
const s = Signal
.of(1, 2, 3)
.scan((a, b) => a + b, 0)
s.subscribe(console.log) // 1, 3, 6
Emits the next value from the given values for every value emitted by the signal. The returned signal will complete immediately after the last value has been emitted.
(...any)
The values to emit.
Signal
:
A new signal.
import { Signal } from 'bulb'
const s = Signal
.periodic(1000)
.sequential(1, 2, 3)
s.subscribe(console.log) // 1, 2, 3
Applies a transform function f
to each value emitted by the signal.
The transform function must return a new state, it can also optionally
emit values or errors using the emit
object.
(Function)
The transform function to apply to each value emitted
by the signal.
(any)
The initial state.
Signal
:
A new signal.
import { Signal } from 'bulb'
const s = Signal
.of(1, 2, 3)
.stateMachine((a, b, emit) => {
emit.next(a + b)
return a * b
}, 1)
s.subscribe(console.log) // 1, 3, 5
Subscribes an observer to the signal.
The subscribe
method returns a subscription handle, which can be used to
unsubscribe from the signal.
(Function?)
The callback function called when the signal
emits a value.
(Function?)
The callback function called when the signal
emits an error.
(Function?)
The callback function called when the
signal has completed.
Subscription
:
A subscription handle.
import { Signal } from 'bulb'
const s = Signal.of(1, 2, 3)
// Subscribe to the signal and log emitted values to the console.
const subscription = s.subscribe(console.log)
// When we are done, we can unsubscribe from the signal.
subscription.unsubscribe()
Subscribes to the most recent signal emitted by the signal (a signal that emits other signals). The returned signal will emit values from the most recent signal.
Signal
:
A new signal.
import { Signal } from 'bulb'
const s = Signal.of(1)
const t = Signal.of(2)
const u = Signal
.periodic(1000)
.sequential(s, t)
.switchLatest()
u.subscribe(console.log) // 1, 2
Applies a function f
, that returns a Signal
, to each value emitted by
the signal. The returned signal will emit values from the most recent
signal returned by the function.
(Function)
The function to apply to each value emitted by the
signal. It must also return a
Signal
.
Signal
:
A new signal.
import { Signal } from 'bulb'
const s = Signal
.of(1, 2, 3)
.switchMap(a => Signal.of(a + 1))
s.subscribe(console.log) // 2, 3, 4
Emits values from the signal until the given control signal emits a value. The returned signal will complete once the control signal emits a value.
(Signal)
The control signal.
Signal
:
A new signal.
const s = Signal.periodic(1000)
const t = Signal.of().delay(1000)
const u = s.takeUntil(t)
u.subscribe(console.log) // 0
Emits values from the signal while the predicate function p
is
satisfied. The returned signal will complete once the predicate function
is not satisfied.
(Function)
The predicate function to apply to each value emitted
by the signal. If it returns
true
, the value will be emitted, otherwise
the value will not be emitted.
Signal
:
A new signal.
const s = Signal
.of(1, 2, 3)
.takeWhile(a => a < 2)
s.subscribe(console.log) // 1
Performs the side effect function f
for each value emitted by the
signal. The returned signal contains the same events as the original
signal.
(Function)
The function to apply to each value emitted by the
signal.
Signal
:
A new signal.
import { Signal } from 'bulb'
const s = Signal
.of(1, 2, 3)
.tap(console.log)
s.subscribe() // 1, 2, 3
Limits the rate at which values are emitted by the signal to one every n
milliseconds. Values will be dropped when the rate limit is exceeded.
(Number)
The number of milliseconds to wait between each value.
Signal
:
A new signal.
import { Mouse } from 'bulb-input'
const s = Mouse
.position(document)
.throttle(1000)
s.subscribe(console.log) // [1, 1], [2, 2], ...
Returns a higher-order signal that emits the windowed values of this signal.
(Signal)
The control signal.
Signal
:
A new signal.
import { Signal } from 'bulb'
const t = Signal.periodic(1000)
const s = Signal
.of(1, 2, 3, 4)
.window(t)
.switchLatest()
s.subscribe(console.log) // 1, 2, X, 3, 4, X, ...
Combines the corresponding values emitted by the given signals into tuples. The returned signal will complete when any of the signals have completed.
(...Signal)
The signals to zip.
Signal
:
A new signal.
import { Signal } from 'bulb'
const s = Signal.of(1, 2, 3)
const t = Signal.of(4, 5, 6)
const u = s.zip(t)
u.subscribe(console.log) // [1, 4], [2, 5], [3, 6]
Applies the function f
to the corresponding values emitted by the given
signals. The returned signal will complete when any of the signals have
completed.
(Function)
The function to apply to the corresponding values
emitted by the signals.
(...Signal)
The signals to zip.
Signal
:
A new signal.
import { Signal } from 'bulb'
const s = Signal.of(1, 2, 3)
const t = Signal.of(4, 5, 6)
const u = s.zipWith((a, b) => a + b, t)
u.subscribe(console.log) // 5, 7, 9
Combines the latest values emitted by the given signals into tuples. The returned signal will complete when any of the signals have completed.
(...Signal)
The signals to zip.
Signal
:
A new signal.
import { Signal } from 'bulb'
const s = Signal.of(1, 2, 3)
const t = Signal.of(4, 5, 6)
const u = s.zipLatest(t)
u.subscribe(console.log) // [1, 4], [2, 5], [3, 6]
Applies the function f
to the latest values emitted by the given signals.
The returned signal will complete when any of the signals have completed.
(Function)
The function to apply to the corresponding values
emitted by the signals.
(...Signal)
The signals to zip.
Signal
:
A new signal.
import { Signal } from 'bulb'
const s = Signal.of(1, 2, 3)
const t = Signal.of(4, 5, 6)
const u = s.zipLatestWith((a, b) => a + b, t)
u.subscribe(console.log) // 5, 7, 9
The Subscription
class represents an observer who has subscribed to
a Signal
.