npm.io
1.0.0 • Published 6 years ago

minsky-event-dispatcher

Licence
ISC
Version
1.0.0
Deps
1
Size
11 kB
Vulns
0
Weekly
0

EventDispatcher

Provide classes with Event management logic

Event management is a handy piece of logic to allow classes to communicate with each other without having to provide their logic on their own. Contains two classes 'Event dispatcher' & 'Event'

Classes need to extend from 'vent Dispatcher', events are dispatched will be instances of 'Event'.

‌

Class type: Manager

Dependencies

  • Base - 1.0.0

Getting started

EventDispatcher can be used both standalone or as base for a class to extend from

// standalone
const dispatcher = new EventDispatcher()


// extend
export default class Dispatcher extends EventDispatcher {
	// ...
}
Constructor Parameters

… Base constructor parameters


Interface

Options
silent

Type: Boolean Default: false

Sets instance to silent on creation so it won’t dispatch events when dispatch is called.

… Base constructor options

Properties
listeners

Type: Object Default: {}

Collection of all the binded listeners.

silent

Type: Boolean Default: false

Toggles silent state of instance.

… Base properties

Methods
on

Parameters: event [, data] [, callback] Return: self

Binds a listener to an event

once

Parameters: event [, data] [, callback] Return: self

Binds a listener to an event and will remove the binding once the event has been dispatched.

off

Parameters: event [, callback] [, process] Return: self

Removes certain binding of certain event. Will remove all bindings if callback isn’t provided.

dispatch

Parameters: event [, data] [, process] Return: self

Dispatch a given event so all the listeners would be called.

Extra data can be provided to the Event data using the data parameter. It will merge the original data with the new.

Process is a method that can will be run right before every listener of the given event is called.

destroy

Parameters: none Return: self

Clears all bindings and destroys the instance. No need for Off() to be called before destruction as it will happened automatically.

… Base methods


To Do

  • None

Keywords