JavaScript RxJS - which operator can combine history of last N events? rxjs which operator to use,rxjs which operator do i

I'm looking for an operator which transform the stream to the stream with history or last N events.

So for a stream e.g. like this one:

clicks$.subscribe((ev) => ...)

I would like to have something like:

clicks$.pipe(
   historyOf(2)
).subscribe(([ev, ev-1, ev-2]) => ...)

Does the operator like this exists, or is there an easy way to combine existing operators to achieve the desired effect?

Answer:1

You can use ideally bufferCount or depending how you want to handle the beginning or termination of the sequence when there're less than N events also scan or filter:

const N = 3;

of(1, 2, 3, 4, 5, 6, 7).pipe(
  bufferCount(N, 1), // `1` means it'll emit after every emission from source
).subscribe(console.log);

... or if you want to skip emissions when the chain completes:

of(1, 2, 3, 4, 5, 6, 7).pipe(
  bufferCount(N, 1),
  filter(arr => arr.length === N),
).subscribe(console.log);

... or if you want the beginning events as well:

of(1, 2, 3, 4, 5, 6, 7).pipe(
  scan((acc, item) => [...acc, item].slice(-N), []),
).subscribe(console.log);

Live demo: https://stackblitz.com/edit/rxjs-xtnscq

Answer:2

I have one static component and many for loop rendered components in a screen. The all code below here. import React from "react"; import { View, Text, FlatList } from "react-native"; ... class ...

I have one static component and many for loop rendered components in a screen. The all code below here. import React from "react"; import { View, Text, FlatList } from "react-native"; ... class ...

  1. flatlist scroll to top
  2. flatlist scroll to index
  3. flatlist scroll to bottom
  4. flatlist scroll to end
  5. flatlist scroll to
  6. flatlist scroll to item
  7. flatlist scroll to top react native
  8. flatlist scroll to bottom react native
  9. flatlist scroll to index example
  10. flatlist scroll to index react native
  11. flatlist scroll to end react native
  12. flatlist scroll to offset
  13. flatlist scroll to position
  14. flatlist scroll to index not working
  15. flatlist scroll to start
  16. flatlist scroll to element
  17. flatlist on scroll
  18. flatlist in scrollview

I have a basic script which shows/hides a div. I'm using this for a drop-down menu. https://www.w3schools.com/howto/howto_js_toggle_hide_show.asp I'm looking for the div element to be hidden when ...

I have a basic script which shows/hides a div. I'm using this for a drop-down menu. https://www.w3schools.com/howto/howto_js_toggle_hide_show.asp I'm looking for the div element to be hidden when ...

I'm using this library here: ng-sortable And I'm using it on a table. The thing is, when I physically drag a table row, the row shrinks and doesn't keep the original look of the table. I narrowed ...

I'm using this library here: ng-sortable And I'm using it on a table. The thing is, when I physically drag a table row, the row shrinks and doesn't keep the original look of the table. I narrowed ...

I have the following array var x = [{"id":"757382348857","title":"title","handle":"linkhere","productimage":"url","ippid":true,"location_x":26,"location_y":18}] And i am trying to add the following ...

I have the following array var x = [{"id":"757382348857","title":"title","handle":"linkhere","productimage":"url","ippid":true,"location_x":26,"location_y":18}] And i am trying to add the following ...

  1. concatenate arrays with different dimensions python
  2. concatenate arrays with different dimensions matlab
  3. concatenate arrays with numpy
  4. concatenate arrays with python
  5. concatenate arrays with
  6. concat arrays with spread operator
  7. concat arrays with reduce
  8. concatenate arrays in matlab
  9. concatenate arrays of different sizes matlab
  10. concatenate arrays of different sizes python
  11. concatenate arrays in java
  12. concatenate arrays in javascript
  13. concatenate arrays in c
  14. concatenate arrays in r
  15. concatenate arrays in list python
  16. concatenate arrays in excel
  17. concatenate arrays in loop matlab
  18. concatenate arrays of different sizes numpy
  19. concatenate arrays in php
  20. concatenate arrays in a list