Flocc
    Preparing search index...

    Class EventBus

    A publish-subscribe event bus for agent and environment events.

    const events = new EventBus();
    const env = new Environment({ events });

    // Subscribe to events
    events.on('food:found', (event) => {
    console.log('Food found at', event.data.location);
    });

    // Emit events
    events.emit('food:found', { location: { x: 10, y: 20 } });

    0.6.0

    Index

    Constructors

    Methods

    • Emit an event to all subscribers.

      Type Parameters

      • T = any

      Parameters

      • type: string

        Event type

      • data: T

        Event payload

      • source: Agent | Environment = null

        The agent or environment emitting the event

      Returns void

      events.emit('predator:attack', { 
      target: preyAgent,
      damage: 10
      }, predatorAgent);
    • Get the number of handlers for an event type.

      Parameters

      • type: string

        Event type to check

      Returns number

    • Check if there are any handlers for an event type.

      Parameters

      • type: string

        Event type to check

      Returns boolean

    • Remove all handlers for a specific event type.

      Parameters

      • type: string

        Event type to clear handlers for

      Returns void

    • Subscribe to an event type.

      Type Parameters

      • T = any

      Parameters

      • type: string

        Event type to listen for

      • handler: EventHandler<T>

        Function to call when event is emitted

      Returns () => void

      Unsubscribe function

      const unsubscribe = events.on('tick:end', (event) => {
      console.log('Tick completed at time', event.time);
      });

      // Later, to stop listening:
      unsubscribe();
    • Subscribe to an event type, but only fire once.

      Type Parameters

      • T = any

      Parameters

      • type: string

        Event type to listen for

      • handler: EventHandler<T>

        Function to call when event is emitted

      Returns () => void

      Unsubscribe function (can cancel before event fires)