Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
  • 16:36
    beck opened #6225
  • 13:04
    cartant commented #6215
  • 12:58
    cartant commented #6215
  • 12:41
    cartant commented #6215
  • 11:49
    cartant commented #6215
  • 11:33
    cartant commented #6215
  • 11:33
    cartant commented #6215
  • 11:10
    cartant commented #6215
  • 10:33
    cartant commented #6215
  • 00:39

    benlesh on master

    fix(pipe): Ensure that `unknown… (compare)

  • 00:39
    benlesh closed #6212
  • 00:36
    benlesh closed #5391
  • 00:36
    benlesh commented #5391
  • Apr 12 22:01
    benlesh commented #6215
  • Apr 12 21:56
    cartant commented #6215
  • Apr 12 21:55
    cartant commented #6215
  • Apr 12 21:54
    cartant commented #6215
  • Apr 12 21:54
    benlesh commented #6215
  • Apr 12 21:53
    cartant commented #6215
  • Apr 12 21:53
    benlesh commented #6215
Dorus
@Dorus
If i remember right, something along the lines of repeatWhen(ob => ob.mergeMap((e, i) => EMPTY.pipe(delay(i*n))))
Dominic Watson
@intellix
with retryWhen you at least have access to the error, so you can conditionally do something conditionally there
Dorus
@Dorus
Idk if it was EMPTY or of(e) to be used there.

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.
Dominic Watson
@intellix
ah, cause I only want to repeat conditionally based on the response. In my mind:
source().pipe(
  repeatUntil(response => !response.verified),
)
...false....false.....false.....true| (assuming it's inclusive)
Dorus
@Dorus
@intellix That's why my example above ended with first, first will terminate the observable including the repeat once you got the result you're looking for.
or you could use find
Andreas Weinzierl
@this-is-me:matrix.org
[m]
Is this the correct way to return a void Observable?
observable$.pipe(
    map( _ => {} ),
)
Miloš Lapiš
@mlc-mlapis
@this-is-me:matrix.org Don't think so, of(void 0) returns Observable<void>. Or empty()?
Miloš Lapiš
@mlc-mlapis
@this-is-me:matrix.org of() probably doesn't emit, only complete.
Dorus
@Dorus
@this-is-me:matrix.org just use EMPTY
Miloš Lapiš
@mlc-mlapis
@Dorus Do you know more why it's preferable to use EMPTY and not empty()?
Dorus
@Dorus

@mlc-mlapis The link i shared:

The same Observable instance returned by any call to empty without a scheduler. It is preferrable to use this over empty().

I'm not too sure about the technical details
Miloš Lapiš
@mlc-mlapis
@Dorus OK, it's the same place where I stopped searching. :smile:
Dorus
@Dorus
But the docs for empty() do say it 's deprecated in favor or empty(scheduler) scheduled and EMPTY
Miloš Lapiš
@mlc-mlapis
OK, so the scheduler looks to be as the reason.
Dorus
@Dorus
Anyway if you want to go deeper, this changeset added the deprecated tag: ReactiveX/rxjs@f57e1fc
Mm, do they mean to use scheduled(EMPTY, scheduler)?
I'm the kind of guys that needs examples to understand this stuff :D
Miloš Lapiš
@mlc-mlapis
export function empty(scheduler?: SchedulerLike) {
  return scheduler ? emptyScheduled(scheduler) : EMPTY;
}
From this point it's equivalent, but the principal logic says: Without a scheduler, it's a micro faster to use directly EMPTY.
Dorus
@Dorus
I believe many patterns in RxJs are inherited from Rx.Net. Like having a scheduler on every operator. However in Js this isnt necessarily and they're doing other things now like with testScheduler where you can just pass a lambda that does all work on the testscheduler. As a result they're simplifying the api's and removing the scheduler parameter.
Rx.Net has the rule that every operator should have a scheduler and you pass along. All required to make testing possible.
Miloš Lapiš
@mlc-mlapis
OK, understand.
Dorus
@Dorus
Like in rx.net, if you make a new operator that's a composition of other operators, and do not pass the scheduler, it's untestable
But this is not a problem in js
So i'm guessing it's part of that effort
Miloš Lapiš
@mlc-mlapis
:thumbsup:
Roy Touw
@roytouw7

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.

Dorus
@Dorus
@roytouw7 is a$ hot?
aka, is it a subject too? So that you can resubscribe to it at will.
Or i guess it probably is ok looking at your wording.
I would do b$.pipe(switchMap(e => concat(e, a$)))
Roy Touw
@roytouw7
@Dorus yes a$ is hot too
oh thank you! I'm gonna read about concat, that's a new one for me.
Dorus
@Dorus
I just realise it can be cold too, as long as resubscribing is the intended behavior.
Roy Touw
@roytouw7
yes that's the intended behavior. Thanks!
Roy Touw
@roytouw7

https://stackblitz.com/edit/rxjs-srwthz?file=index.ts

Made a prototype and it works like a charm, cheers. :)

Juan Bigorra
@jbigorra

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!

Dorus
@Dorus
@jbigorra Try throwError<IUser>(new NotFoundException()).
The type of the observable tells something about what's called on the next callback, not on the error callback.
Thus it's perfectly valid for 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.
2 replies
Andreas Bjørn Hassing
@AndreasHassing

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.

There are so many operators to choose from so it's been hard for me to wrap my head around this :D - any other suggestions based on previous code is welcome.