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

ventage

v2.3.0

Published

Ventage is a simple eventing library for JavaScript objects.

Downloads

9

Readme

Ventage

Ventage is a simple eventing library for JavaScript objects.

Installation

Ventage may be installed as a bower package:

$ bower install ventage

It may also be installed as a CommonJS module:

$ npm install ventage

In the browser it may be used:

  • as a require.js AMD module
  • as a CommonJS module with Browserify
  • as a direct <script> reference

Usage

Creating Ventage objects

Ventage is designed to be used as a prototype for your custom objects. You can accomplish this in several ways.

as the prototype to a constructor function

function View() {
  this.render = function () {
    this.trigger('rendering');
    // other code
    this.trigger('rendered');
  };
}

View.prototype = new Ventage();

by setting up the prototype chain manually

var databaseObject = Object.create(new Ventage());
databaseObject.connect = function () {
    // connection code
    this.trigger('connected');
  }
};

by using the Ventage factory function

var poll = Ventage.create({
  start: function () {
    var self = this;
    setInterval(function () {
      var result = self.ping('http://localhost');
      self.trigger('polled', result);
    }, 100);
  }
});

by instantiating Ventage objects directly

var v = new Ventage();
v.on('foo', onFoo);
v.trigger('foo', 'bar');

Using Ventage objects

Client objects can subscribe to events raised by Ventage objects.

var v = new Ventage();
v.on('message', function (sender, content) {
  console.log('message from %s: %s', sender, content);
});
v.trigger('message', 334455, 'hello, world!');

subscribing to events

The on() method accepts three arguments:

  • event name
  • callback function
  • (optional) context object

If a context argument is passed to the on() method, it will be used as the value of this within the callback function.

triggering events

The trigger() method accepts a single event name argument, and a variable number of data arguments that will be passed, in order, to any callback associated with the event.

By default, trigger() will invoke callbacks synchronously. To change this behavior, you can:

  • pass a boolean value to the Ventage constructor to indicate whether the object should always trigger callbacks asynchronously; or
  • invoke the triggerAsync() method manually when you want callbacks to be called asynchronously

The triggerAsync() method accepts the same arguments as trigger().

unsubscribing from events

To unsubscribe from events, use the off() method. It takes three arguments:

  • (optional) event name
  • (optional) callback reference
  • (optional) context object

If off() is called with no arguments, it will unsubscribe all callbacks from all events. This has the same effect as calling clear().

If off() is called with an event name only, it will unsubscribe all callbacks from that event.

If off() is called with an event name and a callback reference, it will unsubscribe only that callback from the event. If the callback was subscribed with a specific context, it is necessary to provide that context to the off() method to unsubscribe the callback.

piping events

Events may be piped from one Ventage instance to another. This means that events triggered on one instance will also be triggered, with the same arguments, on the other.

var v1 = new Ventage();
var v2 = new Ventage();
var callbackHandle = v1.pipe('alert', v2);
v2.on('alert', function () {
  console.log('Danger, Will Robinson!');
});
v1.trigger('alert'); // v2 event will trigger as well
v1.off('alert', callbackHandle, v2);

The context for a piped event is always the receiving Ventage object (in the code above, v2).

The callback handle is returned from pipe() so that it may later be used to unsubscribe from the piped event.

subscribing to wildcard events

Currently Ventage supports a single wildcard event, *, which is invoked for any event triggered on a Ventage object.

var v = new Ventage();
var count = 0;
v.on('*', function () {
  count += 1;
});
v.trigger('foo');
v.trigger('bar');
v.trigger('baz');
console.log(count); // 3

License

MIT License