Last active
September 4, 2021 18:06
-
-
Save banujan6/12f1a09a80cf168cde963246f36641ad to your computer and use it in GitHub Desktop.
Rxjs Medium
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
First Example | |
**/ | |
import { Observable } from 'rxjs'; | |
// Observable Object | |
const observable = new Observable(subscriber => { | |
subscriber.next("First Message"); | |
subscriber.next("Second Message"); | |
}); | |
// Observer Object | |
const observer = { | |
next: (data) => console.log('We Received new data ' + data), | |
error: (err) => console.error('Oh, No. We got an error : ' + err), | |
complete: () => console.log('Done. Nothing more.') | |
}; | |
// Subscribing | |
const subscription = observable.subscribe(observer); | |
// Unsubscribing | |
subscription.unsubscribe(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
Second Example | |
**/ | |
import { Observable } from 'rxjs'; | |
// First Observable Object | |
const observable = new Observable(subscriber => { | |
subscriber.next("First Message"); | |
}); | |
// Second Observable Object | |
const observable2 = new Observable(subscriber => { | |
subscriber.next("Second Message"); | |
}); | |
// first subscription | |
const subscription = observable.subscribe(data => console.log("We got a data " + data)); | |
// second subscription | |
const subscriptionChild = observable2.subscribe(data => console.log("We got a child data " + data)); | |
// adding 2nd subscription as child of 1st | |
subscription.add(subscriptionChild); | |
// unsubscribing parent BUT child also will be unsubscribed | |
subscription.unsubscribe(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment