Subscription ownership and lifecycle
This lesson uses the behavioral-contracts.zip runtime comparison API.
Its policies differ deliberately from core behavioral-patterns.zip; see the
contract comparison.
What this concept is
A subscription is a resource: someone owns its cancellation. A provider holding a callback may also retain everything the callback captures. Lifecycle design determines when those references are released.
Why it matters here
A view can disappear while its callback still receives events and keeps the view alive. Repeated mounting can accumulate listeners even when each individual subscription appears to work.
Concrete example
In the lab, unsubscribe(token) returns true only when an active subscription is removed; repeating it returns false. close() removes all registrations and prevents future subscribe/publish calls. Closing from inside a callback cancels the remaining current deliveries. Unsubscribe remains safe after close, and close is repeatable.
Common confusion / misconception
An integer token is not globally unique: it belongs to one publisher. Weak references are not a substitute for a clear lifecycle; they change whether the provider keeps a subscriber alive and can make notifications disappear when ownership is unclear.
How to use it
Choose one owner and disposal point. In a UI, a component can store its subscription token and unsubscribe when disposed. Verify three mount/unmount cycles followed by one publication. Inspect release after dispatch finishes: the currently executing callback is still referenced by the call stack even after it removes itself. Avoid promising instant garbage collection or using object finalizers as the primary cleanup protocol.
Check yourself
If the same callable is registered twice, should removing one token remove both? Under this contract, why not?
Mini drill or application
Write a trace for A closing the bus while B is still pending. Expected callbacks: A only; later publish/subscribe reject, repeated close succeeds, old-token unsubscribe returns false. Explain which lifecycle rule would change for a reusable paused publisher.
Read this only if stuck
Use the existing book/reference map and runtime reading guide. The Design Patterns book index supplies pattern vocabulary; the local lab contract specifies its actual semantics.