DEV Community

Discussion on: What's the RxJs/ NgRx code bit that you are most proud of? (Round 2)

Collapse
 
johannesjo profile image
Johannes Millan • Edited

Got another one:

const isServerRunning$ = timer(0, 2000).pipe(
    switchMap(() => {
        const img = new Image();
        const obs = merge(
            fromEvent(img, 'load').pipe(mapTo(true)),
            fromEvent(img, 'error').pipe(mapTo(false)),
        ).pipe(take(1));
        img.src = `${SERVER_ADDRESS}?random-no-cache=${Math.floor((1 + Math.random()) * 0x10000).toString(16)}`;
        return obs;
    }),
    distinctUntilChanged(),
);