Simple Emitter Example
simplygreatwork opened this issue · comments
Please consider including a dead simple example in the docs for writing to a Lazy.js stream. The existing example for Lazy.createWrapper() is too complicated. Also, consider renaming Lazy.createWrapper to something else: like Lazy.emitter, Lazy.createEmitter, Lazy.factory, or Lazy.createFactory
The following code is what I was aiming to achieve. However, my data will ultimately come from a WebSocket. I did manage to work out how to simply and dynamically originate a Lazy.js factory.
Lazy.emitter = Lazy.createWrapper;
Lazy.emitter(function() {
var counter = 0;
setInterval(function() {
this.emit(++counter);
}.bind(this), 100);
})()
.take(5)
.each(function(each) {
console.log('each: ' + each);
});
Here's another simple example:
Lazy.emitter = function() {
return {
emit : function(data) {
this.emitter.emit(data);
}
}
};
Lazy.stream = function(emitter) {
return Lazy.createWrapper(function() {
emitter.emitter = this;
})();
};
Lazy.stream(this.emitter = Lazy.emitter())
.each(function(each) {
console.log('each: ' + each);
});
this.emitter.emit('woot');