# Bus

`class` in `core` · v0.2.1

<https://docs.seedcord.org/packages/core/0.2.1/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
emit<TEventKey>(
    event: TEventKey,
    args: TEvents[TEventKey]
): boolean
```

Emits an event, invoking each listener with the payload tuple. Returns true when the event had listeners.

### 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
off<TEventKey>(
    event: TEventKey,
    listener: (...args: TEvents[TEventKey]) => void
): this
```

Removes a previously registered listener.

### on

```ts
on<TEventKey>(
    event: TEventKey,
    listener: (...args: TEvents[TEventKey]) => void
): this
```

Registers a persistent listener for an event.

### once

```ts
once<TEventKey>(
    event: TEventKey,
    listener: (...args: TEvents[TEventKey]) => void
): this
```

Registers a listener removed after its first invocation.

### onListenerError

```ts
protected onListenerError(
    error: unknown,
    event: string | symbol
): 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. A caller cannot assume a side effect has completed. 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
removeListener<TEventKey>(
    event: TEventKey,
    listener: (...args: TEvents[TEventKey]) => void
): this
```

Alias of [`off`](/packages/event-emitter/0.1.5/classes/typed-event-emitter#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.
