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

concert

v2.1.0

Published

An event library that implements the observer pattern (a.k.a publish/subscribe). Similar to Node's EventEmitter and Backbone.Events, but independent, minimal and light-weight.

Downloads

39

Readme

Concert.js

![NPM version][npm-badge] ![Build status][travis-badge] [npm-badge]: https://badge.fury.io/js/concert.png [travis-badge]: https://travis-ci.org/moll/js-concert.png?branch=master

Concert.js is an event library for JavaScript and Node.js that implements the observer pattern (a.k.a publish/subscribe). This is a useful pattern for creating decoupled architectures, event driven systems and is one key element in the Model-View-Controller pattern. Concert.js similar to Node's EventEmitter and Backbone.Events, but independent, minimal and light-weight.

Tour

  • Simple and minimal — just on, once, off and trigger.
    No unnecessary method pollution or large API surface area like with other libraries. No legacy method names either.
  • Light-weight with little code and no external dependencies.
  • Inheritable — You can inherit from you observables and add listeners later.
    All event listeners will initially be inherited and then copied only once you call on, once or off on the child instances. Eliminate an awful lot of computation by setting your event listeners on your class's prototype. Read more on inheritable observables.
  • Familiar if you've ever used Backbone.js or Node.js's EventEmitter.
  • Comes with a built-in once function to listen to an event only once and then remove the listener automatically.
  • Set a listener's context and optionally extra arguments.
  • Rename or alias any function to a name of your choice.
    Handy if you need to present a compatible or legacy API:
    obj.addEventListener = Concert.on.
  • Special all event for catching all triggered events.
    Useful also for delegating or proxying all events from one object to another.
  • Add listeners for multiple events at once with object syntax:
    obj.on({change: onChange, save: onSave})
  • Works well with namespaced event names such as change:name.
    Because there's no limit to event names, you can easily create faux namespaces.
  • Supports ECMAScript 6's Symbol in case you need to create events a little more private.
    But really, that's an illusion. There's Object.getOwnPropertySymbols.
  • Thoroughly tested.

Installing

Installing on Node.js

npm install concert

Installing for the browser

Concert.js doesn't yet have a build ready for the browser, but you might be able to use Browserify to have it run there till then.

Using

To add events to any object of your choice, just mix Concert's functions to your object:

var Concert = require("concert")
var music = {}
for (var name in Concert) music[name] = Concert[name]

Then use on and trigger to add listeners and trigger events:

music.on("cowbell", function() { console.log("Cluck!") })
music.trigger("cowbell")

If you're using Underscore.js or Lo-dash, you can use _.extend to mix Concert in:

_.extend(music, Concert)

Enabling Concert on all instances

Mix Concert in to your class's prototype to make each instance observable.

function Music() {}
_.extend(Music.prototype, Concert)

Then you can listen to and trigger events on each instance.

var music = new Music
music.on("cowbell", console.log)

Faux Namespaces

Because there are no limits to event names, you can create faux namespaces by adding a separator, e.g :, to event names. Then trigger both the specific and general version in your application code and you're good to go. This happens to be also what Backbone.Model does for its change events.

model.trigger("change:name", "John")
model.trigger("change")
function Music(song) { this.song = song }

_.extend(Music.prototype, Concert)

Music.prototype.play = function() { this.trigger("play", this.song) }

Music.prototype.on("play", console.log.bind(null, "Playing %s."))

Once you initialize your object, all of the event listeners will be ready without having to call a bunch of ons and onces in the constructor. This pattern saves you from an awful lot of unnecessary computation.

Ensure the third argument, the listener's context, remains undefined when calling Music.prototype.on. The listener's context will then be set to any particular instance on which trigger was called.

var music = new Music("On Broadway")
music.play() // => Will log "Playing On Broadway.".

You can then add listeners without worrying you'll change every instance in the system (as you would when you'd use Node.js's EventEmitter or Backbone's Events).

var jam = new Music("The Way It Is")
jam.off("play")
jam.on("play", console.log.bind(null, "Jamming %s."))
music.play() // => Will log "Jamming The Way It Is.".

var classic = new Music("Tubular Bells")
classic.play() // => Will still log "Playing Tubular Bells.".

Extra arguments

If you'd like to use a single listener for multiple events, but need a way to still differentiate between events, make use of Concert.js's support for binding arguments:

var song = new Music("The Way It Is")
song.on("play", onPlay, undefined, "play")
song.on("stop", onPlay, undefined, "stop")

Your onStartOrStop function will then be called in the context of song with its first argument as either "play" or "stop". Any additional arguments given to trigger will come after the bound arguments.

API

For extended documentation on all functions, please see the Concert.js API Documentation.

Concert

  • off(event, listener, [thisArg])
  • on(event, listener, [thisArg], [arguments...])
  • once(event, listener, [thisArg], [arguments...])
  • trigger(event, [arguments...])

License

Concert.js is released under a Lesser GNU Affero General Public License, which in summary means:

  • You can use this program for no cost.
  • You can use this program for both personal and commercial reasons.
  • You do not have to share your own program's code which uses this program.
  • You have to share modifications (e.g bug-fixes) you've made to this program.

For more convoluted language, see the LICENSE file.

About

Andri Möll typed this and the code.
Monday Calendar supported the engineering work.

If you find Concert.js needs improving, please don't hesitate to type to me now at [email protected] or create an issue online.