-
-
Save jasdev/4e417aded551fafd37e8159066aa10b0 to your computer and use it in GitHub Desktop.
Unordered batch subscribe.
This file contains hidden or 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
| extension Sequence where Element: Publisher { | |
| func bufferedSubscribe(by size: Int) -> AnyPublisher<[Element.Output], Element.Failure> { | |
| publisher | |
| .buffer(size: .max, prefetch: .byRequest, whenFull: .dropOldest) // Since we’re | |
| // buffering up to `Int.max` elements _by request_ — to avoid flooding the buffer — , | |
| // `BufferingStrategy.dropOldest` or `.dropNewest` likely work equally well here. | |
| .flatMap(maxPublishers: .max(size)) { $0 } | |
| .collect(size) | |
| .eraseToAnyPublisher() | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment