hook is a Tcllib package tht implements the subject/observer (publish/subscribe) pattern.
hook allows subjects, which may be modules, objects, widgets, and so forth, to synchronously call hooks which may be bound to an arbitrary number of subscribers, called observers. A subject may call any number of distinct hooks, and any number of observers can bind callbacks to a particular hook called by a particular subject. Hook bindings can be queried and deleted.
Below is an incomplete but illustrative example from Emiliano Gavilán. The Reader module (code inside a namespace which manages the connection and communication with an instrument) issues
hook call Reader <<Connecting>>
when trying to connect, and
hook call Reader <<Connected>> $conn
upon connection/disconnection, with $conn being 1 on connect and 0 on disconnect:
set connbutton [ttk::button $f.startstop \ -text Connect \ -command [fqcmd connect] \ -image ::img::connect0] hook bind Reader <<Connecting>> $connbutton [lambda {w} { $w state disabled status set-clear "Connecting to reader" } $connbutton] hook bind Reader <<Connected>> $connbutton [lambda {w conn} { $w state !disabled if {$conn} { set spec [list Disconnect [fqcmd disconnect] ::img::connect4] status set-clear "Reader connected" } else { set spec [list Connect [fqcmd connect] ::img::connect0] status set-clear "Reader disconnected" } foreach {txt cmd img} $spec { $w configure -text $txt -command $cmd -image $img } } $connbutton]