Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using an Observable to detect a change in a variable

I think I misunderstand how Observables are supposed to be used. I want to put a value in, and when the value changes it should emit the new value. I thought that was what they were for, but all the tutorials and docs don't seem to do this, but at the same time, I always see them being applied this way. For example, in angular when you subscribe to a "FirebaseListObservable", when the value in firebase changes it fires off a snapshot in the subscription. I want to make that for my own variable. Let's say I just have a string variable, and when it changes, it fires off any subscriptions.

like image 654
Jus10 Avatar asked Sep 09 '17 15:09

Jus10


People also ask

What is observable change?

capable of being or liable to be observed; noticeable; visible; discernible: an observable change in attitude. worthy or important enough to be celebrated, followed, or observed: an observable holiday.

How do you know if observable has value?

The RxJS isEmpty() operator returns an observable with a Boolean value indicating whether the observable was empty or not. It returns an output as true if the source observable is empty; otherwise, false.

How does an observable work?

The Observable is initialized with a function. This function is the observer . The observer gets called with an object, the subscriber . Once the observer function gets called with the subscriber object, it can call three methods in this subscriber — the next , error , and complete methods.

Should I use promise or observable?

Often Observable is preferred over Promise because it provides the features of Promise and more. With Observable it doesn't matter if you want to handle 0, 1, or multiple events. You can utilize the same API in each case. Observable also has the advantage over Promise to be cancellable.


1 Answers

Normally I would have my observables in services that get subscribed to in components, but I bundled them all in one class for the convenience of this answer. I've listed comments explaining each step. I hope this helps. : )

import { Subject } from 'rxjs/Subject';  export class ClassName {     // ------ Creating the observable ----------    // Create a subject - The thing that will be watched by the observable    public stringVar = new Subject<string>();     // Create an observable to watch the subject and send out a stream of updates (You will subscribe to this to get the update stream)    public stringVar$ = this.stringVar.asObservable() //Has a $      // ------ Getting Your updates ----------    // Subscribe to the observable you created.. data will be updated each time there is a change to Subject    public subscription = this.stringVar$.subscribe(data => {          // do stuff with data          // e.g. this.property = data    });    // ------ How to update the subject ---------    // Create a method that allows you to update the subject being watched by observable    public updateStringSubject(newStringVar: string) {      this.stringVar.next(newStringVar);    }    // Update it by calling the method..    // updateStringSubject('some new string value')     // ------- Be responsible and unsubscribe before you destory your component to save memory ------    ngOnDestroy() {      this.subscription.unsubscribe()    } } 
like image 157
Jonathan002 Avatar answered Sep 21 '22 11:09

Jonathan002