-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Tim Smart
committed
Apr 5, 2022
1 parent
06fc44c
commit 3c770bc
Showing
2 changed files
with
119 additions
and
123 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,36 @@ | ||
import * as Rx from "rxjs"; | ||
|
||
export const bufferUntil = <T>(predicate: (value: T) => boolean) => ( | ||
source$: Rx.Observable<T>, | ||
) => | ||
new Rx.Observable<T[]>((s) => { | ||
let buffer: Array<T> = []; | ||
export const bufferUntil = | ||
<T>(predicate: (value: T) => boolean) => | ||
(source$: Rx.Observable<T>) => | ||
new Rx.Observable<T[]>((s) => { | ||
let buffer: Array<T> = []; | ||
|
||
const maybeEmit = () => { | ||
if (buffer.length) { | ||
emit(); | ||
} | ||
}; | ||
const maybeEmit = () => { | ||
if (buffer.length) { | ||
emit(); | ||
} | ||
}; | ||
|
||
const emit = () => { | ||
s.next(buffer); | ||
buffer = []; | ||
}; | ||
const emit = () => { | ||
s.next(buffer); | ||
buffer = []; | ||
}; | ||
|
||
source$.subscribe( | ||
(v) => { | ||
if (predicate(v)) { | ||
source$.subscribe( | ||
(v) => { | ||
if (predicate(v)) { | ||
maybeEmit(); | ||
buffer = [v]; | ||
emit(); | ||
} else { | ||
buffer.push(v); | ||
} | ||
}, | ||
(err) => s.error(err), | ||
() => { | ||
maybeEmit(); | ||
buffer = [v]; | ||
emit(); | ||
} else { | ||
buffer.push(v); | ||
} | ||
}, | ||
(err) => s.error(err), | ||
() => { | ||
maybeEmit(); | ||
s.complete(); | ||
}, | ||
); | ||
}); | ||
s.complete(); | ||
}, | ||
); | ||
}); |