Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simple Emitter Example #218

Open
simplygreatwork opened this issue Apr 28, 2018 · 1 comment
Open

Simple Emitter Example #218

simplygreatwork opened this issue Apr 28, 2018 · 1 comment

Comments

@simplygreatwork
Copy link

simplygreatwork commented Apr 28, 2018

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);
});
@simplygreatwork
Copy link
Author

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');

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant