Version 3 of hook

Updated 2022-04-08 00:01:05 by pooryorick

hook is a Tcllib package tht implements the subject/observer (publish/subscribe) pattern.

What the Critics Say

hook has become an essential part of my toolbox. I can't imagine developing a medium size app without having such functionality
Emiliano Gavilán, Tcl Chatroom, 2022-04-08

Description

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.

An incomplete but illustratie example from Emiliano Gavilán:

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]