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

qi-events

v1.0.2

Published

A no nonsense event API (BASED ON Backbone.Events).

Downloads

14

Readme

Qi Events

Version npmBuild StatusDependenciesCoverage StatusAPI Documentation

http://drkibitz.github.io/qi-events/

A no nonsense event API (BASED ON Backbone.Events).

Goals

  • Maintain a no nonsense event dispatching/emitting/triggering API.
  • Test it.
  • Find a happy medium between simplicity, speed, and usage.
  • Should be able to perform well in high performance situations.
  • Allow for the most common use cases, but within reason to maintain high performance.
  • Embrace CommonJS Module format
  • Allow for UglifyJS and Closure Compiler with ADVANCED_OPTIMIZATIONS.
  • Allow for use as the basis of other event implmentations (scenegraph)

Usage

There are just 4 main methods to remember.

Use the mixin method to allow any object to trigger events.

require('qi-events').mixin({});
    .on('myevent', console.log, console)
    .trigger('myevent', 'something')
    .trigger('myevent', 'something', 'something')
    .trigger('myevent', 'something', 'something', 'darkside');

// > something
// > something something
// > something something darkside

Mixin into any prototype object.

var events = require('qi-events');
function MyEmitter() {}
events.mixin(MyEmitter.prototype);
var emitter = new MyEmitter();
emitter
    .on('myevent', console.log, console)
    .trigger('myevent', 'something', 'something', 'darkside');

// > something something darkside

Extend the provided class.

var Events = require('qi-events').Events;
function MyEmitter() {}
MyEmitter.prototype = Object.create(Events.prototype, {
    constructor: {value: MyEmitter}
});
var emitter = new MyEmitter();
emitter
    .on('myevent', console.log, console)
    .trigger('myevent', 'something', 'something', 'darkside');

// > something something darkside

Use the module itself as a central dispatcher.

require('qi-events');
    .once('myevent', console.log, console)
    .trigger('myevent', 'something', 'something', 'darkside');

// > something something darkside

Changes since Backbone.Events

Backbone.js 0.9.10 (c) 2010-2013 Jeremy Ashkenas, DocumentCloud Inc. Backbone may be freely distributed under the MIT license. For all details and documentation: http://backbonejs.org

  1. Added "use strict"

  2. Converted to CommonJS module

  3. Module is a constructor that can be used with class inheritence

  4. Removed very minor dependencies on underscore.js and Backbone.js

    1. Added mixin method to replace Backbone mixin functionality
    2. Underscore was only used for slice, just using the native method
  5. Completely removed listenTo(), listenToOnce(), and stopListening() methods

  6. Change eventsApi() to accept an array of event names

  7. Change eventsApi() to receive function references instead of "action" strings

    1. This is a micro optimization and allows better obfuscation
  8. trigger() no longer calls eventsApi(), this is a very rare (and mostly unnecessary) use case that can be avoided

    1. Optimization is important when triggering, but convenience is important when adding and removing
  9. With trigger not using eventsApi(), it allowed further optimizations on eventsApi() and trigger()

    1. Removed ctx property from _events[name] object, just use context property instead
    2. Fixed lint errors in the process
  10. Added aStart argument to triggerEvents() as a logical slice removing slice call for most cases

Why not EventEmitter?

So I think we already have four separate versions of EventEmitter at the time of writing this paragraph (April 2015). At this point in time, I am refusing to raise that count to five, or linking to any of them. Just to be clear, I am pretty sure I published this library when there were only two versions published, and I originally implemented my changes privately on top of Backbone.Events before it was broken up into parts. With all of this said, though I may be biased, I still prefer this library. If you are interested in how this API compares to the EventEmitter API, the following example should help.

var events = require('qi-events');

function EventEmitter25() {}
var proto = EventEmitter25.prototype;

proto.emit = events.trigger;
proto.on = proto.addListener = events.on;
proto.once = events.once;
proto.removeListener = events.off;

proto.listeners = function (event) {
    if (!this._events || !this._events[event]) return [];
    for (var i = 0, l = this._events[event].length, arr = new Array(l); i < l; i++) {
        arr[i] = this._events[event][i].callback;
    }
    return arr;
};

proto.removeAllListeners = function (event) {
    return events.off.call(this, event);
};

proto.setMaxListeners = function () {
    return this;
};

module.exports = EventEmitter25;