|
1 | 1 | import type { IIoHost } from '../io-host';
|
2 | 2 | import type { IoMessage, IoRequest } from '../io-message';
|
3 | 3 | import type { ToolkitAction } from '../toolkit-action';
|
| 4 | +import type { SpanEnd, SpanDefinition } from './span'; |
| 5 | +import { SpanMaker } from './span'; |
4 | 6 |
|
5 |
| -export type Optional<T, K extends keyof T> = Pick<Partial<T>, K> & Omit<T, K>; |
6 |
| -export type SimplifiedMessage<T> = Pick<IoMessage<T>, 'level' | 'code' | 'message' | 'data'>; |
7 | 7 | export type ActionLessMessage<T> = Omit<IoMessage<T>, 'action'>;
|
8 | 8 | export type ActionLessRequest<T, U> = Omit<IoRequest<T, U>, 'action'>;
|
9 | 9 |
|
10 | 10 | /**
|
11 |
| - * Helper for IO messaging. |
12 |
| - * |
13 |
| - * Wraps a client provided IoHost and provides additional features & services to toolkit internal classes. |
| 11 | + * A class containing helper tools to interact with IoHost |
14 | 12 | */
|
15 |
| -export interface IoHelper extends IIoHost { |
16 |
| - notify(msg: ActionLessMessage<unknown>): Promise<void>; |
17 |
| - notify<T>(msg: ActionLessMessage<T>): Promise<void>; |
18 |
| - requestResponse<T, U>(msg: ActionLessRequest<T, U>): Promise<U>; |
| 13 | +export class IoHelper implements IIoHost { |
| 14 | + public static fromIoHost(ioHost: IIoHost, action: ToolkitAction) { |
| 15 | + return new IoHelper(ioHost, action); |
| 16 | + } |
| 17 | + |
| 18 | + private readonly ioHost: IIoHost; |
| 19 | + private readonly action: ToolkitAction; |
| 20 | + |
| 21 | + private constructor(ioHost: IIoHost, action: ToolkitAction) { |
| 22 | + this.ioHost = ioHost; |
| 23 | + this.action = action; |
| 24 | + } |
| 25 | + |
| 26 | + /** |
| 27 | + * Forward a message to the IoHost, while injection the current action |
| 28 | + */ |
| 29 | + public notify(msg: ActionLessMessage<unknown>): Promise<void> { |
| 30 | + return this.ioHost.notify({ |
| 31 | + ...msg, |
| 32 | + action: this.action, |
| 33 | + }); |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * Forward a request to the IoHost, while injection the current action |
| 38 | + */ |
| 39 | + public requestResponse<T, U>(msg: ActionLessRequest<T, U>): Promise<U> { |
| 40 | + return this.ioHost.requestResponse({ |
| 41 | + ...msg, |
| 42 | + action: this.action, |
| 43 | + }); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Create a new marker from a given registry entry |
| 48 | + */ |
| 49 | + public span<S extends object, E extends SpanEnd>(definition: SpanDefinition<S, E>) { |
| 50 | + return new SpanMaker(this, definition); |
| 51 | + } |
19 | 52 | }
|
20 | 53 |
|
21 | 54 | /**
|
22 | 55 | * Wraps an IoHost and creates an IoHelper from it
|
23 | 56 | */
|
24 | 57 | export function asIoHelper(ioHost: IIoHost, action: ToolkitAction): IoHelper {
|
25 |
| - return { |
26 |
| - notify: async <T>(msg: Omit<IoMessage<T>, 'action'>) => { |
27 |
| - await ioHost.notify({ |
28 |
| - ...msg, |
29 |
| - action, |
30 |
| - }); |
31 |
| - }, |
32 |
| - requestResponse: async <T, U>(msg: Omit<IoRequest<T, U>, 'action'>) => { |
33 |
| - return ioHost.requestResponse({ |
34 |
| - ...msg, |
35 |
| - action, |
36 |
| - }); |
37 |
| - }, |
38 |
| - }; |
| 58 | + return IoHelper.fromIoHost(ioHost, action); |
39 | 59 | }
|
0 commit comments