Giter Site home page Giter Site logo

mc's Introduction

mc - as in Master of Ceremonies

"Event emitting" with chained and sequential event lists

largely based off of: https://github.com/jeromeetienne/microevent.js and some of https://github.com/primus/eventemitter3

methods:

on('eventname', function) -set function for event name

emit('eventname', param1, param2, etc...) -triggers all associated on functions in sequence they were provided

once('eventname', function) -triggers only once and then destroyed

removeAllListeners() -destroys all events and associated data

removeListener('event', fn) the function is optional but will match function if included
-completely removes event and/or associated function(s)

chain('event', param1, param2, param3, cb) -goes through associated event list and passes results from one to the next until hitting the CB

listeners(event, exists) exists is optional
if exists, returns boolean if exists
if exists is not supplied, then returns array of associated functions

mixin(obj) takes an object or function and appropriately adds functions to it.

For use int the browswer or server side.

Totally vanilla javascript, minimal and still has a few lingering comments but works.

Example implementation:

var telephone = {};  
mc.mixin(telephone);  
// or just var telephone = new mc();  
  
function joespeaks(heard, cb){  
  console.log(heard);  
  cb(null, "i heard you said this");  
}  
function bobspeaks(heard, cb){  
  console.log(heard);  
  cb(null, "i heard you say fish");  
}  
function tinaspeaks(heard, cb){  
  console.log(heard);  
  cb(null, "i hear you did it");  
}  
function jamiespeaks(heard, cb){  
  console.log(heard);  
  cb(null, "what did I do?");  
}  
  
telephone.on('listen', jospeaks);  
telephone.on('listen', bobspeaks);  
telephone.on('listen', tinaspeaks);  
telephone.on('listen', jamiespeaks);  
  
telephone.chain('listen', 'I heard you say dis', function(er, res){  
  console.log(er, res);  
});  
  
telephone.emit('listen', 'I hear you need this');  

==

  • Chain simply runs down the list passing from one to the next and also delivers the end result to the callback.

  • It automatically detects the number of arguments at each event level. Anytime a callback is called with the first argument, it stops running through the event list and delivers the error to the event callback.

  • It is written in a long form to pick up the number of arguments with no use of call or apply type function.

    This is totally vanilla js context drive - or you can say "contextless". This is because there is no binding to objects used.

In some instances, you would use .bind or set a context which you can do on your own. Although, this is just plain vanilla with no additions and is apart of utilizing more of the native engine optimization and plain JS.

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.