EventHandle [ NPM ]
JavaScript event pattern with closures. (Built with Typescript!)
Install
npm install event-handle
or yarn add event-handle
Import
import EventHandle from 'event-handle';
import {
createEventHandle,
isEventHandle,
onEvent,
} from 'event-handle';
const EventHandle = require('event-handle');
Example
let demoStarted = EventHandle.create();
EventHandle.on(demoStarted, (...args) => {
console.log('The demo started!', args)
});
let remove = demoStarted.handle((...args) => {
console.log('Got it...', args);
});
demoStarted('a','r','g','s');
console.log('removed: ', remove());
console.log('handlerCount: ', demoStarted.handlerCount());
console.log('id: ', demoStarted.id);
console.log('isEventHandle: ', EventHandle.isEventHandle(demoStarted));
console.log('removeAllHandlers: ', demoStarted.removeAllHandlers());
See also:
- Test code in
spec/index.spec.ts
- Example code in
examples/
API
Default Export
- EventHandle
Events with closures.
Function Exports
- createEventHandle([config]) ⇒
function
Returns a function that triggers an event. The function has members such
as .handle()
to add a handler, .id
(a string), .handlerCount()
and
.removeAllHandlers()
.
- isEventHandle(fn) ⇒
boolean
Returns true if fn
is a function created by createEventHandle
.
- onEvent(evt, handler, [options]) ⇒
function
Adds a handler to the given event.
EventHandle
Events with closures.
Kind: global variable
EventHandle.create ⇒ function
Returns a function that triggers an event. The function has members such
as .handle()
to add a handler, .id
(a string), .handlerCount()
and
.removeAllHandlers()
.
Kind: static property of EventHandle
Returns: function
- The event.
Param | Type | Description |
---|
[config] | EventHandleConfiguration | string | Event id or configuration. |
EventHandle.isEventHandle ⇒ boolean
Returns true if fn
is a function created by createEventHandle
.
Kind: static property of EventHandle
Returns: boolean
- True if the given fn is an EventHandle.
Param | Type | Description |
---|
fn | function | The function to check. |
EventHandle.on ⇒ function
Adds a handler to the given event.
Kind: static property of EventHandle
Returns: function
- A function to remove the handler.
Param | Type | Description |
---|
evt | function | The event to add a handler to. |
handler | function | The handler function for the event. |
[options] | EventHandlerOptions | Options for the handler. |
createEventHandle([config]) ⇒ function
Returns a function that triggers an event. The function has members such
as .handle()
to add a handler, .id
(a string), .handlerCount()
and
.removeAllHandlers()
.
Kind: global function
Returns: function
- The event.
Param | Type | Description |
---|
[config] | EventHandleConfiguration | string | Event id or configuration. |
isEventHandle(fn) ⇒ boolean
Returns true if fn
is a function created by createEventHandle
.
Kind: global function
Returns: boolean
- True if the given fn is an EventHandle.
Param | Type | Description |
---|
fn | function | The function to check. |
onEvent(evt, handler, [options]) ⇒ function
Adds a handler to the given event.
Kind: global function
Returns: function
- A function to remove the handler.
Param | Type | Description |
---|
evt | function | The event to add a handler to. |
handler | function | The handler function for the event. |
[options] | EventHandlerOptions | Options for the handler. |