Options
All
  • Public
  • Public/Protected
  • All
Menu

Interface QuickEventParams

All classes CallbackList, EventDispatcher and EventQueue in quick-event accepts a parameters object in constructor to configure and extend each components' behavior. The parameters object is undefined by default.
All parameters are optional. If any parameter is omitted, the default value is used.
The same parameter mechanism applies to all three classes, CallbackList, EventDispatcher and EventQueue, though not all classes requires the same parameter.

export
interface

QuickEventParams

Hierarchy

  • QuickEventParams

Index

Properties

Optional argumentPassingMode

argumentPassingMode: undefined | IncludeEvent | ExcludeEvent

ArgumentPassingMode is the argument passing mode.

The possible values,

EventDispatcher.argumentPassingIncludeEvent = ArgumentPassingMode.IncludeEvent;
EventDispatcher.argumentPassingExcludeEvent = ArgumentPassingMode.ExcludeEvent;

The global default value,

EventDispatcher.defaultArgumentPassingMode = ArgumentPassingMode.ExcludeEvent;

Let's see some examples. Assume we have the dispatcher

let dispatcher = new EventDispatcher();
// same as
let dispatcher = new EventDispatcher({ argumentPassingMode: EventDispatcher.argumentPassingExcludeEvent });
dispatcher.dispatch(3, "hello");

The listener will be invoked with the argument ("hello"), the event type is omitted since it's argumentPassingExcludeEvent.

apply

EventDispatcher, EventQueue

default

EventDispatcher.argumentPassingExcludeEvent

memberof

QuickEventParams

Optional argumentsAsArray

argumentsAsArray: undefined | boolean

argumentsAsArray affects how the listeners and getEvent receive the arguments.

When argumentsAsArray is false, the listeners and getEvent receive the arguments as individual parameters. For example,

let dispatcher = new EventDispatcher();
// same as
let dispatcher = new EventDispatcher({ argumentsAsArray: false });
dispatcher.dispatch(a, b, c);
// The listener will be called as
myListener(a, b, c);

When argumentsAsArray is true, the listeners and getEvent receive the arguments as an array of which each elements are the arguments. For example,

let dispatcher = new EventDispatcher({ argumentsAsArray: true });
dispatcher.dispatch(a, b, c);
// The listener will be called as
myListener([ a, b, c ]);

Setting argumentsAsArray to true will slightly improve the performance.

apply

CallbackList, EventDispatcher, EventQueue

default

false

memberof

QuickEventParams

Optional canContinueInvoking

canContinueInvoking: undefined | ((...args: any[]) => boolean)

canContinueInvoking(arg1, arg2, ...). The function receives same arguments as EventDispatcher.dispatch and EventQueue.enqueue, and must return true if the event dispatching or callback list invoking can continue, false if the dispatching should stop.

apply

CallbackList, EventDispatcher, EventQueue

default

the default implementation always returns true

memberof

QuickEventParams

example
// The event object we will dispatch looks like,
// MyEvent = {
//   type: int,
//   canceled: boolean
//};

// When construct the dispatcher, pass the parameter
// getEvent to indicate how to get the event type.
// Parameter canContinueInvoking checks if the dispatching can continue.
let dispatcher = new EventDispatcher({
  getEvent: e => e.type,

  canContinueInvoking: (e) => ! e.canceled
});

dispatcher.appendListener(3, (e) => {
  console.log("Got event 3");
  e.canceled = true;
});
dispatcher.appendListener(3, (e) => {
  console.log("Should not get this event 3");
});

// Dispatch the event.
// The first argument is MyEvent.
dispatcher.dispatch({ type: 3, canceled: false });

Optional getEvent

getEvent: undefined | ((...args: any[]) => any)

The function receives same arguments as EventDispatcher.dispatch and EventQueue.enqueue, and must return an event type.
quick-event forwards all arguments of EventDispatcher.dispatch and EventQueue.enqueue (both has same arguments) to getEvent to get the event type, then invokes the callback list of the event type.

apply

EventDispatcher, EventQueue

default

the default implementation returns the first argument of getEvent

memberof

QuickEventParams

example
// The event object we will dispatch looks like,
// MyEvent = {
//   type: int,
//   message: string,
//   param: int
//};

// When construct the dispatcher, pass the parameter
// getEvent to indicate how to get the event type.
let dispatcher = new EventDispatcher({
  getEvent: (e) => e.type
});

// Add a listener.
// Note: the first argument is the event type of type int (same as the return type of getEvent), not MyEvent.
// e is the main event object.
// b is an extra parameter.
dispatcher.appendListener(3, (e, b) => {
  console.log("Got event 3");
  console.log("Event::type is", e.type);
  console.log("Event::message is", e.message);
  console.log("Event::param is", e.param);
  console.log("b is", b);
});

// Dispatch the event.
// The first argument is MyEvent.
dispatcher.dispatch({ type: 3, message: "Hello world", param: 38 }, true);

// Output:
// > Got event 3
// > Event::type is 3
// > Event::message is Hello world
// > Event::param is 38
// > b is true

Optional mixins

mixins: undefined | MixinFilter[]

A mixin is used to inject code in the EventDispatcher/EventQueue inheritance hierarchy to extend the functionalities. For more details, please read the document of mixins.

apply

EventDispatcher, EventQueue

default

undefined

memberof

QuickEventParams

Generated using TypeDoc