ojhawkins ojhawkins - 3 years ago 117
Javascript Question

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)
}

Answer Source

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]);
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download