11900

Create infinite repeatable Observable from array

Let's say I have an array <strong>items</strong>

I know I can create an observable from this array using

Rx.Observable.fromArray(items)

How do I create a lazily infinitely repeating observable from this (i.e.: repeating the items as long as they are being requested)?

Tried

Rx.Observable.fromArray(items).repeat()

But this doesn't execute lazily and therefor locks up the browser.

Answer1:

You cannot do this with an Observable. You way want to look at using an Enumerable.

The Enumerable flavor of Reactive Extensions is known as Interective Extensions.

Answer2:

I'm still a newcomer to RxJS, so perhaps what I am proposing is complete madness, but could something along the lines of the following work for this?

var items = [1, 2, 3, 4, 5]; var infiniteSource = Rx.Observable.from(items) .map(function (x) { return Rx.Observable.return(x).delay(1000); }) .concatAll() .doWhile(function(_) { return true; /* i.e. never end */ }); infiniteSource.subscribe(function(x) { console.log(x); });

I have an example here: http://ctrlplusb.jsbin.com/sihewo/edit?js,console

The delay is put in there so as not to flood the console. In terms of the "until no longer needed part" perhaps an unsubscribe or other mechanism can be injected into the doWhile?

Recommend

  • Cache-aware tree impementation
  • Ext JS Licensing Options [closed]
  • Writing a Pandas Dataframe to MySQL
  • Instance vs. static method. Calling it statically or dynamically
  • Generated signed Apk successfully but when I try to Run my project, I get an error
  • VBA Compare 2 sheets, move old comments to new sheet
  • Hide or disable WebView softkeyboard Go/Done/Enter button
  • Does using Meteor with React mean that you don't have to use IronRouter or FlowRouter?
  • Set default option in md-select in angular material 2
  • A regex for maximal periodic substrings
  • Send big file over reactive stream
  • Filter Values of Current Week with XQuery
  • Why isn't this code behaving asynchronous
  • Entity Framework - Include in sub query? - Part 2
  • C++ mySQL connector LINKER errors (Windows)
  • How to make a shared resource thread-safe when using dependency injection?
  • ruby - lazily iterate through an array
  • Entity Framework 4.1. Updating many-to-many relationships. Is this the right way?
  • Why am I getting an error in my ASP.NET Core SQL Server Express Connection String?
  • iPhone - UIWebView multiple locks
  • How concerned should we be about thread safety with JSF managed beans?
  • Entity Framework Core: Include many-to-many related objects in WebAPI
  • How can I stop my python script when another python script is running?
  • Sum values in array of hash if they have the same value
  • Firefox extension testing and developing - I'm confused
  • goJS dropdown remove items
  • Magento get URL before current
  • Prevent Tomcat from caching request during starup
  • what makes a request a new request in asp.net C#
  • How do I signal completion of my dataflow?
  • Security issues with PHP's Readfile method
  • System.InvalidCastException: Specified cast is not valid
  • How to use JavaScript to determine whether a file exists in a directory?
  • error importing numpy
  • onBackPressed() not being executed
  • why do I get the error when installing the gem 'pg'? [duplicate]
  • Read a local file using javascript
  • Apache 2.4 and php-fpm does not trigger apache http basic auth for php pages
  • Delete MySQLi record without showing the id in the URL
  • Programmatically clearing map cache