Skip to content

Commit 1300a8b

Browse files
committed
Add context filtering to API and improve docs
- Added overloads for addContextListener that include contextType parameter - Added optional contextType parameter to getContextValue - Rename Channel.addBroadcastListener to Channel.addContextListener for consistency - Change signature of channel context listener to match desktop agent one (remove redundant channel parameter) - Introduce ContextHandler shared type for context handlers like addContextListener. - Add documentation for API changes - Move Channel, Listener, DisplayMetadata and ContextHandler to their own pages - Cleanup of markdown docs
1 parent 04c0f23 commit 1300a8b

File tree

9 files changed

+479
-166
lines changed

9 files changed

+479
-166
lines changed

docs/api/Channel.md

Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
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)

docs/api/Context.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,22 @@ type Context = object;
1111
```
1212

1313
The base object that all contexts should extend.
14+
15+
The API specification allows this to be any object, but typically this is supposed to be a context data object adhering to the [Context Data Specification](../context-spec).
16+
17+
This means that it must at least have a `type` property that indicates what type of data it represents, e.g. `'fdc3.contact'`.
18+
19+
The `type` property of context objects is important for certain FDC3 operations, like [`Channel.getCurrentContext`](Channel#getCurrentContext) and [`DesktopAgent.addContextListener`](DesktopAgent#addContextListener), which allows you to filter contexts by their type.
20+
21+
#### See also
22+
* [`ContextHandler`](ContextHandler)
23+
* [`DesktopAgent.open`](DesktopAgent#open)
24+
* [`DesktopAgent.broadcast`](DesktopAgent#broadcast)
25+
* [`DesktopAgent.addIntentListener`](DesktopAgent#addintentlistener)
26+
* [`DesktopAgent.addContextListener`](DesktopAgent#addcontextlistener)
27+
* [`DesktopAgent.findIntent`](DesktopAgent#findintent)
28+
* [`DesktopAgent.findIntentsByContext`](DesktopAgent#findintentsbycontext)
29+
* [`DesktopAgent.raiseIntent`](DesktopAgent#raiseintent)
30+
* [`Channel.broadcast`](Channel#broadcast)
31+
* [`Channel.getCurrentContext`](Cahnnel#getCurrentContext)
32+
* [`Channel.addContextListener`](Cahnnel#addContextListener)

docs/api/ContextHandler.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
---
2+
id: ContextHandler
3+
sidebar_label: ContextHandler
4+
title: ContextHandler
5+
hide_title: true
6+
---
7+
# `ContextHandler`
8+
9+
```typescript
10+
type ContextHandler = (context: Context) => void;
11+
```
12+
13+
Describes a callback that handles a context event.
14+
15+
Used when attaching listeners for context broadcasts and raised intents.
16+
17+
#### See also
18+
* [`Context`](Context)
19+
* [`DesktopAgent.addIntentListener`](DesktopAgent#addintentlistener)
20+
* [`DesktopAgent.addContextListener`](DesktopAgent#addcontextlistener)
21+
* [`Channel.addContextListener`](Channel#addcontextlistener)

0 commit comments

Comments
 (0)