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

eventus

v0.1.1

Published

Eventful object that can everything

Downloads

4

Readme

Eventus Build Status

Eventful object that can everything. It's a simple and rapid realization of Pub-Sub, Mediator & Observer patterns.

How to use it

Constructor

You can create instance of Eventus using its constructor.

var Eventus = require('eventus');
var foo = new Eventus();

Also you can pass options object that will extend simple Eventus object and after taht you can use it as object properties (methods). After creating init event will be triggered.

var bar = new Eventus({
	baz: 'qux'
});

Event listening

Each Eventus object has amount of built-in events and may has own custom events. Events can be defined on options and by using .on() and .once() methods as well.

var foo = new Eventus({
    events: {
        bar: console.log
    }
});

foo.trigger('bar', 'Hello World!');
// output in console: Hello World!

foo.on('baz', console.log);
foo.trigget('baz', 'Hello again');
// output in console: Hello again

For one-time listener you can use .once() instead of .on().

To remove listener use .off() method. It can accept different set of arguments.

var foo = new Eventus();
foo.on('bar', console.log);

// remove specified listener
foo.off('bar', console.log);

// remove all listener for specified event
foo.off('bar');

// remove all listeners for all event
foo.off();

Event triggering

For triggering event of object just use .trigger() method. Method accepts at least 1 parameter - event name, all other parameters is data that will be passed for listeners;

var foo = new Eventus();
foo.on('bar', console.log);
foo.trigger('bar', 'Hello', 'World');
// output in console: Hello World

Child elements

Each Eventus object can has child elements. For creating and removing children use .createChild() and .removeChild() methods. .addChild() accepts two parameters: name (alias for child element in parent object) and options (will be passed as options of child element to it's constructor). After adding child element event child:add will be triggered on parent object.

var foo = new Eventus();
foo.createChild('bar');
foo.createChild('baz', { some: 'option' });

// each child element is available by alias in parent element
console.log(foo.baz.some);
// output in console: option

Each child element can has it's own deep child element

foo.bar.createChild('qux');

For deleting child element you can use both name of object and it's instance. After deleting child element event destroy will be triggered on removed object and child:remove will be triggered on parent object.

var foo = new Eventus();
var bar = foo.createChild('bar');
var baz = foo.createChild('baz');

// remove element using name
foo.removeChild('bar');

// remove element using instance
foo.removeChild(baz);

Destroying object

You can simply destroy object by using .destroy() method. This will remove all listeners from object and triggers destroy event.

Broadcasting & emitting events

For passing events to parent elements use .emit() method, for children - .broadcast() method. Arguments are the same as for triggering events.

var foo = new Eventus();
var bar = foo.createChild('bar');
var baz = bar.createChild('baz');

// this will trigger 'qux' event on bar & baz
foo.broadcast('qux', { some: 'options' });

// this will trigger 'bat' event on foo & bar
baz.emit('bat', { some: 'options'});

List of Eventus modules

Full list of modules

License

Eventus is published under the MIT License.