11.8 · Future versus Deferred, subject replay, share, and bridging to async

Where a stream starts decides when work runs and who sees what. These questions are about eager versus lazy, what a late subscriber misses, and moving between Combine and async code.

Mental model. Ask of every publisher: does it run before anyone subscribes, does a second subscriber repeat the work, and what does a late subscriber get?

Eager, lazy and replay

Future versus Deferred, how subjects replay and complete, and what share does for late subscribers.

  1. When does a Future run its work, what do two subscribers get, and what does Deferred change? 11.8.1 · L2
    A Future is eager: its closure runs when you create it, before anyone subscribes, which is why the log shows “2 Future closure runs” before “3 Future created…
  2. What happens when you send to a subject after it has completed, and what does a late subscriber to a finished subject receive? 11.8.2 · L2
    A PassthroughSubject has no memory: a subscriber receives only the values sent after it subscribed. The 0 sent before subscribing is lost (the log starts at…
  3. What does share() do for several subscribers, and what happens to a subscriber who arrives after the shared work has finished? 11.8.3 · L5
    share() makes the upstream publisher run once for all current subscribers: it subscribes upstream when the first subscriber arrives and fans the values out. In…

Bridging

Publisher values as an async sequence, and Future around async work.

  1. How do you move between Combine and async/await: the values property, Future around a Task, and what to watch for? 11.8.4 · L3
    Combine to async: every publisher with Failure == Never has a .values property that is an AsyncSequence (an AsyncPublisher; AsyncThrowingPublisher for failing…

More in Combine