
Security News
Python Adopts Standard Lock File Format for Reproducible Installs
Python has adopted a standardized lock file format to improve reproducibility, security, and tool interoperability across the packaging ecosystem.
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.
![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.
on
, once
, off
and trigger
.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.once
function to listen to an event only once and
then remove the listener automatically.obj.addEventListener = Concert.on
.all
event for catching all triggered events.obj.on({change: onChange, save: onSave})
change:name
.Object.getOwnPropertySymbols
.npm install concert
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.
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)
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)
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")
### Inheritable Observables
Concert.js supports inheriting from your observables without worrying you'll
change the prototypes. Set up your event listeners once on your "class's"
prototype and then, if need be, add or remove listeners.
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 on
s and once
s 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.".
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.
For extended documentation on all functions, please see the Concert.js API Documentation.
Concert.js is released under a Lesser GNU Affero General Public License, which in summary means:
For more convoluted language, see the LICENSE
file.
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 andri@dot.ee or create an issue online.
2.1.0 (May 27, 2015)
Allows binding a listener's thisArg
to null
.
Using null
will mean the event listener will be called in the null
context. In [JavaScript's strict mode][strict] that means this === null
.
model.on("change", onChange, null)
As before, using undefined
or leaving the context out will mean the event
listener will be called in the object's context. This is most useful when
setting up listeners on the object's prototype as v2.0.0 featured:
Model.prototype.on("change", fn)
Allows binding additional arguments:
model.on("change", onChange, thisArg, 1, 2, 3)
FAQs
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.
The npm package concert receives a total of 4 weekly downloads. As such, concert popularity was classified as not popular.
We found that concert demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Python has adopted a standardized lock file format to improve reproducibility, security, and tool interoperability across the packaging ecosystem.
Security News
OpenGrep has restored fingerprint and metavariable support in JSON and SARIF outputs, making static analysis more effective for CI/CD security automation.
Security News
Security experts warn that recent classification changes obscure the true scope of the NVD backlog as CVE volume hits all-time highs.