Skip to content

Instantly share code, notes, and snippets.

@javiermadueno
Forked from jayphelps/batchsampling.js
Created December 30, 2019 19:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save javiermadueno/74e4483fc6d102e26e1ffb3812eab556 to your computer and use it in GitHub Desktop.
Save javiermadueno/74e4483fc6d102e26e1ffb3812eab556 to your computer and use it in GitHub Desktop.
Batch Sampling Example from my talk, Real-time Insights, powered by Reactive Programming
let buffer = getWebSocket()
.bufferTime(1000);
let gate = new BehaviorSubject(true);
let batchSize = 50;
let batchSizeCounter = 0;
let results = gate
.switchMap(enabled => enabled ? buffer : Observable.never())
.do(buffer => {
batchSizeCounter += buffer.length;
if (batchSizeCounter >= batchSize) {
// truncates the array, if it's over batchSize
let overage = batchSizeCounter - batchSize;
buffer.length = buffer.length - overage;
// turns on the gate, pausing the stream
gate.next(false);
batchSizeCounter = 0;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment