Would a bufferWhile
operator have its place in the operator's list?
#7300
LcsGa
started this conversation in
Ideas / Feature request
Replies: 2 comments 2 replies
-
I improved it (I guess) by adding the same export function bufferWhile<T, R extends T>(predicate: (value: T, index: number) => value is R): OperatorFunction<T, R[]>;
export function bufferWhile<T, R extends T>(predicate: (value: T, index: number) => value is R, inclusive: boolean): OperatorFunction<T, R[]>;
export function bufferWhile<T>(predicate: (value: T, index: number) => boolean): OperatorFunction<T, T[]>;
export function bufferWhile<T>(predicate: (value: T, index: number) => boolean, inclusive: boolean): OperatorFunction<T, T[]>;
export function bufferWhile<T>(predicate: (value: T, index: number) => boolean, inclusive = false): OperatorFunction<T, T[]> {
return (source$: Observable<T>) =>
new Observable((destination) => {
let buffer: T[] = [];
let index = 0;
const emitBuffer = (firstValue?: T) => {
destination.next(buffer);
buffer = firstValue ? [firstValue] : [];
};
return source$.subscribe({
next: (value) => {
if (predicate(value, index++)) {
buffer.push(value);
} else {
if (inclusive) {
buffer.push(value);
emitBuffer();
} else {
emitBuffer(value);
}
}
},
error: (err) => destination.error(err),
complete: () => {
emitBuffer();
destination.complete();
},
});
});
} |
Beta Was this translation helpful? Give feedback.
0 replies
-
any thought on this? @benlesh |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I had a use case today where I wanted to buffer some values based on a predicate!
Since I couldn't find an operator (maybe I missed it?) that already handled that I made one:
Example:
=> Does it have its place within the operators list?
BTW, did I miss some possible use cases with potential errors? I made this operator quickly and couldn't experiment more about it 😬
Beta Was this translation helpful? Give feedback.
All reactions