benlesh on master
fix(pipe): Ensure that `unknown… (compare)
with retryWhen you at least have access to the error, so you can conditionally do something conditionally there
In repeatWhen
you have access to the complete()
call, that will always be void (or well, undefined
), but you can still count the repeats.
repeatWhen(ob => ob.mergeMap((e, i) => timer(i*n)))
could be the correct one.
find
observable$.pipe(
map( _ => {} ),
)
scheduled(EMPTY, scheduler)
?
EMPTY
.
Hello all, I have the following problem having:
long living observable a$
a subject emitting short living observables b$
My goal is to subscribe on the values of a$, when no active observable emitted by b$ is present;
thus take values from a$ until b$ emits an observable, then switch to this observable until it completes, then switch back to taking values from a$; and repeat.
I don't know in what direction to look, tried playing with switchAll but couldn't manage to get it to work.
b$.pipe(switchMap(e => concat(e, a$)))
https://stackblitz.com/edit/rxjs-srwthz?file=index.ts
Made a prototype and it works like a charm, cheers. :)
Hi people, how are you doing today ? :)
First time I'm coming in here (or any other help forum/chat) because i really cannot find information about how to approach the following problem related to observables, typescript and error handling.
This is the problem:
I have a repository class which i use to fetch user information. One of the methods looks like this:
export interface IUser {
id?: number,
username: string,
email: string,
password: string,
}
findByEmail(email: string): Observable<IUser | NotFoundException> {
const user = this._users.find(u => u.email === email);
if (!user) return throwError(new NotFoundException());
return of(user);
}
Because I'm handling the error as seen there, that means that wherever i use this findByEmail method it either returns Observable<IUser> or Observable<never>, which means i have to be checking the type of data returned by the method.
My question is, what's the proper way of handling errors like this without having types becoming annoying to deal with ?
Should i just throw the error normally without using throwError from rxjs ?
Any idea about how to go about this ?
Any example i have found online always uses Observable<any> as return type, which i find kind of bs if one is using typescript (ˆˆ')
I would be very grateful if someone provides a nice way to go about this or some web resource with good examples :). Thanks in advance!
next
callback, not on the error
callback.
Observable<IUser>
to call the error callback with NotFoundException
.
throwError<IUser>(new NotFoundException())
will create an observable typed Observable<IUser>
, but when you subscribe, call the error callback with a NotFoundException
.
Hey folks, I'm trying to learn RxJS for the first time. I have the following:
// TimeSubject is the current heartbeat of the game (i.e. current client time)
// RoundSubject stores information about the current and previous round state
combineLatest([TimeSubject, RoundSubject])
.pipe(
// detect when the bomb is planted
filter(([_, { current }]) => current.bomb === 'planted'),
// create a timer object to keep track of the time until explosion
map(([time, _]) => ({ plantTime: time, explosionTime: time + 40, currentTime: time })),
// ignore other "bomb planted" events for the next 50 seconds
throttleTime(50 * 1000),
// count down until bomb is exploded
// problem: RoundSubject can emit an event indicating the bomb got defused
// or the round ended before the bomb got to explode,
// but how do I catch that since I am throttling the events from that channel?
expand(({ plantTime, explosionTime, currentTime }) =>
explosionTime > currentTime
? of(({ plantTime, explosionTime, currentTime: currentTime + 1 }))
.pipe(delay(1000))
: EMPTY)
).subscribe(({ plantTime, explosionTime, currentTime }) => {
if (plantTime === currentTime)
console.log('Bomb planted and will explode in 40 seconds!');
else if (explosionTime >= currentTime) {
const secondsToExplode = explosionTime - currentTime;
console.log(`.. explodes in: ${secondsToExplode} seconds`);
}
});
How can I, with RxJS, collect more events from RoundSubject
, indicating that the round ended before the bomb went off, or because the bomb got defused? I.e. I want to stop the ".. explodes in X seconds" messages from being emitted when the bomb is defused or the round ended.