Currently, I am dealing with a stream of letters that need to be arranged in the correct order to form a word. However, an issue arises when the user switches tabs, minimizes the browser, or switches applications - the behavior mimics using setTimeout(), resulting in a jumbled order and lost items. Despite attempting to utilize functions like bufferWhen()
, bufferToggle()
, takeUntil()
, publish()
, and connect()
, none of them have helped me achieve my objective. While considering using delayWhen
, it seems deprecated and not practical as it immediately halts the stream. Could you suggest which functions I should use and how? Here's the snippet of my code:
export class MyComponent implements AfterViewInit {
private visibilityChange$ = fromEvent(document, 'visibilitychange').pipe(startWith('visible'), shareReplay({ refCount: true, bufferSize: 1 }));
private show$ = this.visibilityChange$.pipe(filter(() => document.visibilityState === 'visible'));
private hide$ = this.visibilityChange$.pipe(filter(() => document.visibilityState === 'hidden'));
public ngAfterViewInit() {
const lettersStream$ = zip( // add delay for each letter
from(['w', 'o', 'r', 'd']),
interval(1000))
// pause when hide$ fires, resume when show$
.pipe(map(([letter, delayTime]) => letter))
.subscribe(console.log);
}
}
I have created a demonstration on stackblitz with the main goal being to observe (and halt writing when tab is inactive) how the phrase appears on the screen.