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

neva

v1.0.2

Published

Simple library to work with custom events.

Downloads

10

Readme

neva

Simple library to work with custom events.

NPM version Build Status Built with Grunt

  • Simple event emitter API: on, off, emit, hasEventHandler.
  • Event emitter API is chainable.
  • Events can be emitted with multiple attached parameters.
  • Data about emitted event are wrapped into an object with uniform structure that is passed to handlers.
  • Ability to add event handler that should be called just once.
  • Ability to enhance prototype of constructor function (class) with event emitter API (emitter methods can be mixed in prototype).
  • Available as ECMAScript 6/2015 module, CommonJS module or UMD.
  • Work in any ECMAScript 3+ environment (browsers, Node.js etc).
  • Small.

Table of contents

Installation

Node

npm install neva

Bower

bower install neva

AMD, <script>

Use dist/neva.js or dist/neva.min.js (minified version).

Usage

ECMAScript 6+

import getEmitter from 'neva';

Node

const getEmitter = require('neva').getEmitter;

AMD

define(['path/to/dist/neva.js'], function(neva) {
    const getEmitter = neva.getEmitter;
});

Bower, <script>

<!-- Use bower_components/neva/dist/neva.js if the library was installed by Bower -->
<script type="text/javascript" src="path/to/dist/neva.js"></script>
<script type="text/javascript">
    // neva is available via neva field of window object
    const getEmitter = neva.getEmitter;
</script>

Examples

const emitter = getEmitter();
const eventHandler = (event) => {
    console.log('eventHandler: event type -', event.type, ', data -', event.data);
};
const obj = {
    name: 'obj',
    handler(event) {
        console.log(`${this.name}.handler: event type -`, event.type, ', params -', event.params);
    }
};
function onceHandler(event) {
    console.log('onceHandler: event type -', event.type, ', data -', event.data);
}

emitter.on(['event1', 'event2'], eventHandler)
        .on('event1', obj.handler, obj)
        .on('event2', onceHandler, null, {once: true});

emitter.emit('event1', 1, 2, 3)
        .emit({type: 'event2', data: 'some data', qty: 8});
// The following will be printed into console:
// eventHandler: event type - event1 , data - 1
// obj.handler: event type - event1 , params - Array [ 1, 2, 3 ]
// eventHandler: event type - event2 , data - some data
// onceHandler: event type - event2 , data - some data

emitter.hasEventHandler('event1', eventHandler); // true
emitter.hasEventHandler('event2', onceHandler); // false

emitter.off('event1', eventHandler);
emitter.hasEventHandler('event1', eventHandler); // false
emitter.hasEventHandler('event1'); // true

emitter.off('event1');
emitter.hasEventHandler('event1'); // false
emitter.hasEventHandler(); // true

emitter.off();
emitter.hasEventHandler(); // false

class SomeClass {
    ...
}
// Add event emitter methods to class
getEmitter(SomeClass.prototype);

API

getEmitter([target: Object]): EventEmitter

Create event emitter or add methods to work with events into specified object.

target can be prototype of some constructor function (class).

EventEmitter API

on(type: string | string[], handler: Function, [context: Object], [settings: HandlerSettings]): EventEmitter

Register a handler for the specified event type(s).

off(type: string, handler: Function, [context: Object]): EventEmitter

Remove the specified event handler.

off(type: string): EventEmitter

Remove all handlers for the given event type.

off(): EventEmitter

Remove all registered event handlers.

emit(type: string, [param1: any, param2: any, ...]): EventEmitter

Call all handlers for the specified event type.

An object with the following fields will be passed in each handler:

  • type: string - the event type (value of type parameter).
  • params: Array - list of additional parameters that are passed besides the event type ([param1, param2, ...]).
  • data: any - value of the second function's parameter (value of params[0]).

emit({type: string, ...}): EventEmitter

Call all handlers for the specified event type and pass the given object in each handler.

hasEventHandler(type: string, handler: Function, [context: Object]): boolean

Check whether the specified event handler is registered.

hasEventHandler(type: string): boolean

Check whether any handler for the specified event type is registered.

hasEventHandler(): boolean

Check whether any event handler is registered.

See docs for details.

Contributing

In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using Grunt.

License

Copyright (c) 2017-2021 Denis Sikuler
Licensed under the MIT license.