UNPKG

4.65 kBTypeScriptView Raw
1import { Disposable, DisposableCollection } from '../disposable';
2import { Emitter, Event } from '../event';
3import { ReadBuffer, WriteBuffer } from './message-buffer';
4/**
5 * A channel is a bidirectional communications channel with lifecycle and
6 * error signalling. Note that creation of channels is specific to particular
7 * implementations and thus not part of the protocol.
8 */
9export interface Channel {
10 /**
11 * The remote side has closed the channel
12 */
13 onClose: Event<ChannelCloseEvent>;
14 /**
15 * An error has occurred while writing to or reading from the channel
16 */
17 onError: Event<unknown>;
18 /**
19 * A message has arrived and can be read by listeners using a {@link MessageProvider}.
20 */
21 onMessage: Event<MessageProvider>;
22 /**
23 * Obtain a {@link WriteBuffer} to write a message to the channel.
24 */
25 getWriteBuffer(): WriteBuffer;
26 /**
27 * Close this channel. No {@link onClose} event should be sent
28 */
29 close(): void;
30}
31/**
32 * The event that is emitted when a channel is closed from the remote side.
33 */
34export interface ChannelCloseEvent {
35 reason: string;
36 code?: number;
37}
38/**
39 * The `MessageProvider` is emitted when a channel receives a new message.
40 * Listeners can invoke the provider to obtain a new {@link ReadBuffer} for the received message.
41 * This ensures that each listener has its own isolated {@link ReadBuffer} instance.
42 *
43 */
44export declare type MessageProvider = () => ReadBuffer;
45/**
46 * Reusable abstract {@link Channel} implementation that sets up
47 * the basic channel event listeners and offers a generic close method.
48 */
49export declare abstract class AbstractChannel implements Channel {
50 onCloseEmitter: Emitter<ChannelCloseEvent>;
51 get onClose(): Event<ChannelCloseEvent>;
52 onErrorEmitter: Emitter<unknown>;
53 get onError(): Event<unknown>;
54 onMessageEmitter: Emitter<MessageProvider>;
55 get onMessage(): Event<MessageProvider>;
56 protected toDispose: DisposableCollection;
57 constructor();
58 close(): void;
59 abstract getWriteBuffer(): WriteBuffer;
60}
61/**
62 * A very basic {@link AbstractChannel} implementation which takes a function
63 * for retrieving the {@link WriteBuffer} as constructor argument.
64 */
65export declare class BasicChannel extends AbstractChannel {
66 protected writeBufferProvider: () => WriteBuffer;
67 constructor(writeBufferProvider: () => WriteBuffer);
68 getWriteBuffer(): WriteBuffer;
69}
70/**
71 * Helper class to implement the single channels on a {@link ChannelMultiplexer}. Simply forwards write requests to
72 * the given write buffer source i.e. the main channel of the {@link ChannelMultiplexer}.
73 */
74export declare class ForwardingChannel extends AbstractChannel {
75 readonly id: string;
76 protected readonly closeHandler: () => void;
77 protected readonly writeBufferSource: () => WriteBuffer;
78 constructor(id: string, closeHandler: () => void, writeBufferSource: () => WriteBuffer);
79 getWriteBuffer(): WriteBuffer;
80 close(): void;
81}
82/**
83 * The different message types used in the messaging protocol of the {@link ChannelMultiplexer}
84 */
85export declare enum MessageTypes {
86 Open = 1,
87 Close = 2,
88 AckOpen = 3,
89 Data = 4
90}
91/**
92 * The write buffers in this implementation immediately write to the underlying
93 * channel, so we rely on writers to the multiplexed channels to always commit their
94 * messages and always in one go.
95 */
96export declare class ChannelMultiplexer implements Disposable {
97 protected readonly underlyingChannel: Channel;
98 protected pendingOpen: Map<string, (channel: ForwardingChannel) => void>;
99 protected openChannels: Map<string, ForwardingChannel>;
100 protected readonly onOpenChannelEmitter: Emitter<{
101 id: string;
102 channel: Channel;
103 }>;
104 get onDidOpenChannel(): Event<{
105 id: string;
106 channel: Channel;
107 }>;
108 protected toDispose: DisposableCollection;
109 constructor(underlyingChannel: Channel);
110 protected handleError(error: unknown): void;
111 onUnderlyingChannelClose(event?: ChannelCloseEvent): void;
112 protected handleMessage(buffer: ReadBuffer): void;
113 protected handleAckOpen(id: string): void;
114 protected handleOpen(id: string): void;
115 protected handleClose(id: string): void;
116 protected handleData(id: string, data: ReadBuffer): void;
117 protected createChannel(id: string): ForwardingChannel;
118 protected prepareWriteBuffer(id: string): WriteBuffer;
119 protected closeChannel(id: string): void;
120 open(id: string): Promise<Channel>;
121 getOpenChannel(id: string): Channel | undefined;
122 dispose(): void;
123}
124//# sourceMappingURL=channel.d.ts.map
\No newline at end of file