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 🙏

© 2026 – Pkg Stats / Ryan Hefner

events-class-mixin

v0.0.3

Published

A mixin for a simple, reusable Events implementation in Javascript.

Downloads

12

Readme

events-class-mixin

A mixin for a simple, reusable Events implementation in Javascript.

What?

Events follow the publish and subscribe pattern. Sender called publisher emit their messages, receiver called subscriber receive these messages. The benefit: Publisher and subscriber of events do not need to know each other.

How?

Publisher and subscriber communicate via an intermediary, the event. The event has a unique name. The receiver just subscribes for a named event and provides a callback. The sender emits a named event and optionally provides additional information. In this case, the events-class-mixin works as the intermediary, notifing all subscribers as soon as an event occurs.

Extending a class from EventsClassMixin:

const EventsClassMixin = require('events-class-mixin/index.class')
class AClass extends EventsClassMixin {}
let   anObject = new AClass()

Compose a class by assigning the object properties:

const eventsMixin = require('events-class-mixin/index.object')
class AClass {}
Object.assign(AClass.prototype, eventsMixin)
let   anObject = new AClass()

Compose an object by assigning the object properties:

const eventsMixin = require('events-class-mixin/index.object')
let   anObject = {}
Object.assign(anObject, eventsMixin)

Please note the difference between assigning the events mixin to a class or to an object. For classes you have to add the properties to prototype.

class AClass {}
Object.assign(AClass.prototype, eventsMixin)

let   anObject = {}
Object.assign(anObject, eventsMixin)

Install

The mixin is available, installable and manageable via NPM.

npm install events-class-mixin --save

API

Create a subscription

To create a subscription, call subscribe or its shortform on, specifiy the events name and provide a callback:

subscribe (eventName, subscriptionCallback, [...subscriptionParams]): subscriptionId
on        (eventName, subscriptionCallback, [...subscriptionParams]): subscriptionId

subscribe() returns the subscription id to explicitly identify the subscription. This id is a Javascript Symbol and must be stored in a variable.

Optional: You may provide additional parameters for the subscription that will be passed through the callback of the subscription.

ev.subscribe('input', event => console.log(`the event sent '${event}'`))
ev.subscribe('input', (event, data) => console.log(`the event sent '${event}' and '${data}'`), 'Data')

Publish an event

To publish an event, call publish or its shortform emit. Provide the events name as the first argument and additional data as the second argument.

publish (eventName, eventData)
emit    (eventName, eventData)

publish() has no return value, because it's a fire-and-forget mechanism.

ev.publish('input', 'Hello world!')
)
// output of first subscription: the event sent 'Hello world!'
// output of seconds subscription: the event sent 'Hello world!' and 'Data'

Delete a subscription

It's also possible to remove a previously created subscription. Call unsubscribe or its shortform off, provide the subscription id returned by subscribe or on and also provide the events name. The events name wouldn't be necessary, but serves as an additional locking mechanism to prevent accidental deletion.

unsubscribe (subscriptionId, eventName)
off         (subscriptionId, eventName)

Neither unsubscribe() nor off() return a value.

ev.unsubscribe(subscriptionId, 'input')
ev.off(subscriptionId, 'input')

Example

In ./examples/menu.js you will find a very simple example mimicking a menu. The example is not representative, but explains the functionality of Events and the pub-sub-pattern very clearly. First of all the script creates a subscription to click, providing a callback that simply logs the received value. In the second step it creates an subscription to input, provides an additional parameter with the value email and logs the received values to the console. Last but not least, the both subscriptions will be revoked, so the finally emitted events won't trigger any output.

const EventsClassMixin = require('../index.class.js')
class Menu extends EventsClassMixin {}
let   menu = new Menu()

//  create a subscription for 'click'
let clickId = menu.subscribe('click', event => console.log(`Item clicked: ${event}`))
//  publish an event 'click'
menu.publish('click', 'main')

//  create a subscription for 'input'
let inputId = menu.on('input', (event, data) => console.log(`The value for ${data} is '${event}'`), 'email')
//  publish an event 'input'
menu.emit('input', '[email protected]')

//  unsubscribe from 'click' and publish an event 'click'
menu.unsubscribe(clickId, 'click')
menu.publish('click', 'no subscriber available')

//  unsubscribe from 'input' and publish an event 'input'
menu.off(inputId, 'input')
menu.emit('input', 'no subscriber available')

LICENSE

MIT License

Copyright (c) 2020 Mark Lubkowitz

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.