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

Alternative for join patterns #5098

Open
xgrommx opened this issue Oct 20, 2019 · 2 comments
Open

Alternative for join patterns #5098

xgrommx opened this issue Oct 20, 2019 · 2 comments

Comments

@xgrommx
Copy link

xgrommx commented Oct 20, 2019

Hello @benlesh

In baconjs and old version of rx we have feature like join patterns. But how can I emulate it in current version of Rx?

Example with bacon

const Bacon = require('baconjs');

var chopsticks = [new Bacon.Bus(), new Bacon.Bus(), new Bacon.Bus()]
var hungry     = [new Bacon.Bus(), new Bacon.Bus(), new Bacon.Bus()]

var eat = function(i) {
  return function() {
    setTimeout(function() {
      console.log('done!')
      chopsticks[i].push({})
      chopsticks[(i+1) % 3].push({})
    }, 1000);
    return 'philosopher ' + i + ' eating'
  }
}

var dining = Bacon.when(
  [hungry[0], chopsticks[0], chopsticks[1]],  eat(0),
  [hungry[1], chopsticks[1], chopsticks[2]],  eat(1),
  [hungry[2], chopsticks[2], chopsticks[0]],  eat(2))

dining.log()

chopsticks[0].push({}); 
chopsticks[1].push({}); 
chopsticks[2].push({})

for (var i = 0; i < 3; i++) {
  hungry[0].push({}); 
  hungry[1].push({}); 
  hungry[2].push({})
}

Output

// philosopher 0 eating
// done!
// philosopher 2 eating
// done!
// philosopher 1 eating
// done!
// philosopher 0 eating
// done!
// philosopher 2 eating
// done!
// philosopher 1 eating
// done!
// philosopher 0 eating
// done!
// philosopher 2 eating
// done!
// philosopher 1 eating
// done!

Old version of Rx

const Rx = require('rx');

var chopsticks = [new Rx.Subject(), new Rx.Subject(), new Rx.Subject()]
var hungry     = [new Rx.Subject(), new Rx.Subject(), new Rx.Subject()]

var eat = function(i) {
  return function() {
    setTimeout(function() {
      console.log('done!')
      chopsticks[i].onNext({})
      chopsticks[(i+1) % 3].onNext({})
    }, 1000);
    return 'philosopher ' + i + ' eating'
  }
}

var dining = Rx.Observable.when(
  hungry[0].and(chopsticks[0]).and(chopsticks[1]).thenDo(eat(0)),
  hungry[1].and(chopsticks[1]).and(chopsticks[2]).thenDo(eat(1)),
  hungry[2].and(chopsticks[2]).and(chopsticks[0]).thenDo(eat(2))
)

dining.subscribe(x => console.log(x));

chopsticks[0].onNext({}); 
chopsticks[1].onNext({}); 
chopsticks[2].onNext({})

for (var i = 0; i < 3; i++) {
  hungry[0].onNext({}); 
  hungry[1].onNext({}); 
  hungry[2].onNext({})
}

Output

// philosopher 0 eating
// done!
// philosopher 2 eating
// done!
// philosopher 1 eating
// done!
// philosopher 0 eating
// done!
// philosopher 2 eating
// done!
// philosopher 1 eating
// done!
// philosopher 0 eating
// done!
// philosopher 2 eating
// done!
// philosopher 1 eating
// done!

also probably will be helpful to use live example
https://codesandbox.io/s/elastic-khayyam-wxh62

@xgrommx
Copy link
Author

xgrommx commented Nov 1, 2019

@benlesh Are u here?)

@benlesh
Copy link
Member

benlesh commented Nov 5, 2019

I am here, I'm just sort of the waiting for the community to comment on the desire for this feature. It's been brought up before as something we might want to add back into the library, but never gained traction.

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

2 participants