constructor<T>(value: T)
View sourceCreates a signal with an initial current value.
Construction does not notify listeners because none are registered yet.
Class
Publishes updates to one mutable value by notifying registered listeners synchronously.
import { Signal } from '@wimaengine/event'
const health = new Signal<number>(100)
const reportHealth = (value) => console.log(`Health: ${value}`)
health.addListener(reportHealth)
health.setValue(75) // logs "Health: 75" before setValue() returns
console.log(health.getValue()) // 75
health.removeListener(reportHealth)
health.setValue(50) // no longer logsconstructor<T>(value: T)Creates a signal with an initial current value.
Construction does not notify listeners because none are registered yet.
Registered listener callbacks, kept in the order they were added.
Each call to Signal.addListener adds one registration; adding the same
callback more than once therefore creates separate entries.
Default: []
The current value held by this signal.
Signal.setValue assigns the new value before notifying listeners, so
callbacks can read the updated value with Signal.getValue.
Assigning this public field directly changes the current value without
notifying listeners; use Signal.setValue to publish an update.
getValue(): TReturns the signal’s current value without notifying listeners.
setValue(x: T): voidReplaces the current value and synchronously notifies listeners.
The assignment happens before the first callback. Each callback receives
the signal’s current Signal.value at its turn, normally the value passed
as x; the previous value is not passed. Assigning the same value still
notifies listeners.
Notification walks the live listener list. On an unchanged list, callbacks
run in registration order and duplicate registrations are visited once per
entry. A callback that uses Signal.addListener can append an entry that
is reached later in this call, while Signal.removeListener can shift
entries and cause a later entry to be skipped. A listener exception
propagates to the caller and stops notification of later entries.
const signal = new Signal(0)
const seen = []
signal.addListener((value) => seen.push([value, signal.getValue()]))
signal.setValue(1)
console.log(seen) // [[1, 1]]: the listener ran before setValue returnedaddListener(listener: signalListener<T>): voidRegisters a listener for future value updates.
Registration appends one callback entry and does not invoke it with the
current value. If the same callback is added more than once, each
registration is a separate entry: Signal.setValue can notify the
callback once for each entry, and Signal.removeListener removes them one
at a time.
const signal = new Signal(0)
const seen = []
const listener = (value) => seen.push(value)
signal.addListener(listener)
signal.addListener(listener)
signal.setValue(1)
console.log(seen) // [1, 1]removeListener(listener: signalListener<T>): voidRemoves the first registration matching a listener callback.
The search starts at the first entry and uses callback identity. If the
callback was registered multiple times, later registrations remain and a
second call removes the next matching entry. Removing a callback that is
not registered is a no-op and returns undefined.
const signal = new Signal(0)
const listener = () => console.log('called')
signal.addListener(listener)
signal.addListener(listener)
signal.removeListener(listener)
signal.setValue(1) // logs "called" once: one registration remains