IOutputChannel Interface#
A two-way message channel between one cell output and the view that renders it, held for as long as the view is mounted rather than for the length of a cell execution.
public interface IOutputChannel : System.IAsyncDisposable
Implements System.IAsyncDisposable
Remarks#
Deliberately shaped like ILayoutFrameChannel, which solves the same problem one layer up: a component that has to keep talking to something drawn on a page it cannot reach directly. The difference is lifetime. A layout frame channel belongs to one mount of one renderer, and this one belongs to one output for as long as the session holds it, so it is still usable when no cell is running, which is exactly when someone is interacting with what the last cell drew.
Properties#
CellId Property#
The cell whose output this channel belongs to.
System.Guid CellId { get; }
Property Value#
ChannelId Property#
Host-allocated, opaque token identifying this channel. Handed only to the view the channel belongs to, so a message naming a channel is a claim the host checks rather than trusts.
string ChannelId { get; }
Property Value#
IsAlive Property#
Whether the channel is still open. Turns false before anything else observes the
close, so a handler running at close time already sees it. Once it is false,
PostMessageAsync(string, object, CancellationToken) throws rather than dropping the message, which is what a
caller posting from a callback able to outlive the view needs: a variable-changed handler
racing the close is the case this is written for.
bool IsAlive { get; }
Property Value#
SnapshotProvider Property#
Asked for a current document for the output this channel belongs to, when something is about to write the notebook out and wants the file to hold what the view is showing rather than what the cell drew. Set by the channel's owner, and left null by an owner with nothing better to offer than what is already written, in which case nothing asks.
System.Func<System.Threading.CancellationToken,System.Threading.Tasks.Task<string?>>? SnapshotProvider { get; set; }
Property Value#
System.Func<System.Threading.CancellationToken,System.Threading.Tasks.Task<System.String>>
Remarks#
A provider returning null means it could not produce one and the existing content stands. The bound is the caller's rather than the provider's, and a provider that has not answered within it is abandoned rather than waited on, so a save is never held up by one.
Methods#
PostMessageAsync(string, object, CancellationToken) Method#
Posts a typed message to the view. The host prefixes the supplied type
with "ext/" before delivery, and strips the same prefix from what comes back, so a
round trip carries the type the caller supplied.
System.Threading.Tasks.Task PostMessageAsync(string type, object? payload, System.Threading.CancellationToken ct=default(System.Threading.CancellationToken));
Parameters#
type System.String
payload System.Object
ct System.Threading.CancellationToken
Returns#
Exceptions#
System.ArgumentException
type is null or empty, or is in the reserved verso/ namespace,
which is held for framework messages (the host adds the ext/ prefix automatically,
so callers never supply it).
System.InvalidOperationException
The channel is closed (IsAlive is false). A host closes a channel when
its view goes away, and also when a caller posts more than the host will hold for a view
that has not mounted.
Remarks#
A dead channel or an invalid type is never silently dropped: both throw synchronously, before the returned task is awaited. Guard with IsAlive when posting from a callback that can run after the view has gone.
Posting before the view has mounted is ordinary and is what happens every time, because an output's first message is produced while the client is still rendering it. Those messages are queued in order and delivered when the view reports itself ready.
Events#
MessageReceived Event#
Raised for each message the view sends back. Handlers are awaited in turn, and one that throws is reported and does not stop the handlers after it or the messages after this one.
event Func<OutputChannelMessage,Task>? MessageReceived;
Event Type#
System.Func<OutputChannelMessage,System.Threading.Tasks.Task>