Git Product home page Git Product logo

dead-simple-event-bus's Introduction

Dead simple event bus that can be used in Browsers and Node.js

Installation

Available via npm

npm install dead-simple-event-bus 

Public API

It as 3 methods.

  • add()
  • remove()
  • trigger()

API can be used in 3 ways , namely

  • Single event
  • Multiple events with args style
  • Multiple events with array style

Browser Setup

<script type="text/javascript" src="node_modules/dead-simple-event-bus/cli.js"></script>
<script type="text/javascript">
var ebSingle = new window.EventBus();
var ebArgsStyle = new window.EventBus();
var ebArrayStyle = new window.EventBus();
</script>

node module

var EventBus = require('dead-simple-event-bus').EventBus;
var ebSingle = new EventBus();
var ebArgsStyle = new EventBus();
var ebArrayStyle = new EventBus();

Single event

//event Bus with single event 
ebSingle.add('a' , function(a , b ){ console.log(a + ' ' + b ); });
ebSingle.trigger('a' , [ 1 , 2 ]);
ebSingle.remove('a' , function(a , b ){ console.log(a + ' ' + b ); } );

Multiple events with args style

ebArgsStyle.add('a' , function(a , b ){ console.log(a + ' ' + b ); } , function(a , b ){ console.log( b + ' ' + a  ); });
ebArgsStyle.trigger('a' , [ 1 , 2 ]);
ebArgsStyle.remove('a' , function(a , b ){ console.log(a + ' ' + b ); } , function(a , b ){ console.log( b + ' ' + a  ); });

Multiple events with array style

ebArrayStyle.add('a' ,[ function(a , b ){ console.log(a + ' ' + b ); } , function(a , b ){ console.log( b + ' ' + a  ); } ] );
ebArrayStyle.trigger('a' , [ 1 , 2 ]);
ebArrayStyle.remove('a' , [ function(a , b ){ console.log(a + ' ' + b ); } , function(a , b ){ console.log( b + ' ' + a  ); }]);

Duplicate functions

dead-simple-event-bus optimizes the event bus by not allowing more than one copy of function to be added.

ebSingle.add('a' , function(a , b ){ console.log(a + ' ' + b ); }); // assigns event 
ebSingle.add('a' , function(a , b ){ console.log(a + ' ' + b ); }); // avoids assigning and prints message

By default , duplicate functions print a message to console and doesn't get addedup again on the event bus. If you want to handle this manually , you could dig a bit into the source and insert a callback.

Contribution

More than welcomed !

License

dead-simple-event-bus's People

Contributors

scriptnull avatar

Watchers

 avatar  avatar  avatar

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.