Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I observe additions to an array with rx.js?

Tags:

fromArray Rx wiki on github

coffee> rext = require 'rx'                                                  coffee> arr = [1..5]                                                  [ 1, 2, 3, 4, 5 ]                                                     coffee> obs = rext.Observable.fromArray(arr)                          { _subscribe: [Function] }                                            coffee> obs.subscribe( (x) -> console.log("added value: " + x))       added value: 1                                                        added value: 2                                                        added value: 3                                                        added value: 4                                                        added value: 5                                                        { isStopped: true,                                                      observer:                                                              { isStopped: true,                                                      _onNext: [Function],                                                  _onError: [Function: defaultError],                                   _onCompleted: [Function: noop] },                                  m: { isDisposed: true, current: null } }                            coffee> arr.push(12)    # expecting "added value: 12"                                               6                       # instead got new length of array                                               coffee>           

It really looks like the subscribe function will only fire one time, when it's created. It seems like it's a bit of a misnomer, since I'm really just for-eaching the array instead of observing changes on it. That code is almost exactly the same as what's on the wiki though. So either I'm doing it wrong or the subscribe doesn't work how I expect.

like image 678
jcollum Avatar asked Jan 22 '13 19:01

jcollum


People also ask

When to use observable?

Observables provide support for passing messages between parts of your application. They are used frequently in Angular and are a technique for event handling, asynchronous programming, and handling multiple values.

How to declare observable in Angular?

observable. subscribe((data) => { console. log(data); // should be 'data to send can be object or anything' }); you can also convert it to promise using toPromise() or fromPromise(observable) operator and so on.

How does Rx JS work?

RxJS comes with an object called Observable . Within this Observable object, you can create a container for your 'observing' item. It then pipelines your item into a subscriber that does something when the conditions of the subscription are met.


2 Answers

In RxJS what you are looking for is called a Subject. You can push data into it and stream it from there.

  • Subject getting started guide
  • Subject API docs.

Example:

var array = []; var arraySubject = new Rx.Subject();  var pushToArray = function (item) {   array.push(item);   arraySubject.next(item); }  // Subscribe to the subject to react to changes arraySubject.subscribe((item) => console.log(item)); 
like image 183
Frederic Leitenberger Avatar answered Sep 30 '22 17:09

Frederic Leitenberger


I found Rx.Observable.ofObjectChanges(obj) to work just as I expected.

From the documentation page:

Creates an Observable sequence from changes to an object using Object.observe.

Hope it helps.

like image 43
Pablo Avatar answered Sep 30 '22 16:09

Pablo