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

eventsjs

v2.0.0

Published

Event supplier library for browser and server

Downloads

2

Readme

#Events

This library is intended to supply an event interface to javascript objects using a Mixin pattern. The library is designed with encapsulation and uninterupptability in mind. This means a broken function will not stop the event loop (on modern browsers).

Events now also supports AMD and CommonJS syntax.

##Features

  1. Easy to integrate - can be used as a mixin, thus making it easy to integrate with any existing codebase.
  2. Fast - On modern browsers (FF, Chrome, Safari and IE > 8) the library uses costume DOM events to handle the events.
  3. Encapsulated - When used on modern browsers, broken functions will not break the event loop.
  4. Supports creating pseudo events (like :once and :latched).
  5. Comes with built in useful utilities.
  6. Cross-browser - falls back to a more standard costume events handler.

Installation

npm install eventsjs

##Usage

The simplest way to enable events support is like this:

function MyObject(){
    Events.call(this);
}

This method will introduce the entire events API onto the object. You could also create an instance of the object:

var ev = new Events();

Or, if you wish, you could always inherit it:

obj.prototype = new Events();

##Methods

###addEvent(type, fn) Adds a listener. When fired, the function will be passed an event object, with 2 parameters:

  1. args - arguments that were passed by dispatcher.
  2. dispatcher - the object dispatching the event
obj.addEvent('show',function(e){
    e.dispatcher; //obj
    e.args; //whatever arguments were passed
});

###addEvents(events) Adds multiple events in one callback

obj.addEvents({
    'show' : show_fn,
    'hide' : hide_fn
});

###fireEvent(type, args) Dispathces an event, passing arguments:

obj.fireEvent('show', {counter:1});

###removeEvent(type, fn) Removes a listener.

obj.removeEvent('show', this.bound.handleShow);

###addEventOnce(type, fn) Same as addEvent only it will remove the listener automatically once dispatched.

##Pseudo events The library supports a few pseudo events out of the box:

  1. :once - when used event will be added once.
  2. :latched - see latched section
  3. :times(number) - same as once, only it will execute X times (as passed by parameter).
  4. :delay(ms) - on fireEvent, will delay X miliseconds before firing the event. On addEvent will delay each execution of specific function.
obj.addEvent('test:once', function(){/* ... */ }); //will add a function to be fired once

obj.fireEvent('load:latched'); //will fire a latched event

obj.addEvent('test:times(5)', fn); //will add an event that will remove itself after 5 runs

obj.addEvent('test:delay(500)',fn); //will add an event that will wait 500ms before executing when fired

obj.fireEvent('test:delay(500)',args); //will wait 500ms before firing the event

You can also add your own pseudo events, by adding them to Events.Pseudoes. In order to create a new pseudo-event, add an object to the collection, containing either addEvent method, fireEvent method or both. You can even add a parameter to the pseudo-event. The addEvent and fireEvent methods will be fired instead of the default methods. It's arguments will be the same as their default, with a third argument, which is the passed pseudo-parameter (if any). In order to see more simply look at the code.

###Important note The library does not support multiple pseudo events. This is by design - the pseudo events hide complex logic and function-wrapping. Doing addEvent:once:delay(1000) might look nice, but hides the fact that it uses 3 levels of function wrapping. By default, The library would simply fail to assign the pseudo event properly (will try to match once:delay which doesn't exist), and will log an error to the console. However, you can make the librart throw exceptions for multiple pseudo events by setting the Events.strict flag to true.

##Latched events Latched events are events that once fired once will dispatch automatically afterwards. Examples for such events can be a 'load' event, or a 'domready' event. If any arguments were passed, they will be passed on as well. For example:

obj.fireEvent('load:latched',{someParam:"a"});

//will be fire automatically
obj.addEvent('load', function(e){
    e.args.someParam; //a
});

##Cleanup In case you want to ensure cleanup, the Mixin automatically listens to the destroy event and cleans itself up for destruction