Class Signal<T>

A signal represents a stream of events, a suspension point for coroutines, and a store of value. Signals can be awaited in coroutines using yield, emit values to notify listeners, store the most recently emitted value for reading, and accept callback subscriptions.

Type Parameters

  • T

    The type of values this signal emits

Constructors

  • Create a new signal, optionally with an initial value and/or driver coroutine.

    The value to return from value before the first emit. null by default.

    If a driver coroutine is given, it is immediately run with go passing this as the only parameter. It can be used to associate asynchronous logic with a new Signal.

    Type Parameters

    • T

    Parameters

    • initial: T = null

      Initial value

    • driver: ((self) => Generator<any, any, any>) = null

      Driver coroutine

        • (self): Generator<any, any, any>
        • Parameters

          Returns Generator<any, any, any>

    Returns Signal<T>

Properties

#back: any[] = []
#callbacks: any[] = []
#emitted: boolean = false
#front: any[] = []
#id: number = 0
#value: T
#count: number = 0

Accessors

  • get emitted(): boolean
  • true if this signal is in the process of emitting. Useful to determine which signal emitted coming out of a first.

    Returns boolean

  • get id(): number
  • Automatically generated unique numerical IDs of the Signal

    Returns number

  • get value(): T
  • The most recently emitted value, or the initial value passed to the constructor if not emitted yet.

    Returns T

Methods

  • Parameters

    • exception: any

    Returns void

  • Emits a value through this signal, updating the stored value and notifying all listeners. Resumes any coroutines waiting on this signal and calls all subscribed callbacks.

    Parameters

    • Optional value: T = undefined

      Value to emit and store. If undefined, only triggers notifications without updating stored value

    Returns void

  • Creates a new signal that only emits values that pass the provided predicate function. Values that don't pass the predicate are filtered out and not emitted.

    Parameters

    • f: ((value) => boolean)

      Predicate function to test each value

        • (value): boolean
        • Parameters

          • value: T

          Returns boolean

    Returns Signal<T>

    New signal that emits only filtered values

  • Creates a new signal that accumulates values using a reducer function. Each emission updates the accumulated state and emits the new state.

    Type Parameters

    • TState

    Parameters

    • f: ((state, value) => TState)

      Reducer function that combines state with new values

    • initial: TState

      Initial state value

    Returns Signal<TState>

    New signal that emits accumulated state values

  • Creates a new signal that mirrors this signal's emissions but starts with the given initial value.

    Parameters

    • value: T

      Initial value for the new signal

    Returns Signal<T>

    New signal with the specified initial value

  • Creates a new signal that transforms each emitted value using the provided function. The new signal emits the transformed values and can optionally start with an initial value.

    Type Parameters

    • U

    Parameters

    • f: ((value) => U)

      Function to transform each emitted value

        • (value): U
        • Parameters

          • value: T

          Returns U

    • Optional initial: U

      Optional initial value for the new signal

    Returns Signal<U>

    New signal that emits transformed values

  • Unsubscribes a callback function that was added with on.

    Parameters

    • f: ((value) => void)

      Callback function to remove

        • (value): void
        • Parameters

          • value: T

          Returns void

    Returns void

  • Subscribes a callback function to be called every time this signal emits.

    Parameters

    • f: ((value) => void)

      Callback function to invoke on emission

        • (value): void
        • Parameters

          • value: T

          Returns void

    Returns void

  • Registers a generator to be resumed exactly once when this signal emits. Used internally by go when a coroutine yield this signal.

    Parameters

    • f: Generator<any, any, any>

      Generator to resume on emission

    Returns void

  • Creates a new signal that only emits values that are different from the previous emission. Duplicate consecutive values are filtered out using strict equality (===) comparison.

    Returns Signal<T>

    New signal that emits only when values change

  • Creates a signal that emits when a DOM event fires on the target element. The signal automatically cleans up its event listener when cancelled.

    Parameters

    • eventTarget: EventTarget

      DOM element or event target to listen on

    • event: string

      Event name to listen for (e.g., 'click', 'mousemove', 'keydown')

    • Optional options: boolean | AddEventListenerOptions = true

      Event listener options

    Returns Signal<Event>

    Signal that emits with event data when the event fires

  • Converts various objects to signals for use in coroutines. Signals pass through unchanged, Promises are wrapped in signals that emit when resolved. Throws TypeError for generators/functions - use yield* instead.

    Parameters

    • obj: Promise<any> | Signal<any>

      Object to convert to signal

    Returns Signal<any>

    Signal representation of the object

    Throws

    If object cannot be converted to signal