RxJS - Operatore di trasformazione bufferWhen

Questo operatore fornirà i valori in forma di matrice, accetta un argomento come funzione che deciderà quando chiudere, emettere e resettare il buffer.

Sintassi

bufferWhen(closing_func: Observable): Observable

Parametri

closing_func - Una funzione che restituisce un Observable che indica la chiusura del buffer.

Valore di ritorno

Verrà restituito un osservabile, che avrà un array di valori bufferizzati.

Esempio

Ecco un esempio funzionante di bufferWhen:

import { fromEvent, interval} from 'rxjs';
import { bufferWhen } from 'rxjs/operators';

let btn = document.getElementById("btnclick");
let btn_clicks = fromEvent(btn, 'click');
let buffered_array = btn_clicks.pipe(bufferWhen(() => interval(5000)));
buffered_array.subscribe(arr => console.log(arr));

Per bufferWhenabbiamo dato una funzione che viene eseguita ad un intervallo di 5 secondi. Quindi, dopo ogni 5 secondi, emetterà tutti i clic registrati e verrà ripristinato e riavviato.

Produzione