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

evan

v0.8.1

Published

Universal events

Downloads

34

Readme

Evan

Universal events

Evan implements a general eventing system in which any object that can be represented by a path (typically in an hierarchical, tree-like structure like the DOM) may trigger and capture events.

Evan is based on Troop for OOP and Sntls for collections.

Wiki

Reference

Npm package

Features

  • Subscription and unsubscription
  • Event bubbling
  • Delegation (capturing closer to the root than the actually observed node)
  • Broadcasting (triggering many subscribed paths at once)
  • Chaining original events
  • Evented behavior to be assigned to classes and instances. Most of the event management will be done through this, as the tutorial below illustrates.

Tutorial

The following example creates an evented class, endows it and its instances with evented properties, then triggers an event on the instance. Right below it's presented in its entirety, followed by a step by step explanation.

var eventSpace = evan.EventSpace.create(),
    MyClass = troop.Base.extend()
        .addTrait(evan.Evented)
        .setEventSpace(eventSpace)
        .setEventPath('test'.toPath())
        .addMethods({
            init: function (path) {
                this.setEventPath(path);
            }
        })
        .subscribeTo('myEvent', function (event) {
            console.log("event triggered", event.clone());
        });

var myInstance = MyClass.create('test>path>foo'.toPath());

myInstance.triggerSync('myEvent');

Step by step

First, we created an event space for the class. Events will traverse within the confines of this space. Events cannot cross between spaces.

var eventSpace = evan.EventSpace.create(),

Then, we created a new class ...

MyClass = troop.Base.extend()

... and added the evented behavior as a Troop trait.

.addTrait(evan.Evented)

Then, we assign the event space and a path that represents the whole class.

.setEventSpace(eventSpace)
.setEventPath('test'.toPath())

Then added an init method, which takes an individual path specific to the instance and applies it to it.

this.setEventPath(path);

Building the class is concluded by adding an event handler that is supposed to capture all 'myEvent' events that concern this class. The handler logs the event to the console. (The event is cloned because when an event has finished traversing the event space it will reset.)

.subscribeTo('myEvent', function (event) {
    console.log("event triggered", event.clone());
});

Now, to try how this works on an instance, we need to instantiate the class. Instances must have paths relative to the class path or instantiation will fail.

var myInstance = MyClass.create('test>path>foo'.toPath());

Finally, we trigger an event on the instance.

myInstance.triggerSync('myEvent');

This will trigger the event handler we applied to it statically on the class, but within the handler, while event.currentPath.asArray will say ['test','path'], event.originalPath.asArray will say ['test','path','foo'], as implied by the path we assigned to the instance.