Gathering detailed insights and metrics for @jitesoft/events
Gathering detailed insights and metrics for @jitesoft/events
Gathering detailed insights and metrics for @jitesoft/events
Gathering detailed insights and metrics for @jitesoft/events
npm install @jitesoft/events
Typescript
Module System
Node Version
NPM Version
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
A simple event handling system for browser and node alike.
Install will npm or yarn:
1npm install @jitesoft/events 2yarn add @jitesoft/events
The event handler class is the main instance which handles listening and emitting of events.
The following methods are exposed on the event handler:
on(string: eventName, function: listener, number: priority = 0, boolean: once = false): number
The on
method attaches a callback to the handler which will be fired once the given event is emitted by the handler.
It's possible to set a priority on the callback by changing the priority number
value, where 0 is lowest priority. The once
argument
determines if the callback should be removed after first run or not and defaults to false
.
The method returns a handle id (number
) which can be used to remove the event listener if wanted.
If the listener callback returns FALSE, the event will not bubble to the next handler.
once(string: eventName, function: listener, number: priority): number
The once
method does pretty much the same as on
, but is always a fire once
listener type.
clear(): void
Clear empties all the listeners from the handler.
off(string: eventName, function|number: listener): boolean
The off
method removes a given listener from the handler either by its handle or by passing its callback method.
emit(string: eventName, Event: event): void
Emits a event and fires each listener that listens to the given event.
emitAsync(string: eventName, Event: event, boolean: throw = false): Promise<void>
The emitAsync method works just like the Emit method, with the exception that it is an async method.
When used, it will group all the listeners by their priority and call them in batches, and just as with emit
, if
one of the callback returns false, it will not call the next batch.
It uses the Promise.allSettled
function - by default - which does not throw exceptions from the listeners.
If the throw
parameter is passed, it will instead use all
which will throw exceptions from the listeners.
When emitting a event, the Event
class is used as a object which passes the data. It accepts a data
object, which can be accessed
via the exposed data
getter and an optional callee
argument which can be used to pass information (or a reference) to the calling object.
The following example shows the most simple way create a handler and a listener and emit an event.
1import { EventHandler, Event } from '@jitesoft/events'; 2 3const handler = new Handler(); 4 5handler.on('test-event', (event) => { 6 console.log(event.data.message); 7}); 8 9handler.emit('test-event', new Event({ 10 message: 'This is a simple example...' 11}));
1MIT License 2 3Copyright (c) 2018 Jitesoft 4 5Permission is hereby granted, free of charge, to any person obtaining a copy 6of this software and associated documentation files (the "Software"), to deal 7in the Software without restriction, including without limitation the rights 8to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9copies of the Software, and to permit persons to whom the Software is 10furnished to do so, subject to the following conditions: 11 12The above copyright notice and this permission notice shall be included in all 13copies or substantial portions of the Software. 14 15THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21SOFTWARE.
No vulnerabilities found.
No security vulnerabilities found.