GithubHelp home page GithubHelp logo

jfusco / es6-event-emitter Goto Github PK

View Code? Open in Web Editor NEW
16.0 0.0 2.0 630 KB

Custom, simple, extendable event/messaging system written in ES6

License: MIT License

JavaScript 37.82% CSS 14.41% HTML 47.77%
es6 es2015 events pubsub pub-sub emitter listener jasmine karma karma-jasmine istanbul isparta

es6-event-emitter's Introduction

es6-event-emitter

Build Status

devDependency Status

MIT npm

Custom, simple, extendable event/messaging system written in ES6

Getting Started

Installation

From the root of your project.

npm install es6-event-emitter --save

Usage

Simple implementation of emitter. See api below.

import Emitter from 'es6-event-emitter';

export default class Component extends Emitter {
	constructor(){
		super();
	}

	//-- Trigger
	someAction(){
		...

		this.trigger('component:action');
	}

	//-- Trigger with data
	someOtherAction(){
		...

        this.trigger('component:otheraction', {
            foo: 'bar',
            baz: 'buzz'
        });
    }
}

//-- Create a new component
const component = new Component();

//-- Set up functions for listeners - best practice in case you want to remove them later.
const action = () => {
	console.log('action triggered');
}

const otheraction = data => {
	console.log(`other action triggered with data ${data}`);
}

//-- Register listeners
component.on('component:action', action);
component.on('component:otheraction', otheraction);

//-- Call methods
component.someAction();
component.someOtherAction();

API

on - on(event:String, callback:Function)

Registers a listener

component.on('component:action', action);

off - off(event:String, callback:Function)

Removes a registered listener from the event stack

component.off('component:action', action);

once - once(event:String, callback:Function)

Registers a listener that will only fire once no matter how many times you try to trigger

component.once('component:action', action);

trigger - trigger(event:String, ...args:*)

Triggers a registered event with optional data

this.trigger('component:action');

//-- With data
this.trigger('component:action', {
    foo: 'bar',
    baz: 'buzz'
});

destroy - destroy()

Destroys the entire event emitter

component.destroy();

Tests

View the test coverage

npm test

es6-event-emitter's People

Contributors

cymen avatar dependabot[bot] avatar jfusco avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Forkers

wkasel enrodroid

es6-event-emitter's Issues

Dependency on babel-runtime

Attempting to use es6-event-emitter without babel-runtime as a direct dependency results in this error:

module.js:327
    throw err;
    ^

Error: Cannot find module 'babel-runtime/helpers/classCallCheck'
    at Function.Module._resolveFilename (module.js:325:15)
    at Function.Module._load (module.js:276:25)
    at Module.require (module.js:353:17)
    at require (internal/module.js:12:17)
    at Object.<anonymous> (/Users/pwagener/Devel/callbacks/node_modules/es6-event-emitter/dist/emitter.js:7:24)
    at Module._compile (module.js:409:26)
    at Module._extensions..js (module.js:416:10)
    at Object.require.extensions.(anonymous function) [as .js] (/Users/pwagener/Devel/callbacks/node_modules/babel-register/lib/node.js:154:7)
    at Module.load (module.js:343:32)
    at Function.Module._load (module.js:300:12)
    at Module.require (module.js:353:17)
    at require (internal/module.js:12:17)
   ...

Adding babel-runtime as a direct dependency of my project seems to resolve it.

Cannot find module 'babel-runtime/helpers/classCallCheck'

Hi there, your class looks promising! I am having problems, while trying it out: The compiler says this:

Error: Cannot find module 'babel-runtime/helpers/classCallCheck' from 'node_modules/es6-event-emitter/dist'

Any ideas what I should do about that?

NPM Mismatch distro bundle

The fix for #3 is that the package needs to be updated in NPM. If you look in dist:

var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');
var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
var _createClass2 = require('babel-runtime/helpers/createClass');
var _createClass3 = _interopRequireDefault(_createClass2);
var _weakMap = require('babel-runtime/core-js/weak-map');

Triggering events before dom is ready

Hello,

I'm wondering if it's possible to trigger an event on the constructor, or a method that is called immediately after the class is instantiated.

I'm trying to do something like this;

class Bla extends Emitter {
  constructor() {
    super()
    this.trigger('bla:initialised')
}
}

I notice this event doesn't fire, probably because it hasn't fully loaded or something. How would you do something like this?

Cheers

.bablerc in npm package

I'm trying to use this with react-native and ran into it trying to run the .bablerc file from within node_modules. It blows up because of the plugin not being present. Apparently, the convention is becoming not to ship the .bablerc in the npm package by adding it to .npmignore.

facebook/react-native#4062

Are you onboard with that?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.