RXJS Timer continues to run after takeWhile operator returns 'false'

vcetinick

I am trying to setup a simple queue, then poll until complete style mechanism using rxjs Observables and have been struggling to figure out what I'm doing wrong.

I have the following code

siteService.queue("123")  //(1) Queues a processing task
        .pipe(
            switchMap((res) => {
                console.log("Polling for changes...");
                return timer(100, 2000)  //(2)
                    .pipe(
                        flatMap(() => {
                            console.log("Fetching Site...");
                            return siteService.get("123);  //(3)
                        }),
                        takeWhile((res: SiteResponse) => {
                             let r = res.site.site_generation_status !== 'Current';
                            console.log("Take While", r); //(4)
                            return r;
                        })
                    );
            })
        )
        .subscribe((res) => {
            console.log("Result", res);
        }, (err) => {
            console.error("Error", err);
        }, () => {
            console.log("Done")
        });

This code should queue some processing request (1) which will then set the site_generation_status of the site object to 'Queued' and will be fulfilled by some backend process which eventually updates the status to 'Current'. The idea is that the timer (2) should initially fetch the result after 100ms and then run every 2000 ms until the generation_status is current.

This code mostly works, however, the call at (3) continues to be executed after the takeWhile (4) evaluates to false

Here is some console output

Polling for changes...
Fetching Site...
Take While true
Result {site: {…}}
Fetching Site...
Take While true
Result {site: {…}}
Fetching Site...
Take While true
Result {site: {…}}
Fetching Site...
Take While false
Done
Fetching Site...
Fetching Site...
Fetching Site...
Fetching Site...

Does anyone have any ideas as to what may be causing the command at (3) to continue to be executed after the takeWhile evaluates to true and the subscription ends (indicated by 'Done' in the log output)

vcetinick

Arghh.. Gotta love figuring out the problem after 5 minutes of posting the question..

The issue was that I had imported the wrong timer which was not violently wrong enough to cause obvious issues with the code execution (perhaps a lesson for the next person).

The issue was I imported thanks to my IDE

import { timer } from "rxjs/internal/observable/timer";

but the correct import is

import { timer } from 'rxjs';

and magically now my code works as expected

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Socket isClosed() returns false after client disconnected

EntityManager.contains() returns false after persist()

RxJS takeWhile but include the last value

Rx: a zip-like operator that continues after one of the streams ended?

Rxjs: Observable with takeUntil(timer) keeps emitting after the timer has ticked

My web application continues to run after stopping the debugger.

Micro Switch with pyserial RS232 starts/stops a timer in a tkinter thread but continues to run even when stopped

rxjs operator that adds logging before and after a callback

Looking for RxJs Operator that returns early, skipping operators below, not filter() or skip()

In operator returns false

Object array returns undefined after filter is run on it

Why rxjs ajax operator returns an Observable?

How to use rxjs buffer with takeWhile

setInterval continues to run after clearInterval is called

React Checkbox always returns false after handleFormSubmit

Why code continues to run even after 'return res.send();'

onclick still running even after it returns false

Two piped programs, one continues to run after another is killed

Excel continues to run after exiting (process still in task manager)

Windows Timer Queue Timer continues after .NET breakpoint

Batch file continues to run command after Task Scheduler stops it

cython code continues after prange returns value

Timer continues after specified amount of time

Select timer continues running after the process has been stopped with SIGSTOP?

RXJS in Angular 6 returns only { "_isScalar": false }

Function continues after notifyAll(), but Thread terminates after function returns

How to make a timer that returns the amount of time passed after mouse is released

Logical OR Operator Returns False

After recursive function returned true/false java continues to "return false" line that I put just to eliminite error

TOP Ranking

HotTag

Archive