Options
All
  • Public
  • Public/Protected
  • All
Menu

Provides publish/subscribe functionality.

example
import { tracker } from '~/tracking';

const bus = events.bus();

bus.on('event', function handler(arg1, arg2) {
console.log(`received ${arg1} and ${arg2}`);
return arg1 + arg2;
});

// subscribers can be asynchronous
bus.on('event', async function handler(arg1, arg2) {
const result = await someAsyncMethod(arg1);
await someOtherAsyncMethod(result, arg2);
return 'abc';
});

// fire and forget
bus.fire('event', 1, 2);

// catch any rejected promises returned by
// handlers (or errors thrown by handlers)
await bus.fire('event', 1, 2).catch(tracker.error);

// examine the return values of handlers
const results = await bus.fire('event', 1, 2);
console.log(results); // [3, 'abc']

Hierarchy

Index

Methods

  • fire(event: string | Symbol, ...args: any[]): Promise<any[]>
  • Notifies any subscribers registered through on or one that the specified event has occurred. If any subscribers throw an exception then the fire promise will be rejected, but any other subscribers will continue to be notified of the initial event.

    example
    import { tracker } from '~/tracking';

    const bus = events.bus();

    bus.on('event', async function handler(value1, value2) {
    return await someAsyncMethod(value1, value2);
    });

    bus.fire('event', arg1, arg2);
    const results = await bus.fire('event', arg1, arg2);
    await bus.fire('event', arg1, arg2).catch(tracker.error);
    example
    const bus = events.bus();

    async function dirtyCheck(container, path) {
    if (container.dirty)
    throw errors.error('save your changes');
    }

    async function navigate(container, path) {
    // load new route
    }

    bus.on('navigate', functions.sequence(dirtyCheck, navigate));

    export async function navigate(container, path) {
    await bus.fire('navigate', container, path);
    }

    // caller
    function linkHandler(e) {
    e.preventDefault();
    e.stopPropagation();
    const route = e.target.getAttribute('route');
    const container = e.target.getAttribute('container');
    navigate(container, route).then(
    () => console.info('navigation complete'),
    (err) => console.log('navigation failed', err);
    );
    }
    example
    const bus1 = events.bus(null, functions.sequence);
    const bus2 = events.bus(null, functions.parallel); // the default behavior

    function handler1() {
    return 1;
    }

    function handler2() {
    return 2;
    }

    bus1.on('event', handler1);
    bus1.on('event', handler2);

    // sequence bus returns last subscriber's return value
    await bus1.fire('event'); // 2

    bus2.on('event', handler1);
    bus2.on('event', handler2);

    // parallel bus returns array
    await bus2.fire('event'); // [1, 2]

    Parameters

    • event: string | Symbol

      The name of the event to fire.

    • Rest ...args: any[]

      Optional arguments to pass to subscribers.

    Returns Promise<any[]>

    Returns false if the bus is stopped. Otherwise, returns a Promise that will resolve with an array of values returned by event subscribers, or reject with the first Promise rejection or thrown error.

  • on(event: string | Symbol, subscriber: Function): VoidFunction
  • Registers a subscriber for the given event. The subscriber will be invoked in the context used to create the EventBus and passed any arguments provided to the fire method.

    example
    import { createSomething } from '../someFactory';

    const obj = createSomething();
    const bus = events.bus(obj); // subscriber context

    bus.one('initialize', function init() {
    // this only runs the first time
    // the 'initialize' event is fired;
    });

    export const off = bus.on('some-event', function handler(arg) {
    console.log(this === obj); // true
    });

    Parameters

    • event: string | Symbol

      The name of the event to listen for.

    • subscriber: Function

      The subscriber to invoke when the event is fired.

    Returns VoidFunction

    Method to invoke to remove the subscriber.

  • one(event: string | Symbol, subscriber: Function): VoidFunction
  • Similar to on, except the subscriber will be removed as soon as it is invoked.

    Parameters

    • event: string | Symbol

      The name of the event to listen for.

    • subscriber: Function

      The subscriber to invoke when the event is fired.

    Returns VoidFunction

    Method to invoke to remove the subscriber.

  • resume(): void
  • Resumes notifying subscribers after stop was called. Any events fired before resuming are dropped entirely.

    example
    const bus = events.bus();

    bus.on('add', function handler(value1, value2) {
    console.log(value1 + value2);
    });

    bus.fire('add', 1, 2); // 3
    bus.stop();
    bus.fire('add', 1, 2); // does not invoke subscriber
    bus.resume();
    bus.fire('add', 1, 2); // 3

    Returns void

  • stop(): void
  • Stops notifying subscribers of fired events until resume is called.

    example
    const bus = events.bus();

    bus.on('add', function handler(value1, value2) {
    console.log(value1 + value2);
    });

    bus.fire('add', 1, 2); // 3
    bus.stop();
    bus.fire('add', 1, 2); // does not invoke subscriber

    Returns void