5
5
EmitOptions ,
6
6
CallOptions ,
7
7
} from './common' ;
8
- import { Emitter , IEmitter } from './emitter' ;
8
+ import { Emitter , ConcreteEmitter } from './emitter' ;
9
9
import { Dispatcher } from './dispatcher' ;
10
10
import {
11
11
MessageType ,
@@ -16,49 +16,149 @@ import {
16
16
} from './messages' ;
17
17
import { createCallbackProxy , isCallbackProxy } from './proxy' ;
18
18
19
+ /**
20
+ * A handle to the other end of the connection
21
+ *
22
+ * @remarks
23
+ *
24
+ * Use this handle to:
25
+ *
26
+ * - Call methods exposed by the other end
27
+ *
28
+ * - Add listeners to custom events emitted by the other end
29
+ *
30
+ * @typeParam M - The methods exposed by the other context
31
+ * @typeParam E - The events exposed by the other context
32
+ *
33
+ * @public
34
+ *
35
+ */
19
36
export interface RemoteHandle <
20
37
M extends MethodsType = any ,
21
38
E extends EventsType = any
22
- > extends IEmitter < E > {
23
- call : < K extends keyof M > (
39
+ > extends Emitter < E > {
40
+ /**
41
+ * Call a method exposed by the other end.
42
+ *
43
+ * @param methodName - The name of the method
44
+ * @param args - The list of arguments passed to the method
45
+ * @returns A Promise of the value returned by the method
46
+ *
47
+ */
48
+ call < K extends keyof M > (
24
49
methodName : K ,
25
50
...args : Parameters < M [ K ] >
26
- ) => Promise < InnerType < ReturnType < M [ K ] > > > ;
27
- customCall : < K extends keyof M > (
51
+ ) : Promise < InnerType < ReturnType < M [ K ] > > > ;
52
+
53
+ /**
54
+ * Call a method exposed by the other end.
55
+ *
56
+ * @param methodName - The name of the method
57
+ * @param args - The list of arguments passed to the method
58
+ * @param options - The {@link CallOptions} to customize this method call
59
+ * @returns A Promise of the value returned by the method
60
+ *
61
+ */
62
+ customCall < K extends keyof M > (
28
63
methodName : K ,
29
64
args : Parameters < M [ K ] > ,
30
65
options ?: CallOptions
31
- ) => Promise < InnerType < ReturnType < M [ K ] > > > ;
32
- setCallTransfer : < K extends keyof M > (
66
+ ) : Promise < InnerType < ReturnType < M [ K ] > > > ;
67
+
68
+ /**
69
+ * Specify which parts of the arguments of a given method call should be transferred
70
+ * into the other context instead of cloned.
71
+ *
72
+ * @remarks
73
+ *
74
+ * You only need to call setCallTransfer once per method. After the transfer function is set,
75
+ * it will automatically be used by all subsequent calls to the specified method.
76
+ *
77
+ * @param methodName - The name of the method
78
+ * @param transfer - A function that takes as parameters the arguments of a method call, and returns a list of transferable objects.
79
+ *
80
+ */
81
+ setCallTransfer < K extends keyof M > (
33
82
methodName : K ,
34
83
transfer : ( ...args : Parameters < M [ K ] > ) => Transferable [ ]
35
- ) => void ;
84
+ ) : void ;
36
85
}
37
86
87
+ /**
88
+ * A handle to the local end of the connection
89
+ *
90
+ * @remarks
91
+ *
92
+ * Use this handle to:
93
+ *
94
+ * - Emit custom events to the other end
95
+ *
96
+ * @typeParam M - The methods exposed by this context
97
+ * @typeParam E - The events exposed by this context
98
+ *
99
+ * @public
100
+ *
101
+ */
38
102
export interface LocalHandle <
39
103
M extends MethodsType = any ,
40
104
E extends EventsType = any
41
105
> {
42
- emit : < K extends keyof E > (
106
+ /**
107
+ * Emit a custom event with a payload. The event can be captured by the other context.
108
+ *
109
+ * @param eventName - The name of the event
110
+ * @param data - The payload associated with the event
111
+ * @param options - The {@link EmitOptions} to customize this emit call
112
+ *
113
+ */
114
+ emit < K extends keyof E > (
43
115
eventName : K ,
44
116
data : E [ K ] ,
45
117
options ?: EmitOptions
46
- ) => void ;
47
- setReturnTransfer : < K extends keyof M > (
118
+ ) : void ;
119
+
120
+ /**
121
+ * Specify which parts of the return value of a given method call should be transferred
122
+ * into the other context instead of cloned.
123
+ *
124
+ * @remarks
125
+ *
126
+ * You only need to call setReturnTransfer once per method. After the transfer function is set,
127
+ * it will automatically be used every time a value is returned by the specified method.
128
+ *
129
+ * @param methodName - The name of the method
130
+ * @param transfer - A function that takes as parameter the return value of a method call, and returns a list of transferable objects.
131
+ *
132
+ */
133
+ setReturnTransfer < K extends keyof M > (
48
134
methodName : K ,
49
135
transfer : ( result : InnerType < ReturnType < M [ K ] > > ) => Transferable [ ]
50
- ) => void ;
51
- setEmitTransfer : < K extends keyof E > (
136
+ ) : void ;
137
+
138
+ /**
139
+ * Specify which parts of the payload of a given event should be transferred
140
+ * into the other context instead of cloned.
141
+ *
142
+ * @remarks
143
+ *
144
+ * You only need to call setEmitTransfer once per event type. After the transfer function is set,
145
+ * it will automatically be used every time a payload is attached to the specific event.
146
+ *
147
+ * @param eventName - The name of the method
148
+ * @param transfer - A function that takes as parameter the payload of an event, and returns a list of transferable objects.
149
+ *
150
+ */
151
+ setEmitTransfer < K extends keyof E > (
52
152
eventName : K ,
53
153
transfer : ( payload : E [ K ] ) => Transferable [ ]
54
- ) => void ;
154
+ ) : void ;
55
155
}
56
156
57
157
export class ConcreteRemoteHandle <
58
158
M extends MethodsType = any ,
59
159
E extends EventsType = any
60
160
>
61
- extends Emitter < E >
161
+ extends ConcreteEmitter < E >
62
162
implements RemoteHandle < M , E > {
63
163
private _dispatcher : Dispatcher ;
64
164
private _callTransfer : { [ x : string ] : ( ...args : any ) => Transferable [ ] } ;
0 commit comments