new Dispatcher( ...eventTypes )

Description

Simple dispatcher (event bus).

It takes a fixed list of event types upon construction. Attempts to use an unknown event will throw an error.

One approach is to define both present and past tense events so that handlers can prepare for and react to events.

Parameters
Name Type Attributes Description
eventTypes NexusHoratio.base.Dispatcher~EventType <repeatable>

Event types this instance can handle.

Examples
const events = [
			  // @event module:lib.Klass#run
			  'run',
			  // @event module:lib.Klass#ran
			  'ran',
			  // @event module:lib.Klass#jump
			  'jump',
			  // @event module:lib.Klass#jumped
			  'jumped',
			  // @event module:lib.Klass#duck
			  'duck',
			  // @event module:lib.Klass#ducked
			  'ducked',
			];
			
			function handleRunJump(evt, data) {
			  switch (evt) {
			    case 'run':
			    case 'jump':
			      // ...
			      break;
			    case 'ran':
			      // ...
			      break;
			    case 'jumped':
			      // ...
			      break;
			}
			
			function handleDuck(evt, data) {
			  // ...
			}
			
			function handleDucked(evt, data) {
			  // ...
			}
			
			 const dispatcher = new Dispatcher(...events);
			
			 dispatcher.on('run', handleRunJump)
			  .on('ran', handleRunJump)
			  .on('jump', handleRunJump)
			  .on('jumped', handleRunJump)
			  .on('duck', handleDuck)
			  .on('ducked', handleDucked);
			
			 dispatcher.fire('jump', jumpData)
			   .fire('duck', duckData);
			 // ... more stuff ...
			 dispatcher.fire('jumped', jumpData)
			   .fire('ducked', duckData);
Details

Members


enabled :boolean

Details
boolean

Methods


on( eventType, func ) → {NexusHoratio.base.Dispatcher}

Description

Attach a function to an eventType.

Parameters
Name Type Description
eventType NexusHoratio.base.Dispatcher~EventType

Event type to connect with.

func NexusHoratio.base.Dispatcher~Handler

Single argument function to call.

Returns

This instance, for chaining.

Details

off( eventType, func ) → {NexusHoratio.base.Dispatcher}

Description

Remove all instances of a function registered to an eventType.

Parameters
Name Type Description
eventType NexusHoratio.base.Dispatcher~EventType

Event type to disconnect from.

func NexusHoratio.base.Dispatcher~Handler

Function to remove.

Returns

This instance, for chaining.

Details

fire( eventType, data ) → {NexusHoratio.base.Dispatcher}

Description

Calls all registered functions for the given eventType.

Parameters
Name Type Description
eventType NexusHoratio.base.Dispatcher~EventType

Event type to use.

data object

Data to pass to each function.

Returns

This instance, for chaining.

Details

<private> getHandlers( eventType ) → {Array.<NexusHoratio.base.Dispatcher~Handler>}

Description

Look up array of handlers by event type.

Parameters
Name Type Description
eventType NexusHoratio.base.Dispatcher~EventType

Event type to look up.

Returns

Handlers currently registered for this eventType.

Throws

When eventType was not registered during instantiation.

Details

Type Definitions


EventType

Details
string

Handler( eventType, data )

Parameters
Name Type Description
eventType NexusHoratio.base.Dispatcher~EventType

Event type.

data object

Event data.

Details
function