Making an Observable from a callback
You can do it like this.
get isLoggedIn(): Observable<boolean> {
// want something like this:
return Observable.create(
observer => firebase
.auth()
.onAuthStateChanged((user) => {
observer.next(!!user)
});
);
}
there is one more way of doing this using bindCallback from rxjs/observable/bindCallback
const observable=bindCallback(functionWhichExpectsCallBack)
e.g
function foo(value, callback) {
callback(value);
}
//converts callback to observable
const observable = bindCallback(foo);
observable(1)
.subscribe(console.log);
Note: bindCallback
and Observable.create
were deprecated,
you could use new Observable
instead:
...
return new Observable((subscriber) => {
firebase
.auth()
.onAuthStateChanged((user) => {
subscriber.next(user);
})
}