generated from amazon-archives/__template_Apache-2.0
-
Notifications
You must be signed in to change notification settings - Fork 82
Expand file tree
/
Copy pathio-helper.ts
More file actions
59 lines (51 loc) · 1.65 KB
/
io-helper.ts
File metadata and controls
59 lines (51 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import type { IIoHost } from '../io-host';
import type { IoMessage, IoRequest } from '../io-message';
import type { ToolkitAction } from '../toolkit-action';
import type { SpanEnd, SpanDefinition } from './span';
import { SpanMaker } from './span';
export type ActionLessMessage<T> = Omit<IoMessage<T>, 'action'>;
export type ActionLessRequest<T, U> = Omit<IoRequest<T, U>, 'action'>;
/**
* A class containing helper tools to interact with IoHost
*/
export class IoHelper implements IIoHost {
public static fromIoHost(ioHost: IIoHost, action: ToolkitAction) {
return new IoHelper(ioHost, action);
}
private readonly ioHost: IIoHost;
private readonly action: ToolkitAction;
private constructor(ioHost: IIoHost, action: ToolkitAction) {
this.ioHost = ioHost;
this.action = action;
}
/**
* Forward a message to the IoHost, while injection the current action
*/
public notify(msg: ActionLessMessage<unknown>): Promise<void> {
return this.ioHost.notify({
...msg,
action: this.action,
});
}
/**
* Forward a request to the IoHost, while injection the current action
*/
public requestResponse<T, U>(msg: ActionLessRequest<T, U>): Promise<U> {
return this.ioHost.requestResponse({
...msg,
action: this.action,
});
}
/**
* Create a new marker from a given registry entry
*/
public span<S extends object, E extends SpanEnd>(definition: SpanDefinition<S, E>) {
return new SpanMaker(this, definition);
}
}
/**
* Wraps an IoHost and creates an IoHelper from it
*/
export function asIoHelper(ioHost: IIoHost, action: ToolkitAction): IoHelper {
return IoHelper.fromIoHost(ioHost, action);
}