Class EventSource<Q, T>

Event emitter class this class allow you to emit and handle events

@example:


interface EvDoIt extends BasicEvent {
param: unknown;
}

function EvDoIt( e: EvDoIt ) : EvDoIt {
return BasicEvent<EvDoIt>( e );
}

interface TestEventMap extends EventMap {
doit: EvDoIt;
}

let ee = new EventSource<TestEventMap>(null);
ee.listen({
doit: (e) => {
console.log(e);
e.preventDefault();
},
});

ee.defaults({
doit: (e) => {
console.log('default handler for ', e.type, e.selection);
},
})

ee.on('doit', (e) => {
debugger;
})

const ev = EvDoIt({ param: 10 });
ee.emit('change', ev);
if (ev.defaultPrevented) {
console.log('prevented');
}

Type Parameters

Hierarchy

Constructors

Properties

m_defaultHandlers: Map<string, EventCallback<BasicEvent>[]>
m_eventRegistry: Map<string, EventCallback<BasicEvent>[]>
m_source: unknown

Methods

  • define a set of default handlers in one call

    Parameters

    • events: T

    Returns void

  • emit an event you can stop propagation of event or prevent default

    Type Parameters

    • K extends string | number | symbol

    Parameters

    • type: K
    • Optional event: Q[K]

      event data

    Returns void

  • define a set of listeners in one call

    Parameters

    • events: T

    Returns void

  • stop listening to an event

    Type Parameters

    • K extends string | number | symbol

    Parameters

    • type: K
    • callback: ((ev) => any)

      callback to remove (must be the same as in on )

        • (ev): any
        • Parameters

          • ev: Q[K]

          Returns any

    Returns void

  • listen for an event

    Type Parameters

    • K extends string | number | symbol

    Parameters

    • type: K
    • callback: ((ev) => any)

      callback to call

        • (ev): any
        • Parameters

          • ev: Q[K]

          Returns any

    Returns EventDisposer

  • handle an event one time

    Type Parameters

    • K extends string | number | symbol

    Parameters

    • type: K
    • callback: ((ev) => any)

      callback to call when event is signaled

        • (ev): any
        • Parameters

          • ev: Q[K]

          Returns any

    Returns void

    Promise if callback is null

    take care with that because if the event is never fired and you await it, the system may overflow

  • remove all listeners for an event

    Parameters

    • eventName: string

      event name

    Returns void

  • remove the previous default handler installed for an event

    Parameters

    • eventName: string

      event name

    • callback: EventCallback<BasicEvent>

      callback handler to remove (must be the same as in setDefaultHandler)

    Returns void

  • set the event default handler

    Parameters

    • eventName: string

      name of the event

    • callback: EventCallback<BasicEvent>

      callback to call when the event is not handled (and preventDeault has not been called)

    Returns void

  • signal en event signaled event are notification : no way to prevent default not stop propagation

    Type Parameters

    • K extends string | number | symbol

    Parameters

    • type: K
    • event: Q[K]

      event data

    • delay: number = -1

    Returns void

Generated using TypeDoc