made with requirebin
Created
April 19, 2014 06:35
-
-
Save krzysztofantczak/11076043 to your computer and use it in GitHub Desktop.
requirebin sketch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// !! Open devtools console and click 'run' button. | |
// | |
// line 24 is responsible for reemiting received events to 'piped' emitters | |
// but it fails when em1 --pipes--> em2 and em2 --pipes--> em1 | |
// when line 42 is uncommented | |
var events = require('events'); | |
var wrap = function ( emitter ) | |
{ | |
emitter.pipe = function ( targetEmitter ) | |
{ | |
if (!this.pipes) this.pipes = []; | |
this.pipes.push(targetEmitter); | |
this._emit = this.emit; | |
this.emit = function () | |
{ | |
// emit event 'locally' | |
this._emit.apply(this, arguments); | |
// emit event in all piped emitters | |
for (var i = 0; i < this.pipes.length; ++i) | |
{ | |
this.pipes[i].emit.apply(this.pipes[i], arguments); | |
} | |
}; | |
return this; | |
}; | |
return emitter; | |
}; | |
// wrap some dummy emitters so we can connect them using firstEmitter.pipe(secondEmitter) | |
var em1 = wrap(new events.EventEmitter()); | |
var em2 = wrap(new events.EventEmitter()); | |
// pass all emitted events in em1 to em2 | |
em1.pipe(em2); | |
// pass all emitted events in em2 to em1 | |
// em2.pipe(em1); // uncomment that line and it will loop forever | |
em1.on('foo', function(msg) { console.log('em1::foo >', msg); }); | |
em2.on('foo', function(msg) { console.log('em2::foo >', msg); }); | |
// em1::foo will be handled by em1.on('foo') and em2.on('foo'); | |
em1.emit('foo', 'emitted on em1'); | |
// em2::foo will be handled by em2.on('foo'); | |
// - it's also handled by em1.on('foo') when piping em2 back to em1 | |
// but it's useless when emitters are falling into inf loop | |
em2.emit('foo', 'emitted on em2'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require=function e(t,s,n){function r(o,h){if(!s[o]){if(!t[o]){var u="function"==typeof require&&require;if(!h&&u)return u(o,!0);if(i)return i(o,!0);throw Error("Cannot find module '"+o+"'")}var v=s[o]={exports:{}};t[o][0].call(v.exports,function(e){var s=t[o][1][e];return r(s?s:e)},v,v.exports,e,t,s,n)}return s[o].exports}for(var i="function"==typeof require&&require,o=0;n.length>o;o++)r(n[o]);return r}({Sk64m6:[function(e,t){function s(){this._events=this._events||{},this._maxListeners=this._maxListeners||void 0}function n(e){return"function"==typeof e}function r(e){return"number"==typeof e}function i(e){return"object"==typeof e&&null!==e}function o(e){return void 0===e}t.exports=s,s.EventEmitter=s,s.prototype._events=void 0,s.prototype._maxListeners=void 0,s.defaultMaxListeners=10,s.prototype.setMaxListeners=function(e){if(!r(e)||0>e||isNaN(e))throw TypeError("n must be a positive number");return this._maxListeners=e,this},s.prototype.emit=function(e){var t,s,r,h,u,v;if(this._events||(this._events={}),"error"===e&&(!this._events.error||i(this._events.error)&&!this._events.error.length))throw t=arguments[1],t instanceof Error?t:TypeError('Uncaught, unspecified "error" event.');if(s=this._events[e],o(s))return!1;if(n(s))switch(arguments.length){case 1:s.call(this);break;case 2:s.call(this,arguments[1]);break;case 3:s.call(this,arguments[1],arguments[2]);break;default:for(r=arguments.length,h=Array(r-1),u=1;r>u;u++)h[u-1]=arguments[u];s.apply(this,h)}else if(i(s)){for(r=arguments.length,h=Array(r-1),u=1;r>u;u++)h[u-1]=arguments[u];for(v=s.slice(),r=v.length,u=0;r>u;u++)v[u].apply(this,h)}return!0},s.prototype.addListener=function(e,t){var r;if(!n(t))throw TypeError("listener must be a function");if(this._events||(this._events={}),this._events.newListener&&this.emit("newListener",e,n(t.listener)?t.listener:t),this._events[e]?i(this._events[e])?this._events[e].push(t):this._events[e]=[this._events[e],t]:this._events[e]=t,i(this._events[e])&&!this._events[e].warned){var r;r=o(this._maxListeners)?s.defaultMaxListeners:this._maxListeners,r&&r>0&&this._events[e].length>r&&(this._events[e].warned=!0,console.error("(node) warning: possible EventEmitter memory leak detected. %d listeners added. Use emitter.setMaxListeners() to increase limit.",this._events[e].length),console.trace())}return this},s.prototype.on=s.prototype.addListener,s.prototype.once=function(e,t){function s(){this.removeListener(e,s),r||(r=!0,t.apply(this,arguments))}if(!n(t))throw TypeError("listener must be a function");var r=!1;return s.listener=t,this.on(e,s),this},s.prototype.removeListener=function(e,t){var s,r,o,h;if(!n(t))throw TypeError("listener must be a function");if(!this._events||!this._events[e])return this;if(s=this._events[e],o=s.length,r=-1,s===t||n(s.listener)&&s.listener===t)delete this._events[e],this._events.removeListener&&this.emit("removeListener",e,t);else if(i(s)){for(h=o;h-->0;)if(s[h]===t||s[h].listener&&s[h].listener===t){r=h;break}if(0>r)return this;1===s.length?(s.length=0,delete this._events[e]):s.splice(r,1),this._events.removeListener&&this.emit("removeListener",e,t)}return this},s.prototype.removeAllListeners=function(e){var t,s;if(!this._events)return this;if(!this._events.removeListener)return 0===arguments.length?this._events={}:this._events[e]&&delete this._events[e],this;if(0===arguments.length){for(t in this._events)"removeListener"!==t&&this.removeAllListeners(t);return this.removeAllListeners("removeListener"),this._events={},this}if(s=this._events[e],n(s))this.removeListener(e,s);else for(;s.length;)this.removeListener(e,s[s.length-1]);return delete this._events[e],this},s.prototype.listeners=function(e){var t;return t=this._events&&this._events[e]?n(this._events[e])?[this._events[e]]:this._events[e].slice():[]},s.listenerCount=function(e,t){var s;return s=e._events&&e._events[t]?n(e._events[t])?1:e._events[t].length:0}},{}],events:[function(e,t){t.exports=e("Sk64m6")},{}]},{},[]);var events=require("events"),wrap=function(e){return e.pipe=function(e){return this.pipes||(this.pipes=[]),this.pipes.push(e),this._emit=this.emit,this.emit=function(){this._emit.apply(this,arguments);for(var e=0;this.pipes.length>e;++e)this.pipes[e].emit.apply(this.pipes[e],arguments)},this},e},em1=wrap(new events.EventEmitter),em2=wrap(new events.EventEmitter);em1.pipe(em2),em1.on("foo",function(e){console.log("em1::foo >",e)}),em2.on("foo",function(e){console.log("em2::foo >",e)}),em1.emit("foo","emitted on em1"),em2.emit("foo","emitted on em2"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "requirebin-sketch", | |
"version": "1.0.0", | |
"dependencies": {} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<style type='text/css'>html, body { margin: 0; padding: 0; border: 0; } | |
body, html { height: 100%; width: 100%; }</style> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment