Skip to content

Class: MessageBus

MessageBus is the core link between each part of the Engine, it handles processing messages and deciding which subscribers should recieve each message. An object can subscribe to a certain message, the message bus will keep track of subscribers for a message type. Messages can be sent to the message bus, which will be queued up. A dispatch can be triggered on the message bus, which will cause the message bus to send all the messages and to the subscribers that have subscribed to the message type. The dispatch should probably be left to the core game loop for triggering.

Implements

Table of contents

Constructors

Methods

Constructors

constructor

+ new MessageBus(subscribers?: Record<string, ISubscriber[]>, messageQueue?: IMessage[]): MessageBus

Parameters:

Name Type
subscribers Record<string, ISubscriber[]>
messageQueue IMessage[]

Returns: MessageBus

Methods

Dispatch

Dispatch(): void

Dispatch processes the current message bus queue and forwards the messages to the subscribers who have subscribed to each message type.

Returns: void


DispatchUntilEmpty

DispatchUntilEmpty(): void

DispatchUntilEmpty repeatedly dispatches until the message queue is empty, used to make sure everything is processed, e.g. if there is a message that causes a new message to be added, it will ensure that all recursive messages are processed.

Returns: void


Publish

Publish(message: IMessage): void

Publish adds a message to the message bus queue to be dispatched.

Parameters:

Name Type
message IMessage

Returns: void


Subscribe

Subscribe(subscriber: ISubscriber, types: string | string[]): void

Subscribe subscibes a subscriber to a particular message type or types.

Parameters:

Name Type
subscriber ISubscriber
types string | string[]

Returns: void


Unsubscribe

Unsubscribe(subscriber: ISubscriber, types: string | string[]): void

Unsubscribe unsubscribes a subscriber from a specific message type or types.

Parameters:

Name Type
subscriber ISubscriber
types string | string[]

Returns: void


UnsubscribeAll

UnsubscribeAll(subscriber: ISubscriber): void

UnsubscribeAll unsubscribes a Subscriber from all messages.

Parameters:

Name Type
subscriber ISubscriber

Returns: void