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

BaconJS examples? #133

Open
frankandrobot opened this issue Jul 19, 2017 · 8 comments
Open

BaconJS examples? #133

frankandrobot opened this issue Jul 19, 2017 · 8 comments

Comments

@frankandrobot
Copy link
Contributor

In the docs you ask for help porting over the BaconJS examples. Any particular examples you had an mind? Note that we would need to add a debounce as streams/observables make extensive use of debouncing streams.

@hung-phan
Copy link
Collaborator

hung-phan commented Jul 19, 2017

Hi @frankandrobot, I would love if you can have a look at distinctUntilChanged. I think it is a good example to start with, and I would love to see any idea on this one. PS: Not really baconjs example but I think baconjs would have sth similar.

const $input = $('#input');
const $results = $('#results');

/* Only get the value from each key up */
var keyups = Rx.Observable.fromEvent($input, 'keyup')
  .pluck('target', 'value')
  .filter(text => text.length > 2 );

/* Now debounce the input for 500ms */
var debounced = keyups
  .debounce(500 /* ms */);

/* Now get only distinct values, so we eliminate the arrows and other control characters */
var distinct = debounced
  .distinctUntilChanged();

@frankandrobot
Copy link
Contributor Author

As I said above, we need a debounce. Here's an initial first pass.

  1. How do we code share? Fork and create a PR? Where do you want this code?
  2. The code below doesn't handle channel closing. The go callback is an infinite loop.
function* debouce(inc, timeout) {
	var scheduler
	const outc = csp.chan();
	csp.go(function* () {
		while(true) {
			const next = yield csp.take(inc);
			scheduler && clearTimeout(scheduler);
			scheduler = setTimeout(
				() => {
					csp.put(outc, next);
				},
				timeout);
		}
	});
	return outc;
}

(I apologize also for the tabs. Need to config emacs)

@hung-phan
Copy link
Collaborator

I think you can create a PR with your implementation into the examples folder. It would be easier for anyone to review

@frankandrobot
Copy link
Contributor Author

What formatting changes?

@hung-phan
Copy link
Collaborator

hung-phan commented Jul 26, 2017

code inside script tag formatting. This should be an example https://github.com/frankandrobot/js-csp/blob/master/examples/web/throttle.html. Your code need to be proper format like this example.

@corporatepiyush
Copy link

I think the code for debounce operation is worth putting in csp.operations.js

@frankandrobot
Copy link
Contributor Author

frankandrobot commented Aug 8, 2017

Another example worth doing (and it doesn't have to be a web example) is flatMapLatest. This is a big deal when doing async workflows since it lets you automagically discard all but the latest ajax request. Here's a web example:

var userPasswordStream = // fires when user enters password and presses OK
var tokenStream = userPasswordStream.flatMapLatest(login) // where `login` is an AJAX request

For a non-web example, see https://rpominov.github.io/kefir/#flat-map-latest

var source = Kefir.sequentially(100, [1, 2, 3]); // outputs 1,2,3 every 100 ms
var result = source.flatMapLatest(x => Kefir.interval(40, x).take(4));
result.log();

source:      ----------1---------2---------3X

spawned 1:             ---1---1---1---1X
spawned 2:                       ---2---2---2---2X
spawned 3:                                 ---3---3---3---3X

result:      -------------1---1-----2---2-----3---3---3---3X

@frankandrobot
Copy link
Contributor Author

@gt3 @hung-phan ?

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

3 participants