|
| 1 | +--- |
| 2 | +id: Channel |
| 3 | +sidebar_label: Channel |
| 4 | +title: Channel |
| 5 | +hide_title: true |
| 6 | +--- |
| 7 | +# `Channel` |
| 8 | + |
| 9 | +```ts |
| 10 | +class Channel { |
| 11 | + |
| 12 | + // properties |
| 13 | + id: string; |
| 14 | + type: string; |
| 15 | + displayMetadata?: DisplayMetadata; |
| 16 | + |
| 17 | + // methods |
| 18 | + broadcast(context: Context): Promise<void>; |
| 19 | + getCurrentContext(contextType?: string): Promise<Context|null>; |
| 20 | + addContextListener(handler: ContextHandler): Listener; |
| 21 | + addContextListener(contextType: string, handler: ContextHandler): Listener; |
| 22 | +} |
| 23 | +``` |
| 24 | + |
| 25 | +Represents a context channel that applications can join to share context data. |
| 26 | + |
| 27 | +A channel can be either a well-known "system" channel (retrieved with [`getSystemChannels`](DesktopAgent#getsystemchannels)) or a custom "app" channel (obtained through [`getOrCreateChannel`](DesktopAgent#getorcreatechannel)). |
| 28 | + |
| 29 | +Channels each have a unique identifier, some display metadata and operations for broadcasting context to other applications, or receiving context from other applications. |
| 30 | + |
| 31 | +#### See also |
| 32 | + |
| 33 | +* [`Context`](Context) |
| 34 | +* [`DesktopAgent.getSystemChannels`](DesktopAgent#getsystemchannels) |
| 35 | +* [`DesktopAgent.getOrCreateChannel`](DesktopAgent#getorcreatechannel) |
| 36 | +* [`DesktopAgent.joinChannel`](DesktopAgent#joinchannel) |
| 37 | + |
| 38 | +## Properties |
| 39 | + |
| 40 | +### `id` |
| 41 | + |
| 42 | +```ts |
| 43 | +public readonly id: string; |
| 44 | +``` |
| 45 | + |
| 46 | +Uniquely identifies the channel. It is either assigned by the desktop agent (system channel) or defined by an application (app channel). |
| 47 | + |
| 48 | +### `type` |
| 49 | + |
| 50 | +```ts |
| 51 | +public readonly type: string; |
| 52 | +``` |
| 53 | + |
| 54 | +Can be _system_ or _app_. |
| 55 | + |
| 56 | +### `displayMetadata` |
| 57 | + |
| 58 | +```ts |
| 59 | +public readonly displayMetadata?: DisplayMetadata; |
| 60 | +``` |
| 61 | + |
| 62 | +DisplayMetadata can be used to provide display hints for channels intended to be visualized and selectable by end users. |
| 63 | + |
| 64 | +#### See also |
| 65 | +* [`DisplayMetadata`](DisplayMetadata) |
| 66 | + |
| 67 | +## Methods |
| 68 | + |
| 69 | +### `broadcast` |
| 70 | + |
| 71 | +```typescript |
| 72 | +public broadcast(context: Context): Promise<void>; |
| 73 | +``` |
| 74 | + |
| 75 | +Broadcasts a context on the channel. This function can be used without first joining the channel, allowing applications to broadcast on channels that they aren't a member of. |
| 76 | + |
| 77 | +If broadcasting fails, the promise will return an `Error` with a string from the [`ChannelError`](Errors#channelerror) enumeration. |
| 78 | + |
| 79 | +#### Example |
| 80 | + |
| 81 | +```javascript |
| 82 | +const instrument = { |
| 83 | + type: 'fdc3.instrument', |
| 84 | + id: { |
| 85 | + ticker: 'AAPL' |
| 86 | + } |
| 87 | +}; |
| 88 | + |
| 89 | +try { |
| 90 | + await channel.broadcast(instrument); |
| 91 | +} catch (err: ChannelError) { |
| 92 | + // handler errror |
| 93 | +} |
| 94 | +``` |
| 95 | + |
| 96 | +#### See also |
| 97 | +* [`ChannelError`](Errors#channelerror) |
| 98 | +* [`getCurrentContext`](#getcurrentcontext) |
| 99 | +* [`addContextListener`](#addcontextlistener) |
| 100 | + |
| 101 | +### `getCurrentContext` |
| 102 | + |
| 103 | +```ts |
| 104 | +public getCurrentContext(contextType?: string): Promise<Context|null>; |
| 105 | +``` |
| 106 | + |
| 107 | +Returns the most recent context that was broadcast on the channel. If no context has been set on the channel, this will return `null`. |
| 108 | + |
| 109 | +Optionally a _context type_ can be provided, in which case the current context of the matching type will be returned (if any). |
| 110 | + |
| 111 | +Desktop agent implementations may decide to record most recent contexts by type, in which case it will be possible to get the most recent context of each type, but this is not necessarily guaranteed. |
| 112 | + |
| 113 | +If getting the current context fails, the promise will return an `Error` with a string from the [`ChannelError`](Errors#channelerror) enumeration. |
| 114 | + |
| 115 | +#### Examples |
| 116 | + |
| 117 | +Without specifying a context type: |
| 118 | + |
| 119 | +```ts |
| 120 | +try { |
| 121 | + const context = await channel.getCurrentContext(); |
| 122 | +} catch (err: ChannelError) { |
| 123 | + // handler errror |
| 124 | +} |
| 125 | +``` |
| 126 | + |
| 127 | +Specifying a context type: |
| 128 | + |
| 129 | +```ts |
| 130 | +try { |
| 131 | + const contact = await channel.getCurrentContext('fdc3.contact'); |
| 132 | +} catch (err: ChannelError) { |
| 133 | + // handler errror |
| 134 | +} |
| 135 | +``` |
| 136 | + |
| 137 | +#### See also |
| 138 | +* [`ChannelError`](Errors#channelerror) |
| 139 | +* [`broadcast`](#broadcast) |
| 140 | +* [`addContextListener`](#addcontextlistener) |
| 141 | + |
| 142 | +### `addContextListener` |
| 143 | + |
| 144 | +```ts |
| 145 | +public addContextListener(handler: ContextHandler): Listener; |
| 146 | +``` |
| 147 | + |
| 148 | +Adds a listener for incoming contexts whenever a broadcast happens on the channel. |
| 149 | + |
| 150 | +```ts |
| 151 | +public addContextListener(contextType: string, handler: ContextHandler): Listener; |
| 152 | +``` |
| 153 | + |
| 154 | +Adds a listener for incoming contexts of the specified _context type_ whenever a broadcast happens on this channel. |
| 155 | + |
| 156 | +#### Examples |
| 157 | + |
| 158 | +Add a listener for any context that is broadcast on the channel: |
| 159 | + |
| 160 | +```ts |
| 161 | +const listener = channel.addContextListener(context => { |
| 162 | + if (context.type === 'fdc3.contact') { |
| 163 | + // handle the contact |
| 164 | + } else if (context.type === 'fdc3.instrument') => { |
| 165 | + // handle the instrument |
| 166 | + } |
| 167 | +}); |
| 168 | + |
| 169 | +// later |
| 170 | +listener.unsubscribe(); |
| 171 | +``` |
| 172 | + |
| 173 | +Adding listeners for specific types of context that is broadcast on the channel: |
| 174 | + |
| 175 | +```ts |
| 176 | +const contactListener = channel.addContextListener('fdc3.contact', contact => { |
| 177 | + // handle the contact |
| 178 | +}); |
| 179 | + |
| 180 | +const instrumentListener = channel.addContextListener('fdc3.instrument', instrument => { |
| 181 | + // handle the instrument |
| 182 | +}); |
| 183 | + |
| 184 | +// later |
| 185 | +contactListener.unsubscribe(); |
| 186 | +instrumentListener.unsubscribe(); |
| 187 | +``` |
| 188 | + |
| 189 | +#### See also |
| 190 | +* [`Listener`](Listener) |
| 191 | +* [`ContextHandler`](ContextHandler) |
| 192 | +* [`broadcast`](#broadcast) |
| 193 | +* [`getCurrentContext`](#addcontextlistener) |
0 commit comments