GithubHelp home page GithubHelp logo

gamtiq / neva Goto Github PK

View Code? Open in Web Editor NEW
0.0 3.0 1.0 850 KB

Simple library to work with custom events

Home Page: https://gamtiq.github.io/neva/

License: MIT License

JavaScript 100.00%
event emitter eventemitter pub-sub eventbus event-handler dispatch subscription listener

neva's Introduction

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.

neva's People

Contributors

gamtiq avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.