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

event-z

v1.1.6

Published

Dependencyless simple ES6/5 event machine inspired by `$.Callbacks`

Downloads

6

Readme

Eventz

npm version Build Status Coverage Status Dependency Status

Async events inspired by $.Callbacks and written in ES6

Why?

You may be wonder why not just use EventEmitter from events package? So, i've been excited by how does $.Callbacks do the job, also i

  • Fully ES6
  • Event options once, memory, stop
  • Events namespacing
  • Can be used as extension for ES6 classes

Install

npm install event-z --save

Usage

There are two ways to use Eventz:

  1. You can invoke new Events(eventsList[, options]) by itslef
  2. You can extend your own class from Eventz

Self-invoking

import Eventz from 'event-z'

// Create instance of Eventz
const events = new Eventz([
  'helloSaid',
  'goodbyeSaid'
])

// Attach event handlers
events.on('helloSaid', function(){
  console.log('hello said')
})

events.on('goodbyeSaid', function(){
  console.log('hello said')
})

// Invoke events

events.emit('helloSaid')

events.emit('goodbyeSaid')

Constructor options

You can pass setup object as a second argument of the constructor.

Available options:

  • context – all the handlers will be invoked with this context, defaults to Eventz instance
  • expose – if true then Eventz will expose methods like .on() and .emit() to selected context

Extending class

import Eventz from 'event-z'

// Create your own class
class MyClass extends Eventz{
  
  eventsList = [
    'helloSaid',
    'goodbyeSaid'
  ];

  invokeAllEvents(){
    this.emit('helloSaid')

    this.emit('goodbyeSaid')  
  }

}

// Create instance of your class
const cls = new MyClass

// Now you can attach events directly
// to instance of your class
cls.on('helloSaid', function(){
  console.log('hello said')
})

cls.on('goodbyeSaid', function(){
  console.log('hello said')
})

// Invoke events from inside of class
cls.invokeAllEvents()

// Or invoke events directly
cls.emit('helloSaid')

cls.emit('goodbyeSaid')

ES6 classes without extends

When you don't want to extend your class but still need to have easy way to bind/enit/detach your events you can use Eventz this way:

import Eventz from 'event-z'

// Create your own class
class MyClass{

  constructor(){
    new Eventz([
      'helloSaid',
      'goodbyeSaid'
    ], {
      context : this,
      expose  : true
    })
  }

  invokeAllEvents(){
    this.emit('helloSaid')

    this.emit('goodbyeSaid')  
  }

}

// Create instance of your class
const cls = new MyClass

// Now you can attach events directly
// to instance of your class
cls.on('helloSaid', function(){
  console.log('hello said')
})

cls.on('goodbyeSaid', function(){
  console.log('hello said')
})

// Invoke events from inside of class
cls.invokeAllEvents()

// Or invoke events directly
cls.emit('helloSaid')

cls.emit('goodbyeSaid')

Event modifiers

When setting up events list you may configure how each of your events will behave. You'll have 3 options:

  • once – each handler will be invoked only once
  • memory – event handlers which were attached after event invocation will be called immediatelly
  • stop – will stop handlers execution if handler returns false

Options should be passed with events separated by semicolon: [eventName]:[option1]:[option2], for example helloSaid:once or goodbyeSaid:memory:stop.

import Eventz from 'event-z'

const events = new Eventz([
  'fireOnce:once',
  'memorized:memory',
  'shouldStop:stop'
])

// Example of `once`
events.on('fireOnce', function(){ console.log('Callback 1') })

events.emit('fireOnce')
// => 'Callback 1'

events.on('fireOnce', function(){ console.log('Callback 2') })

events.emit('fireOnce')
// => 'Callback 2'

events.emit('fireOnce') // nothing will happen, all handlers were invoked once

// Example of `memory`
events.on('memorized', function(){
  console.log('Works just as normal')
})

events.emit('memorized')
// => Works just as normal

events.on('memorized', function(){
  console.log('Will be invoked immediatelly')
})
// => Will be invoked immediatelly

// Example of `stop`

// works
events.on('shouldStop', function(){ console.log('One') })

// also works but will stop everything after
events.on('shouldStop', function(){ console.log('Two'); return false })

// will never work
events.on('shouldStop', function(){ console.log('Three') })

// will never work
events.on('shouldStop', function(){ console.log('Four') })

events.emit('shouldStop')

Passing arguments

You can pass any arguments to the event handler while ivoking event

events.on('someEvent', function(name, surname){
  console.log(`I'am ${name} ${surname}`)
})

events.emit('someEvent', 'Tim', 'Cook')
// => I'am Tim Cook

Multiple events invocation

You also can attach/emit/detach multiple events at once. To do this you should pass event namses as single string separated by space

events.on('firstEvent secondEvent', function(){
  console.log('event invoked')
})

Then emit your events separately

events.emit('firstEvent')

events.emit('secondEvent')

or simultaneously

events.emit('firstEvent secondEvent')

You also can detach events this way

events.off('firstEvent secondEvent')

Events namespacing

Any event can be namespaced. Namespaces separated by . from the event name. You don't need to define namespace when initializing Eventz.

const events = new Eventz([
  'myEvent'
])

events.on('myEvent', function(){
  console.log('Without namespace')
})

events.on('myEvent.myNamespace', function(){
  console.log('With namespace')
})

events.emit('myEvent')
// => Without namespace
// => With namespace

events.emit('myEvent.myNamespace')
// => With namespace

events.off('myEvent.myNamespace')
// will remove only namespaced handler

events.emit('myEvent.myNamespace')
// Nothing happens

events.emit('myEvent')
// => Without namespace

Public methods

These methods will be available on Eventz instance or on the instance of class extended from Eventz.

.on(eventNames, handlerFunction)

Attaches handler to Eventz instance

  • eventNames – one or more event names separated by space
  • handlerFunction – function which will be invoked after firing the event

.off([eventNames, handlerFunction])

Removes handler from Eventz instance.

If no arguments passed, then all the handlers for all the events will be removed

If no handlerFunction passed then all the handlers for specified events will be removed

  • eventNames – one or more event names separated by space
  • handlerFunction – function which will be invoked after firing the event

.emit(eventNames, [...arguments])

Invokes specified event(s)

  • eventNames – one or more event names separated by space
  • arguments – any arguments to pass to handler when event was fired

License

Copyright © 2016 Nicholas Strife [email protected]

This work is free. You can redistribute it and/or modify it under the terms of the MIT License. See LICENSE for full details.