class EventHandler::Subscription

Overview

A single event subscription that remembers how to cancel itself.

The target is captured at subscribe time inside the cancel closure, so #off always removes from the exact object it added to, regardless of where the owner later points. #off is idempotent, so two teardown paths can both call it without double-freeing; #on cancels any previous handler first, so a slot re-armed repeatedly can't leak.

target is any EventHandler includer (duck-typed: anything with the generated on(type, ...)/off(type, wrapper) pair).

Defined in:

subscription.cr

Constructors

Instance Method Summary

Constructor Detail

def self.new(cancel : Proc(Nil), wrapper : Nil | EventHandler::Wrapper(EventHandler::Event -> _) = nil) #

A subscription over an already-installed handler: cancel removes it. This is the form the generated EventHandler#on/#once return, so every subscription — whether built here or handed out by #on — tears down through the same idempotent #off.


[View source]
def self.new #

An empty (inactive) subscription, armed later via #on.


[View source]

Instance Method Detail

def active? : Bool #

Whether a handler is currently installed.


[View source]
def dispose : Nil #

#dispose/#disposed? aliases of #off/!active?, for callers whose teardown vocabulary is #dispose (e.g. reactive stacks). #off stays the canonical spelling (it mirrors #on/#off).


[View source]
def disposed? : Bool #

:ditto: — whether this subscription has been torn down (no handler live).


[View source]
def handler_hash : UInt64 #

The registered handler's identity hash (Wrapper#handler_hash), for the off(type, hash) bulk-removal form. Raises ArgumentError when this subscription holds no wrapper (see #wrapper).


[View source]
def off : Nil #

Removes the handler if one is installed. Idempotent.


[View source]
def on(target, type : T.class, once = false, async = ::EventHandler.async?, at = ::EventHandler.at_end, &block : T -> Nil) : self forall T #

Subscribes block to event type on target, first cancelling any handler this slot already holds. Returns self.


[View source]
def wrapper : EventHandler::Wrapper(Proc(EventHandler::Event, Nil)) | Nil #

The Wrapper behind this subscription, when it was returned by the generated #on/once (in the erased Wrapper(Proc(Event, Nil)) form those hand to AddHandlerEvent listeners). nil for a slot built empty and armed via #on, which wraps a nested subscription instead.


[View source]