Skip to content

Instantly share code, notes, and snippets.

@jcppman
Created April 15, 2015 06:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jcppman/1cb50b9e5653ec880576 to your computer and use it in GitHub Desktop.
Save jcppman/1cb50b9e5653ec880576 to your computer and use it in GitHub Desktop.
mu-emitter exercise
define([ "./config", "when/when" ], function (config, when) {
"use strict";
var UNDEFINED;
var CALLBACK = config.callback;
var SCOPE = config.scope;
var HEAD = config.head;
var NEXT = config.next;
return function executor(event, handlers, args) {
var _handlers = [];
var _handlersCount = 0;
var _callback = event[CALLBACK];
var _scope = event[SCOPE];
var handler;
for (handler = handlers[HEAD]; handler !== UNDEFINED; handler = handler[NEXT]) {
if (_callback && handler[CALLBACK] !== _callback) {
continue;
}
if (_scope && handler[SCOPE] !== _scope) {
continue;
}
_handlers[_handlersCount++] = handler;
}
//return _handlers.reduce(function (previous, _handler) {
return when.reduce(_handlers, function (previous, _handler) {
return _handler.handle(previous);
}, args);
};
});
require({
"baseUrl": "bower_components",
"deps": [ "mu-emitter/main", "../executor", "when/when" ],
"callback": function (Emitter, Executor, when) {
var emitter1 = new Emitter();
emitter1.on('x', f1);
emitter1.one('x', f1);
//emitter1.on('x', f1);
//emitter1.on('x', f2);
//emitter1.on('x', f3);
//emitter1.on('x', f4);
function f1(value){
console.log(value);
return [value + 1];
}
function f2(value){
console.log(value);
return [value + 1];
}
function f3(value){
console.log(value);
return [value + 1];
}
function f4(value){
console.log(value);
return [value + 1];
}
var res = emitter1.emit({
type: 'x',
executor: Executor
}, 1);
var res2 = emitter1.emit({
type: 'x',
executor: Executor
}, 1);
res.then(function(value){
console.log(value);
});
function callback(){
console.log('call me', this);
return when.resolve('x').delay(1000).then(function(){
console.log('bump');
});
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment