strongbus - v3.0.0
    Preparing search index...

    Class Bus<TEventMap>

    Emit events and tear down a Bus instance.

    Extends EmittingSurface with ControlSurface.destroy. Prefer EmittingSurface when a dependency may raise events but must not destroy the bus.

    Event-map typing is bivariant (no in out) so a surface over a wider map is assignable to a narrower view — the same Wide→Narrow shape as Bus.

    Type Parameters

    Hierarchy (View Summary)

    Implements

    Index

    Constructors

    Properties

    name: string

    Subscribe to meta changes. Provided by Bus after lifecycle setup.

    any: SubscriptionSurfaceAny<TEventMap> = ...

    Handle multiple events with the same handler. EventSink receives raised event as first argument, payload as second argument. Duplicate any with the same event set (order-independent) and handler honors Options.duplicateSubscriptionStrategy.

    tap: SubscriptionSurfaceTap<TEventMap> = ...

    Observe every raised event as a correlated {event, payload} message. Does not create a graph edge. Unsubscribe via the returned Subscription.

    pipe: SubscriptionSurfacePipe<TEventMap> = ...

    Attach a graph edge to another Bus, or return a FilteredPipeHandle when given a PipePredicate for gated multi-hop relay.

    • pipe(dest) — first-hop / local raises always deliver to dest.
    • pipe(predicate).pipe(dest) — stores predicate on the edge; passthrough (upstream-sourced) events are delivered only when predicate returns true. Unfiltered outbound edges from a bus that already has inbound pipes warn once per unique unsound path and block passthrough.

    Requires a real Bus instance — not a hand-rolled surface duck type.

    unpipe: <TDownstream extends Bus<any>>(downstream: TDownstream) => void = ...

    Stop piping events into a bus previously passed to Bus.pipe.

    next: SubscriptionSurfaceNext<TEventMap> = ...

    Utility for resolving/rejecting a promise based on the reception of an event. Promise resolves with the triggering event and its payload as {event, payload}.

    Triggers must be SubscribableListenable values. The '*' wildcard is not accepted (see SubscriptionSurface.next).

    specific event(s) that resolve the promise

    specific event(s) that reject the promise. Must be mutually disjoint with resolutionTrigger

    // resolve on the first of several events
    const {event, payload} = await bus.next(['message', 'connected', 'count']);

    // conditional or filtered resolution — use scan (supports trigger: '*')
    const ready = await bus.scan('*', (resolve) => {
    if (resolve.trigger.type === 'event' && isReady(resolve.trigger)) {
    resolve(true);
    }
    });
    scan: SubscriptionSurfaceScan<TEventMap> = ...

    Utility for resolving/rejecting a promise based on an evaluation done when an event is triggered. If options.eager is true (default), evaluates the condition immediately. If the evaluator resolves or rejects during eager evaluation, the scanner does not subscribe to any events.

    Listenable event or events that trigger the evaluator, including '*'. Discriminate on resolve.trigger when reading event or payload.

    checks for a certain state and may resolve or reject the scan.

    attempt to pool scanners that share the same evaluator and trigger; default true.

    cancel the scan after N milliseconds. Values <= 0 are ignored. Configuring a timeout disables pooling even when options.pool is true.

    call the evaluator immediately; default true. This eliminates the anti-pattern of guarding scan with if (!condition).

    hasListenersFor: IntrospectionSurfaceHasListenersForEvent<TEventMap> = ...
    getListenerCountFor: IntrospectionSurfaceListenerCountForEvent<TEventMap> = ...
    getListenersFor: IntrospectionSurfaceListenerForEvent<TEventMap> = ...
    forEach: IntrospectionSurfaceListenerForEach<TEventMap> = ...

    Accessors

    • set defaultAllowUnhandledEvents(allow: boolean): void

      Parameters

      • allow: boolean

      Returns void

      Use Bus.configure with onUnhandledEvent instead.

    • get active(): boolean

      The active state of the bus, i.e. does it have any subscribers. Subscribers include downstreams and scanners.

      Returns boolean

    Methods

    • Merge options onto Bus static defaults for all subsequently constructed instances. Nested thresholds are merged recursively. name cannot be set here; pass it to the constructor for per-instance naming.

      Parameters

      Returns void

    • Emit a void event with no payload (or an explicit null/undefined).

      Type Parameters

      • T extends string | number | symbol

      Parameters

      • event: T
      • Optionalpayload: null

      Returns boolean

    • Emit an event with its correlated payload. The payload is required for any event whose mapped type is not void; correlating it directly (rather than through a rest tuple) lets it type-check even when TEventMap is a generic type parameter, or when forwarding by a generic key over a concrete map (T extends keyof M).

      Type Parameters

      • T extends string | number | symbol

      Parameters

      Returns boolean

    • Correlated-tuple form: the (event, payload) pair must be one of the [event, payload] tuples of TEventMap. Use this after discriminating on event when both values started as independent unions.

      Parameters

      • ...args: { [K in string | number | symbol]: [event: K, payload: TEventMap[K]] }[keyof TEventMap]

      Returns boolean

    • Remove a handler previously registered with Bus.on. Pass the same function reference; no-op if that handler is not registered for event. Honors duplicateSubscriptionStrategy.disposal (collapse clears all stacked on intent; stack pops the oldest frame — head of the stack). Does not remove Bus.once / Bus.any / Bus.tap intent.

      Type Parameters

      • T extends string | number | symbol

      Parameters

      Returns void

    • Subscribe to meta states of the Bus, idle and active. Bus becomes idle when it goes from 1 to 0 subscribers, and active when it goes from 0 to 1. The handler receives a boolean indicating if the bus is active (true) or idle (false)

      Parameters

      • handler: (activeState: boolean) => void

      Returns Subscription

    • Remove all event subscribers, lifecycle subscribers, and downstreams. Triggers lifecycle meta events for all subscribed events before removing lifecycle subscribers, emitting Lifecycle.willDestroy during teardown.

      Returns void