throttleTime does not throttle when the pipe emits a second time synchronously - is there a better way of coding this? #7482
Unanswered
delbertooo
asked this question in
Q&A
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I wanted to use throttle time to reload something every once in a while. By doing so, I found that if the pipe includes something after
throttleTime
that also emits into it's source synchronously, thenthrottleTime
isn't throttling (yet).Example
For example, I've got a Worker: (see StackBlitz for the full code)
And now I'll use rxjs to reload (call)
Worker#bar
on each worker activity, but not more than once every second:Now, when I'm using the worker, the throttling works fine but "reloading bar..." is logged twice on each reload and side-effects (calling
bar()
) are called twice:The problem seems to be, that the second emit of
activity$
is triggered synchronously and maybe before the throttling is set-up (?). The only way I found to work around this was to force the subsequent activity to be asynchronous by using a delay:(Another way would be using
trailing: true
, but this isn't a great behaviour for my reloading logic because of the delay.)My main question
Is this a bug? Is it just an side-effect because there is a better way of coding this in a reactive way? Then which way is it? Are there other ways of working around this issue?
Beta Was this translation helpful? Give feedback.
All reactions