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

emitonoff

v0.1.0

Published

Extremely simple event emitter

Downloads

296

Readme

npm version Build Status Code Climate

This is a simple, extremely lightweight, zero-dependency event/pubsub system for node or the browser.

It's pronounced like "stroganoff", but "emit" instead of "stroge".

It gives your objects these really basic functions:

  • emit - publish an event
  • on - subscribe to an event
  • off - unsubscribe an event

Also available:

  • pause - queue incoming emit() while you are waiting for something (sometimes needed for testing)
  • resume - run all the paused queue and process emit() normally, again

install

Greenkeeper badge

nodejs/webpack/browserify

npm install --save emitonoff

then, in your code:

var emitonoff = require('emitonoff');

plain browser global

You can use a CDN:

<script src="https://rawgit.com/konsumer/emitonoff/master/dist/emitonoff.min.js"></script>

or alternately use one of the files in dist/, locally.

usage details

var kitty = {
    purr: function(){
        this.emit('purr');
    },
    
    eat: function(food){
        this.emit('ate', food);
    } 
};

emitonoff(kitty);

function showAte(food){
    console.log('The kitty was hungry, so it ate', food);
}

function showPurred(){
    console.log('the kitty purred.')
}

kitty.on('ate', showAte);
kitty.on('purr', showPurred);

kitty.eat('kibble');
kitty.eat('mouse');
kitty.off('ate', showAte);
kitty.eat('tail');
kitty.purr();

You can also use it without an object, like this:

var kitty = emitonoff();

function showAte(food){
    console.log('The kitty was hungry, so it ate', food);
}

function showPurred(){
    console.log('the kitty purred.')
}

kitty.on('ate', showAte);
kitty.on('purr', showPurred);

kitty.emit('ate', 'kibbles');
kitty.emit('ate', 'bits');
kitty.emit('purr');

react

You can also use it with react (as a simpler alternative to flux, redux, thunk, etc.)

Here is an example:

import React from 'react'
import ReactDOM from 'react-dom'
import emitonoff from 'emitonoff'

import EmitOnOff from 'emitonoff/EmitOnOff'

// this could also be exported from a seperate file for sharing with other code
const state = emitonoff({counter:0})

const App = (props) => (
  <div className='App'>APP {state.counter}</div>
)

ReactDOM.render(<EmitOnOff state={state}><App /></EmitOnOff>, document.getElementById('root'))

// async re-renders
setInterval(() => {
  state.counter++
  state.emit('render')
}, 1000)

You can also make lots of little bound components, with different triggers and differnt stores!

import React from 'react'
import ReactDOM from 'react-dom'
import emitonoff from 'emitonoff'

import EmitOnOff from 'emitonoff/EmitOnOff'

// this could also be exported from a seperate file for sharing with other code
const state = {
  counter: emitonoff({counter: 0}),
  random: emitonoff({value: 0})
}

const App = (props) => (
  <div className='App'>
    APP
    <EmitOnOff state={state.counter} trigger='update'>
      counter: {state.counter.counter}
    </EmitOnOff>
    <EmitOnOff state={state.random} trigger='new'>
      random: {state.random.value}
    </EmitOnOff>
  </div>
)

ReactDOM.render(<App />, document.getElementById('root'))

// async re-renders
setInterval(() => {
  state.counter.counter++
  state.random.value = Math.random()
  state.counter.emit('update')
  state.random.emit('new')
}, 1000)

Note: using the standard render event trigger everywhere, and a single store, in general, will probably simplify things but do what you like!

browser support

I haven't done too much browser-testing, but it should work on any browser that can Array.slice (all of them in the last 15 years.) If you need it to work on a particular old browser, file a bug and I will add support (or tell you t find a Array.slice polyfill.)

It works in IE5.5.