ojhawkins
ojhawkins

Reputation: 3278

RxJS - Emit value after ever n received

Does there exist an operator that enables emissions to be throttled by count?

I essentially want to repeat the Skip call. In the example below I would like to Skip 5, emit a value and repeat.

export default function errorHandler(action$){
  action$.ofType(types.ERROR)
  /* After every n emissions received, emit once */
  .map(someAction)
}

Upvotes: 2

Views: 188

Answers (1)

cartant
cartant

Reputation: 58400

You could use bufferCount, which will emit once it has buffered the specified number of actions.

With RxJS's terminology, throttling would involve the first of the buffered actions being emitted:

export default function errorHandler(action$){
  action$.ofType(types.ERROR)
    .bufferCount(5)
    .map((actions) => actions[0]);
}

Emitting the last buffered action instead would be referred to as debouncing, in RxJS's terminology:

export default function errorHandler(action$){
  action$.ofType(types.ERROR)
    .bufferCount(5)
    .map((actions) => actions[actions.length - 1]);
}

Upvotes: 4

Related Questions