ee

Event-Driven library.

Ee

Event-Driven library inspired by the EventEmitter.

Event defaults to Ee.Event

var ee = new Ee();

ee.Event === Ee.Event is true

or

var CustomEvent = function(event) {
  Ee.Event.call(this, event);
};
 
CustomEvent.prototype = Object.create(Ee.Event.prototype);
CustomEvent.prototype.nyan = function() {
  return 'nyan!';
};
 
var ee = new Ee(CustomEvent);

and

ee.on('nyan', function(e) {
  console.log(e.nyan());
});

ee.Event === CustomEvent is true

Adds a listener to the end of the listeners for the specified event.

Adds a listener to the beginning of the listeners for the specified event.

Adds a one time listener to the end of the listeners for the specified event.

ee.on('nyan', function(e) {
  console.log('on nyan!');
}).once('nyan', function(e) {
  console.log('once nyan?');
}).first('nyan', function(e) {
  console.log('first nyan');
});
 
ee.on('neko', function(e) {
  console.log(instanceof ee.Event);
});

then

ee.emit('nyan');
first nyan
on nyan!
once nyan?
ee.emit('nyan');
first nyan
on nyan!
ee.emit('neko');
true

Remove a listener or all listeners for the specified event.

Returns the number of listeners for the specified event or all listeners.

Returns number of executions for the specified event or for all events.

Returns an array of listeners for the specified event.

Execute each of the listeners in order with the supplied arguments.

ee.on('nyan', function(eargument1argument2argument3) {
  console.log(argument1);
  console.log(argument2);
  console.log(argument3);
});
 
ee.emit('nyan', 'nyan!', 'nyan?');
nyan!
nyan?
undefined

Otherwise,

ee.emit(new ee.Event('nyan'), 'nyan!', 'nyan?');

Execute each of the listeners.

ee.on('nyan', function(eargument1argument2argument3) {
  console.log(argument1);
  console.log(argument2);
  console.log(argument3);
 
  setTimeout(function() {
    e.next(); // must be called 
  }, 1000);
}).on('nyan', function(eargument1) {
  console.log(argument1);
  e.next(); // must be called 
});

then

ee.chain('nyan', ['nyan!', 'nyan?'], function(e) { // complete 
  console.log('complete!');
}, function(objecteargs) { // hook 
  console.log('current listener expects ' + object.listener.length + ' arguments');
});
 
console.log('◡( ╹◡╹ )◡');
current listener expects 4 arguments
nyan!
nyan?
undefined
◡( ╹◡╹ )◡
current listener expects 2 arguments
nyan!
complete!

or

ee.chain('nyan', ['nyan!', 'nyan?'], function(e) { // complete 
  console.log('complete!');
}, function(objecteargscall) { // hook 
  console.log('current listener expects ' + object.listener.length + ' arguments');
  setTimeout(function() {
    call(); // must be called 
  }, 100);
});
 
console.log('◡( ╹◡╹ )◡');
current listener expects 4 arguments
◡( ╹◡╹ )◡
nyan!
nyan?
undefined
current listener expects 2 arguments
nyan!
complete!

application

ee.on('nyan', function() {
  // something to do. 
}).on('nyan', function(enext) {
  setTimeout(function() {
    next();
  }, 1000);
}).on('nyan', function(e) {
  // something to do. 
});
 
ee.chain('nyan', function(e) {
  console.log('complete!');
}, function(objecteargscall) {
  if (object.listener.length < 2) {
    call();
    e.next();
  } else {
    if (typeof args[1] !== 'function') {
      args[1] = function() {
        e.next();
      };
    }
 
    call();
  }
});
complete!

Execute each of the listeners.

ee.on('nyan', function(e) {
  setTimeout(function() {
    e.set('data 1', 'listener 1');
    e.done(); // must be called 
  }, (Math.floor(Math.random() * 10) + 1) * 1000);
}).on('nyan', function(e) {
  setTimeout(function() {
    e.set('data 2', 'listener 2');
    e.done(); // must be called 
  }, (Math.floor(Math.random() * 10) + 1) * 1000);
}).on('nyan', function(e) {
  setTimeout(function() {
    e.set('data 3', 'listener 3');
    e.done(); // must be called 
  }, (Math.floor(Math.random() * 10) + 1) * 1000);
});
 
ee.parallel('nyan', function(e) {
  console.log(e.get('data 1'));
  console.log(e.get('data 2'));
  console.log(e.get('data 3'));
});
listener 1
listener 2
listener 3

Create a new instance.

Set e.aborted to true and skip other listeners.

ee.on('nyan', function(e) {
  console.log('first listener.');
 
  if (true)
    return e.abort(); // should be return. 
 
  console.log('first listener!');
}).on('nyan', function(e) {
  console.log('second listener.');
});
 
ee.emit('nyan');
first listener.

Set e.prevented to true and skip other listeners.

ee.on('nyan', function(e) {
  e.next();
}).on('nyan', function(e) {
  if (confirm('do you nyan?')) {
    e.next();
  } else {
    e.prevent();
  }
});
 
ee.chain('nyan', function(e) {
  if (e.prevented) {
    // do nothing. 
  } else {
    alert('にゃん♡');
  }
});

Set e.aborted and e.prevented to true and skip other listeners.

Execute next listener.

Get value or default_value.

Set value for key.

Unset key.

The MIT License (MIT)

Copyright (c) 2013 tatあt ioiioioloo@gmail.com

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.