2

The following code causes the browser to stop responding, using a value like '100' for example for the repeat solves the problem, but in my case I don't have specific value for it. Would you please suggest a solution:

var observer = Rx.Observer.create(function (x)
                                  {
                                     console.log(x);
                                  },
                                  function (err)
                                  {
                                     console.log('Error: ' + err);
                                  },
                                  function ()
                                  {
                                     console.log('Completed');
                                  });

var repeat = Rx.Observable.repeat(10, null); //repeat indefinitely
var interval = Rx.Observable.interval(1000);

var zip = Rx.Observable.zip(repeat,
                            interval,
                            function(rep, inter)
                            {
                               return rep + inter;
                            });

zip.subscribe(observer);

2 Answers2

1

The browser freezes because .repeat simply yields 10 indefinitely.

Since Rx is push-based, we have no way of knowing when zip needs another item. Instead, we just push new values to zip as they become available. The static (class-method?) repeat says "hey, I have new items RIGHT NOW ALWAYS HERE THEY ARE" and never relinquishes control-flow back to zip. This means zip never actually ends up subscribing to the interval observable, so zip just starts buffering indefinitely.

If you're coming from a functional background, then it would seem like an "infinite" list of "10"s would zip nicely with a finite list of anything. Which is absolutely true, assuming your infinite list is lazy. In this case, our "list" has a mind of it's own, and definitely isn't lazy.

I'd be happy to suggest a solution, but it seems that the example is contrived. What exactly are you attempting to do?

cwharris
  • 17,835
  • 4
  • 44
  • 64
  • I've posted a related question which might be helpful to readers here: http://stackoverflow.com/questions/25893983/create-infinite-repeatable-observable-from-array. Would you mind having a look? – Yousef Sep 17 '14 at 14:58
0

I was dealing with the same problem. Looks like delay can do the trick.

Here's the slightly modified version of your code:

var observer = Rx.Observer.create(function (x)
                                  {
                                     console.log(x);
                                  },
                                  function (err)
                                  {
                                     console.log('Error: ' + err);
                                  },
                                  function ()
                                  {
                                     console.log('Completed');
                                  });

var repeat = Rx.Observable.of(10).delay(0).repeat(-1); //repeat indefinitely
var interval = Rx.Observable.interval(1000);

var zip = Rx.Observable.zip(repeat,
                            interval,
                            function(rep, inter)
                            {
                               return rep + inter;
                            });

zip.subscribe(observer);
radmen
  • 1,584
  • 9
  • 13