class

EventHandler::Subscriptions

Inherits Reference < Object

A bag of Subscriptions that are torn down together. #on adds a tracked subscription and returns it, so a single one can still be re-armed or cancelled individually; #add tracks a subscription made elsewhere, or a plain teardown block; #off cancels every remaining one, idempotently.

Subscriptions that are no longer live (Subscription#live?: cancelled individually, or a once handler that fired) are dropped from the bag as it grows, so a long-lived bag does not accumulate them, nor keep their handlers' closures alive.

Like the standard collections, neither class is synchronized: share one between fibers running in parallel only under your own lock. (The emitter's on/off/emit are safe to call in parallel.)

Instance methods

add

Tracks a teardown that is not an event subscription — a listener registered with a config option, a timer, a file watcher — so the bag's #off cancels it with the rest. cancel runs once, at the bag's #off or at the returned Subscription's own, whichever comes first. Returns the created Subscription.

handle = Config.on_change("app.theme") { apply_theme }
subs.add { Config.off_change "app.theme", handle }
Source
add(subscription : Subscription) : Subscription

Tracks an existing subscription, such as one EventHandler#on returned, for the bag's bulk #off. Returns it.

Source
dispose

dispose/disposed? aliases of #off/#empty?. See Subscription#dispose.

Source
disposed?

:ditto: — whether the bag has been torn down (holds no subscriptions).

Source
empty?

Whether the bag holds no subscriptions. Dead ones not yet pruned count.

Source
off

Cancels every tracked subscription and empties the bag. Idempotent.

Every subscription is cancelled even if one raises: the bag is emptied first, each remaining one is still torn down, and the first exception is re-raised at the end.

Source
on(target, type : T.class, once = false, async = ::EventHandler.async?, at = ::EventHandler.at_end, &block : T -> Nil) : Subscription forall T

Subscribes block to type on target, tracking it for a later bulk #off. Returns the created Subscription.

Source