This is Understanding Combine, written by Matt Neuburg. Corrections and suggestions are greatly appreciated (you can comment here). So are donations; please consider keeping me going by funding this work at http://www.paypal.me/mattneub. Or buy my books: the current (and final) editions are iOS 15 Programming Fundamentals with Swift and Programming iOS 14. Thank you!
.prefix(untilOutputFrom:)
(Publishers.PrefixUntilOutput) is not precisely a joiner; but it does involve input from two publishers, like a joiner, so it is more like a joiner than its .prefix
cousins, which are clearly partitioners. Therefore I’m mentioning it here.
What .prefix(untilOutputFrom:)
does is to allow all values from upstream pass through, until some other publisher emits a value. That value is ignored, and the subscription to that other publisher is dropped — and any further values from upstream are now prevented from flowing down the pipeline.
Basically, this is a kind of switch (or gate), where a secondary publisher has the power to throw the switch (or close the gate) and prevent values from flowing.
The output type of the second publisher doesn’t matter; its value is a dummy signal and is disregarded.
The closing of the gate works by default in the simplest possible way: when a value is received from the second publisher, the upstream publisher is cancelled. However, if the second publisher sends a completion rather than a value, regardless of whether that completion is .finished
or .failure
, the upstream publisher continues to publish (but its output is blocked by the .prefix(untilOutputFrom:)
operator).
The complementary operator is .drop(untilOutputFrom)
.