-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtakeWhile.ts
More file actions
33 lines (30 loc) · 1.13 KB
/
Copy pathtakeWhile.ts
File metadata and controls
33 lines (30 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import { IntermediateStage } from '../../stages';
import { PipelineEvent } from '../../PipelineEvent';
class TakeWhileStage<IN> extends IntermediateStage<IN, IN> {
constructor(private readonly _predicate: (element: IN) => boolean) {
super();
}
override consume(element: IN, hasMoreDataUpstream: boolean): void {
const shouldKeepTaking = this._predicate(element);
if (shouldKeepTaking) {
this._downstream.consume(element, hasMoreDataUpstream);
} else {
this._broadcast(PipelineEvent.TERMINATE_PIPELINE);
this._cascadeEvent(PipelineEvent.TERMINATE_PIPELINE);
}
}
}
/**
* Return an intermediate stage that consumes elements in the pipeline as long as the provided predicate is `true`.
* As soon as the predicate returns `false`, this stage will no longer execute even if the predicate returns `true`
* for subsequent elements down the pipeline.
*
* @param predicate
*
* @template IN The type parameter of each incoming element in the pipeline.
*
* @returns
*/
export function takeWhile<IN>(predicate: (element: IN) => boolean): IntermediateStage<IN, IN> {
return new TakeWhileStage<IN>(predicate);
}