npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@jitesoft/events

v2.3.7

Published

A simple cross-env event handler with custom events.

Downloads

91

Readme

Events

npm (scoped) Known Vulnerabilities pipeline status coverage report npm Back project

A simple event handling system for browser and node alike.


Installation

Install will npm or yarn:

npm install @jitesoft/events
yarn add @jitesoft/events

Classes

The event handler class is the main instance which handles listening and emitting of events.

EventHandler

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.

Event

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.


Example

The following example shows the most simple way create a handler and a listener and emit an event.

import { EventHandler, Event } from '@jitesoft/events';

const handler = new Handler();

handler.on('test-event', (event) => {
  console.log(event.data.message);
});

handler.emit('test-event', new Event({
  message: 'This is a simple example...'
}));

License

MIT License

Copyright (c) 2018 Jitesoft

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.