# Bus

`class` in `core` · v0.13.0

<https://seedcord.org/docs/packages/core/0.13.0/classes/bus>

Registers and dispatches subscribers. Subscribers run on a programmatic or framework publish.
Accessed via `core.bus`. Do not construct it directly.

```ts
class Bus extends TypedEventEmitter<SubscriptionTuples>
```

## Constructors

### constructor

```ts
Bus(core: CoreBase)
```

Constructs a new instance of the `Bus` class

## Properties

### core

```ts
protected core: CoreBase
```

## Methods

### emit

```ts
public override emit<KeyOfSubscribers>(
    event: KeyOfSubscribers,
    _args: SubscriptionTuples[KeyOfSubscribers]
): never
```

### eventNames

```ts
eventNames(): EventKey<TEvents>[]
```

Returns the event names with at least one listener.

### listenerCount

```ts
listenerCount<TEventKey>(event: TEventKey): number
```

Counts the listeners registered for an event.

### off

```ts
public override off<KeyOfSubscribers>(
    event: KeyOfSubscribers,
    listener: BusListener<KeyOfSubscribers>
): this
```

Removes a listener that [`on`](/docs/packages/core/0.13.0/classes/bus#on) or [`once`](/docs/packages/core/0.13.0/classes/bus#once) registered.

### on

```ts
public override on<KeyOfSubscribers>(
    event: KeyOfSubscribers,
    listener: BusListener<KeyOfSubscribers>
): this
```

Registers a listener that runs on every publish of the key. The listener can be async. `publish`
catches what it throws, and an edge bot keeps the request open until it finishes.

### once

```ts
public override once<KeyOfSubscribers>(
    event: KeyOfSubscribers,
    listener: BusListener<KeyOfSubscribers>
): this
```

Registers a listener that runs on the next publish of the key only. It can be async, like [`on`](/docs/packages/core/0.13.0/classes/bus#on).

### onListenerError

```ts
protected onListenerError(
    error: unknown,
    event: string | symbol
): void
```

### onListenerRun

```ts
protected onListenerRun(
    run: Promise<void>
): void
```

### publish

```ts
public publish<KeyOfSubscribers>(
    event: KeyOfSubscribers,
    data: AllSubscriptions[KeyOfSubscribers]
): boolean
```

Publishes an event to its subscribers and native listeners.

Fire-and-forget. Subscriber handlers run asynchronously and this returns before they finish.
Errors thrown by a subscriber or by an `on()` listener are caught and logged, never surfaced
here. One throwing listener does not stop the others. Subscribers on one key run concurrently
and carry no ordering guarantee. A `'once'` subscriber is marked as executed when it starts,
even if it throws. It never runs twice.

The framework's own keys are excluded. Subscribe to those and listen with `on`. The framework
is their only publisher.

### removeAllListeners

```ts
removeAllListeners(
    event?: EventKey<TEvents>
): this
```

Removes every listener for an event, or for all events when no event is given.

### removeListener

```ts
public override removeListener<KeyOfSubscribers>(
    event: KeyOfSubscribers,
    listener: BusListener<KeyOfSubscribers>
): this
```

Alias of [`off`](/docs/packages/core/0.13.0/classes/bus#off).

### waitFor

```ts
async waitFor<TEventKey>(
    event: TEventKey,
    options?: WaitForOptions<TEvents[TEventKey]>
): Promise<TEvents[TEventKey]>
```

Waits for the next emission of an event, resolving with its payload tuple.
With a filter, resolves on the first payload the filter accepts.
