1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
|
[comment {-*- tcl -*- doctools manpage}]
[vset VERSION 0.3]
[manpage_begin hook n [vset VERSION]]
[see_also uevent(n)]
[keywords callback]
[keywords event]
[keywords hook]
[keywords observer]
[keywords producer]
[keywords publisher]
[keywords subject]
[keywords subscriber]
[keywords uevent]
[copyright {2010, by William H. Duquette}]
[moddesc {Hooks}]
[titledesc {Hooks}]
[category {Programming tools}]
[require Tcl "8.5 9"]
[require hook [opt [vset VERSION]]]
[description]
[para]
This package provides the [cmd hook] ensemble command, which
implements the Subject/Observer pattern. It allows [term subjects],
which may be [term modules], [term objects], [term widgets], and so
forth, to synchronously call [term hooks] which may be bound to an
arbitrary number of subscribers, called [term 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.
[para]
This man page is intended to be a reference only.
[section Concepts]
[subsection Introduction]
Tcl modules usually send notifications to other modules in two ways:
via Tk events, and via callback options like the text widget's
[option -yscrollcommand] option. Tk events are available only in Tk,
and callback options require tight coupling between the modules
sending and receiving the notification.
[para]
Loose coupling between sender and receiver is often desirable,
however. In Model/View/Controller terms, a View can send a command
(stemming from user input) to the Controller, which updates the
Model. The Model can then call a hook [emph {to which all relevant
Views subscribe.}] The Model is decoupled from the Views, and indeed
need not know whether any Views actually exist.
At present, Tcl/Tk has no standard mechanism for implementing loose
coupling of this kind. This package defines a new command, [cmd hook],
which implements just such a mechanism.
[subsection Bindings]
The [cmd hook] command manages a collection of hook bindings. A hook
binding has four elements:
[list_begin enumerated]
[enum]
A [term subject]: the name of the entity that will be calling the
hook.
[enum]
The [term hook] itself. A hook usually reflects some occurrence in the
life of the [term subject] that other entities might care to know
about. A [term hook] has a name, and may also have arguments. Hook
names are arbitrary strings. Each [term subject] must document the
names and arguments of the hooks it can call.
[enum]
The name of the [term observer] that wishes to receive the [term hook]
from the [term subject].
[enum]
A command prefix to which the [term hook] arguments will be appended
when the binding is executed.
[list_end]
[subsection {Subjects and observers}]
For convenience, this document collectively refers to subjects and
observers as [term objects], while placing no requirements on how
these [term objects] are actually implemented. An object can be a
[package TclOO] or [package Snit] or [package XOTcl] object, a Tcl
command, a namespace, a module, a pseudo-object managed by some other
object (as tags are managed by the Tk text widget) or simply a
well-known name.
[para]
Subject and observer names are arbitrary strings; however, as
[cmd hook] might be used at the package level, it's necessary to have
conventions that avoid name collisions between packages written by
different people.
[para]
Therefore, any subject or observer name used in core or package level
code should look like a Tcl command name, and should be defined in a
namespace owned by the package. Consider, for example, an ensemble
command [cmd ::foo] that creates a set of pseudo-objects and uses
[package hook] to send notifications. The pseudo-objects have names
that are not commands and exist in their own namespace, rather like
file handles do. To avoid name collisions with subjects defined by
other packages, users of [package hook], these [cmd ::foo] handles
should have names like [const ::foo::1], [const ::foo::2], and so on.
[para]
Because object names are arbitrary strings, application code can use
whatever additional conventions are dictated by the needs of the
application.
[section Reference]
Hook provides the following commands:
[list_begin definitions]
[call [cmd hook] [method bind] [opt [arg subject]] [opt [arg hook]] [opt [arg observer]] [opt [arg cmdPrefix]]]
This subcommand is used to create, update, delete, and query hook
bindings.
[para] Called with no arguments it returns a list of the subjects with
hooks to which observers are currently bound.
[para] Called with one argument, a [arg subject], it returns a list of
the subject's hooks to which observers are currently bound.
[para] Called with two arguments, a [arg subject] and a [arg hook], it
returns a list of the observers which are currently bound to this
[arg subject] and [arg hook].
[para] Called with three arguments, a [arg subject], a [arg hook], and
an [arg observer], it returns the binding proper, the command prefix
to be called when the hook is called, or the empty string if there is
no such binding.
[para] Called with four arguments, it creates, updates, or deletes a
binding. If [arg cmdPrefix] is the empty string, it deletes any
existing binding for the [arg subject], [arg hook], and
[arg observer]; nothing is returned. Otherwise, [arg cmdPrefix] must
be a command prefix taking as many additional arguments as are
documented for the [arg subject] and [arg hook]. The binding is added
or updated, and the observer is returned.
[para] If the [arg observer] is the empty string, "", it will create a
new binding using an automatically generated observer name of the form
[const ::hook::ob]<[var number]>. The automatically generated name
will be returned, and can be used to query, update, and delete the
binding as usual. If automated observer names are always used, the
observer name effectively becomes a unique binding ID.
[para] It is possible to call [cmd {hook bind}] to create or delete a
binding to a [arg subject] and [arg hook] while in an observer binding
for that same [arg subject] and [arg hook]. The following rules
determine what happens when
[example {
hook bind $s $h $o $binding
}]
is called during the execution of
[example {
hook call $s $h
}]
[list_begin enumerated]
[enum]
No binding is ever called after it is deleted.
[enum]
When a binding is called, the most recently given command prefix is
always used.
[enum]
The set of observers whose bindings are to be called is determined
when this method begins to execute, and does not change thereafter,
except that deleted bindings are not called.
[list_end]
In particular:
[list_begin enumerated]
[enum]
If [var \$o]s binding to [var \$s] and [var \$h] is deleted, and
[var \$o]s binding has not yet been called during this execution of
[example {
hook call $s $h
}]
it will not be called. (Note that it might already have been called;
and in all likelihood, it is probably deleting itself.)
[enum]
If [var \$o] changes the command prefix that's bound to [var \$s] and
[var \$h], and if [var \$o]s binding has not yet been called during
this execution of
[example {
hook call $s $h
}]
the new binding will be called when the time comes. (But again, it is
probably [var \$o]s binding that is is making the change.)
[enum]
If a new observer is bound to [var \$s] and [var \$h], its binding will
not be called until the next invocation of
[example {
hook call $s $h
}]
[list_end]
[call [cmd hook] [method call] [arg subject] [arg hook] [opt [arg args]...]]
This command is called when the named [arg subject] wishes to call the
named [arg hook]. All relevant bindings are called with the specified
arguments in the global namespace. Note that the bindings are called
synchronously, before the command returns; this allows the [arg args]
to include references to entities that will be cleaned up as soon as
the hook has been called.
[para]
The order in which the bindings are called is not guaranteed. If
sequence among observers must be preserved, define one observer and
have its bindings call the other callbacks directly in the proper
sequence.
[para]
Because the [cmd hook] mechanism is intended to support loose
coupling, it is presumed that the [arg subject] has no knowledge of
the observers, nor any expectation regarding return values. This has a
number of implications:
[list_begin enumerated]
[enum]
[cmd {hook call}] returns the empty string.
[enum]
Normal return values from observer bindings are ignored.
[enum]
Errors and other exceptional returns propagate normally by
default. This will rarely be what is wanted, because the subjects
usually have no knowledge of the observers and will therefore have no
particular competence at handling their errors. That makes it an
application issue, and so applications will usually want to define an
[option -errorcommand].
[list_end]
If the [option -errorcommand] configuration option has a non-empty
value, its value will be invoked for all errors and other exceptional
returns in observer bindings. See [cmd {hook configure}], below, for
more information on configuration options.
[call [cmd hook] [method forget] [arg object]]
This command deletes any existing bindings in which the named
[arg object] appears as either the [term subject] or the
[term observer].
Bindings deleted by this method will never be called again. In
particular,
[list_begin enumerated]
[enum]
If an observer is forgotten during a call to [cmd {hook call}], any
uncalled binding it might have had to the relevant subject and hook
will [emph not] be called subsequently.
[enum]
If a subject [var \$s] is forgotten during a call to
[example {hook call $s $h}]
then [cmd {hook call}] will return as soon as the current binding
returns. No further bindings will be called.
[list_end]
[call [cmd hook] [method cget] [arg option]]
This command returns the value of one of the [cmd hook] command's
configuration options.
[call [cmd hook] [method configure] [option option] [arg value] ...]
This command sets the value of one or more of the [cmd hook] command's
configuration options:
[list_begin options]
[opt_def -errorcommand [arg cmdPrefix]]
If the value of this option is the empty string, "", then errors
and other exception returns in binding scripts are propagated
normally. Otherwise, it must be a command prefix taking three
additional arguments:
[list_begin enumerated]
[enum] a 4-element list {subject hook arglist observer},
[enum] the result string, and
[enum] the return options dictionary.
[list_end]
Given this information, the [option -errorcommand] can choose to log
the error, call [cmd {interp bgerror}], delete the errant binding
(thus preventing the error from arising a second time) and so forth.
[opt_def -tracecommand [arg cmdPrefix]]
The option's value should be a command prefix taking four
arguments:
[list_begin enumerated]
[enum] a [term subject],
[enum] a [term hook],
[enum] a list of the hook's argument values, and
[enum] a list of [term objects] the hook was called for.
[list_end]
The command will be called for each hook that is called. This allows
the application to trace hook execution for debugging purposes.
[list_end]
[list_end]
[section Example]
The [cmd ::model] module calls the <Update> hook in response to
commands that change the model's data:
[example {
hook call ::model <Update>
}]
The [widget .view] megawidget displays the model state, and needs to
know about model updates. Consequently, it subscribes to the ::model's
<Update> hook.
[example {
hook bind ::model <Update> .view [list .view ModelUpdate]
}]
When the [cmd ::model] calls the hook, the [widget .view]s
ModelUpdate subcommand will be called.
[para]
Later the [widget .view] megawidget is destroyed. In its destructor,
it tells the [term hook] that it no longer exists:
[example {
hook forget .view
}]
All bindings involving [widget .view] are deleted.
[section Credits]
Hook has been designed and implemented by William H. Duquette.
[vset CATEGORY hook]
[include ../common-text/feedback.inc]
[manpage_end]
|