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

simple-event-handler

v1.0.0

Published

Event handler library for both: frontend and backend

Downloads

4

Readme

simple-event-handler

GitHub issues GitHub license NPM Version Bower NPM Downloads Build Status

NPM

Event handler library for both: front-end and back-end

Table of Contents

Installation

Include File

<script type="text/javascript" src=".../js/simple-event-handler.min.js"></script>

Add simple-event-handler in your angular app to your module.

angular.module('app', ['simple-event-handler']);

Bower

Install via Bower

bower install --save simple-event-handler

npm

Install via npm

npm install --save simple-event-handler

Examples

AngularJS Example

app.run.js

angular
    .module('app', ['simple-event-handler'])
    .run(['$eventHandler', function ($eventHandler) {
        $eventHandler.subscribe('my-event', function(data) {
            // your logic here
        })
    }]);

app.controller.js

angular
    .module('app')
    .controller('MainController', ['$eventHandler', function ($eventHandler) {
        var args = {name: 'some name'}
        $eventHandler.fire('my-event', args);
    }]);
Node.js Example
var eventHandler = require('simple-event-handler');

eventHandler.subscribe('my-event', function(data) {
    // your logic here
})

...

var args = {name: 'some name'}
eventHandler.fire('my-event', args);
Basic Example
var eventHandler = new EventHandler();

eventHandler.subscribe('my-event', function(data) {
    // your logic here
})

...

var args = {name: 'some name'}
eventHandler.fire('my-event', args);

Method Chaining

It's just a nice feature to have ability to create a chains like this:

var result = 0;
var handler = function (data) {
    result = result + data;
};

eventHandler
    .once(EVENT_NAME, function () {
        result++;
    })
    .fire(EVENT_NAME) // result = 1
    .fire(EVENT_NAME) // result = 1
    .subscribe(EVENT_NAME, handler)
    .emit(EVENT_NAME, 10) // result = 11
    .on(EVENT_NAME, function (data) {
        result = result * 2 - data;
    })
    .fire(EVENT_NAME, 5) // result = ((11 + 5) * 2) - 5 = 27
    .offAll(EVENT_NAME) // removes everything
    .emit(EVENT_NAME) // no handlers to execute
    .fire(EVENT_NAME) // no handlers to execute
    .on(EVENT_NAME_2, handler)
    .fire(EVENT_NAME_2, 3) // result = 27 + 3 = 30
    .unsubscribe(EVENT_NAME_2, handler)
    .fire(EVENT_NAME_2, 20); // no handlers to execute

console.log(result); // --> 30

Documentation

subscribe

Registers new handler function to subscription list for the event named eventName. The handler will be added to the end of the subscription list.

Params

| Name | Type | Description | |:-----|:----:|:-----| | eventName | String|Array<String> | String or array of strings with event name(s) | | handler | Function | Function with your logic which will be called once event(s) is fired | | $scope | Object | AngularJS $scope object |

Handler function has one input parameter with data object which can be passed via #fire(eventName, handler) method

Example

Basic subscription

eventHandler.subscribe('my-event', function(data) {
    // your logic here
})

Multi-subscription

var events = ['ev1', 'ev2', 'custom-event', 'service:update'];

eventHandler.subscribe(events, function(data) {
    // your logic here
})

fire

Synchronously calls each of the handlers registered for the event named eventName, in the order they were registered, passing the supplied arguments to each, empty object will be passed instead if nothing provided

| Name | Type | Description | |:-----|:----:|:-----| | eventName | String | Event name to fire | | args | * | args that will be passed to each handler function. Can be used as shared resource if it is not a primitive value. Default: {} |

Example
eventHandler.subscribe('report:update', function(data) {
    // your logic here
    console.log(data.id); // --> 44
})

var report = {
    id: 44,
    title: 'Cost Report',
    isPrivate: false,
    storeInDB: false
};

eventHandler.fire('report:update', report);

unsubscribe

Removes registered handler function from subscription list

| Name | Type | Description | |:-----|:----:|:-----| | eventName | String | Event name on which handler was subscribed | | handler | Function | Handler function which should be removed from subscription list |

Example
var handler = function(data) {
  // your logic here
}

eventHandler.subscribe('my-event', handler);

eventHandler.fire('my-event');

eventHandler.unsubscribe('my-event', handler)

unsubscribeAll

Removes all the registered handler functions for the event named eventName

| Name | Type | Description | |:-----|:----:|:-----| | eventName | String | Event name for which subscription list should be cleaned |

Example
eventHandler.subscribe('my-event', function() {
    // handler #1
});

eventHandler.subscribe('my-event', function() {
    // handler #2
});

eventHandler.subscribe('my-event', function() {
    // handler #3
});

eventHandler.fire('my-event');

eventHandler.unsubscribeAll('my-event')

once

Adds a one time handler function for the event named eventName. Handler will be removed from subscription list after first execution

Params

| Name | Type | Description | |:-----|:----:|:-----| | eventName | String|Array<String> | String or array of strings with event name(s) | | handler | Function | Function with your logic which will be called once event(s) is fired | | $scope | Object | AngularJS $scope object |

Handler function has one input parameter with data object which can be passed via #fire(eventName, handler) method

Example

Basic subscription

eventHandler.once('my-event', function(data) {
    // your logic here
})

Multi-subscription

var events = ['ev1', 'ev2', 'custom-event', 'service:update'];

eventHandler.once(events, function(data) {
    // your logic here
})

on

Alias for #subscribe(eventName, handler)

emit

Alias for #fire(eventName, args)

off

Alias for #unsubscribe(eventName, handler)

offAll

Alias for #unsubscribeAll(eventName)

License

MIT License

Copyright (c) 2017 Volodymyr Lavrynovych

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.