transchan(n)
π transchan(n) β the channel transform handler API π
okay bestie, welcome to transchan(n), a page from the Tcl Built-In Commands manual π¦ this is the whole tea on how channel transformations talk to you at the Tcl level β¨
πΈ NAME πΈ
transchan β command handler API of channel transforms periodt π
β¨ SYNOPSIS β¨
chan push channel cmdPrefix
cmdPrefix clear handle
cmdPrefix drain handle
cmdPrefix finalize handle
cmdPrefix flush handle
cmdPrefix initialize handle mode
cmdPrefix limit? handle
cmdPrefix read handle buffer
cmdPrefix write handle buffer
π«Ά DESCRIPTION π«Ά
So like, the Tcl-level handler for a channel transformation has to be a command with subcommands. They call it an ensemble, but bestie, that does NOT mean you have to build it with namespace ensemble create β this whole mechanism is not tied to namespace ensemble in any way, shape, or form, iykyk π
Heads up: cmdPrefix is literally whatever you passed into the chan push call, and it can be multiple arguments. Those get expanded into multiple words in place of the prefix. Multiple. Words. Slay. π
Out of all the possible subcommands, your handler must support initialize and finalize. Transformations for writable channels must also support write, and transformations for readable channels must also support read. That's the non-negotiable part fr fr π
Also note: in the descriptions below, cmdPrefix may be more than one word, and handle is the value returned by the chan push call that created the transformation. Don't lose that handle, bestie, it's your main character. π
π§ GENERIC SUBCOMMANDS π§
These ones are relevant to all types of channel, no exceptions:
cmdPrefix clear handleβ this optional subcommand is called to tell the transformation that any data stored in internal buffers (incoming or outgoing) must be cleared. It fires when achan seekhappens on the channel being transformed. bye bye buffered data β¨cmdPrefix finalize handleβ this mandatory subcommand is called last for the given handle, and then never again. It exists so you can clean up any Tcl-level data structures tied to the transformation.
β οΈ Warning! Any errors thrown by this subcommand will be ignored. And it is not guaranteed to be called if the interpreter is deleted. So likeβ¦ don't put your whole life in there π
cmdPrefix initialize handle modeβ this mandatory subcommand is called first, and then never again (for the given handle). Its whole job is to initialize all the parts of the transformation at the Tcl level.modeis a list containing any ofreadandwrite.writeβ implies that the channel is writable. βοΈreadβ implies that the channel is readable. π
The return value of the subcommand should be a list containing the names of all subcommands supported by this handler π Any error thrown by the subcommand will prevent the creation of the transformation, and that thrown error will show up as an error thrown by chan push. no pressure tho π
π READ-RELATED SUBCOMMANDS π
These are for transformations applied to readable channels. Strictly speaking read is optionalβ¦ but bestie, it must be supported if any of the others is, or the channel will be made non-readable. So like, it's optional the way breathing is optional π
cmdPrefix drain handleβ optional. Called whenever data in the transformation input (i.e. read) buffer has to be forced upward, i.e. towards the user or script. The result returned by the method is taken as the binary data to push upward to the level above this transformation β that's the reader, or a higher-level transformation.
In other words: when this method gets called, the transformation cannot defer the actual transformation operation anymore and has to transform all the data waiting in its internal read buffers and return the result of that action. No more stalling, periodt. π
cmdPrefix limit? handleβ optional. Called so the Tcl I/O engine can figure out how far ahead it should read. If it's present, it should return an integer greater than zero to indicate how many bytes ahead should be read, or an integer less than zero to indicate that the I/O engine may read as far ahead as it likes. zero? don't ask, it's not in the spec β¨cmdPrefix read handle bufferβ this subcommand, which must be present if the transformation is to work with readable channels, is called whenever the base channel β or a transformation below this transformation β pushes data upward.buffercontains the binary data that's been given to us from below. It is this subcommand's responsibility to actually transform the data. The result returned by the subcommand is taken as the binary data to push further upward, to the transformation above this transformation. This can also be the user or script that originally read from the channel. π
Note that the result is allowed to be empty, or even less than the data we received β the transformation is not required to transform everything given to it right now. It's allowed to store incoming data in internal buffers and defer the actual transformation until it has more data. stockpiling arc π
π¦ WRITE-RELATED SUBCOMMANDS π¦
These are for transformations applied to writable channels. Strictly speaking write is optional, but it must be supported if any of the others is, or the channel will be made non-writable. Same deal as before, bestie π
cmdPrefix flush handleβ optional. Called whenever data in the transformation'write'buffer has to be forced downward, i.e. towards the base channel. The result returned by the subcommand is taken as the binary data to write to the transformation below the current transformation. That can be the base channel too. π
In other words: when this subcommand is called, the transformation cannot defer the actual transformation operation anymore and has to transform all the data waiting in its internal write buffers and return the result of that action. Flush it, don't rush itβ¦ actually no, flush it now. π
cmdPrefix write handle bufferβ this subcommand, which must be present if the transformation is to work with writable channels, is called whenever the user β or a transformation above this transformation β writes data downward.buffercontains the binary data that's been written to us. It is this subcommand's responsibility to actually transform the data. βοΈ
The result returned by the subcommand is taken as the binary data to write to the transformation below this transformation. That can be the base channel too. Note that the result is allowed to be empty, or less than the data we got β the transformation is not required to transform everything which was written to it right now. It's allowed to store this data in internal buffers and defer the actual transformation until it has more data. buffer now, transform later, iconic behavior honestly π
π SEE ALSO π
chan(n), refchan(n) go read those next bestie, they're the prequel π«Ά
π₯ KEYWORDS π₯
API, channel, ensemble, prefix, transformation
and that's the whole page, babe π this one shipped with Tcl version 8.6 and it lives under transchan(n) πΈ go forth and transform some channels, you absolute icon πβ¨
π¦ where this page comes from
- fedora 43: tcl-doc 1:9.0.2-1.fc43 Β· TCL AND GPL-3.0-or-later WITH Bison-exception-2.2 AND BSD-3-Clause
- fedora 44: tcl-doc 1:9.0.2-1.fc44 Β· TCL AND GPL-3.0-or-later WITH Bison-exception-2.2 AND BSD-3-Clause
- Copyright (c) 2008 Donal K. Fellows
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms