@@ -4,6 +4,9 @@ import { Emitter } from "@coder/events";
4
4
import { logger , field } from "@coder/logger" ;
5
5
import { ChildProcess , SpawnOptions , ServerProcess } from "./command" ;
6
6
7
+ /**
8
+ * Client accepts an arbitrary connection intended to communicate with the Server.
9
+ */
7
10
export class Client {
8
11
private evalId : number = 0 ;
9
12
private evalDoneEmitter : Emitter < EvalDoneMessage > = new Emitter ( ) ;
@@ -12,6 +15,9 @@ export class Client {
12
15
private sessionId : number = 0 ;
13
16
private sessions : Map < number , ServerProcess > = new Map ( ) ;
14
17
18
+ /**
19
+ * @param connection Established connection to the server
20
+ */
15
21
public constructor (
16
22
private readonly connection : ReadWriteConnection ,
17
23
) {
@@ -31,6 +37,18 @@ export class Client {
31
37
public evaluate < R , T1 , T2 , T3 , T4 > ( func : ( a1 : T1 , a2 : T2 , a3 : T3 , a4 : T4 ) => R , a1 : T1 , a2 : T2 , a3 : T3 , a4 : T4 ) : Promise < R > ;
32
38
public evaluate < R , T1 , T2 , T3 , T4 , T5 > ( func : ( a1 : T1 , a2 : T2 , a3 : T3 , a4 : T4 , a5 : T5 ) => R , a1 : T1 , a2 : T2 , a3 : T3 , a4 : T4 , a5 : T5 ) : Promise < R > ;
33
39
public evaluate < R , T1 , T2 , T3 , T4 , T5 , T6 > ( func : ( a1 : T1 , a2 : T2 , a3 : T3 , a4 : T4 , a5 : T5 , a6 : T6 ) => R , a1 : T1 , a2 : T2 , a3 : T3 , a4 : T4 , a5 : T5 , a6 : T6 ) : Promise < R > ;
40
+ /**
41
+ * Evaluates a function on the server.
42
+ * To pass variables, ensure they are serializable and passed through the included function.
43
+ * @example
44
+ * const returned = await this.client.evaluate((value) => {
45
+ * return value;
46
+ * }, "hi");
47
+ * console.log(returned);
48
+ * // output: "hi"
49
+ * @param func Function to evaluate
50
+ * @returns {Promise } Promise rejected or resolved from the evaluated function
51
+ */
34
52
public evaluate < R , T1 , T2 , T3 , T4 , T5 , T6 > ( func : ( a1 ?: T1 , a2 ?: T2 , a3 ?: T3 , a4 ?: T4 , a5 ?: T5 , a6 ?: T6 ) => R , a1 ?: T1 , a2 ?: T2 , a3 ?: T3 , a4 ?: T4 , a5 ?: T5 , a6 ?: T6 ) : Promise < R > {
35
53
const newEval = new NewEvalMessage ( ) ;
36
54
const id = this . evalId ++ ;
@@ -98,6 +116,10 @@ export class Client {
98
116
99
117
/**
100
118
* Spawns a process from a command. _Somewhat_ reflects the "child_process" API.
119
+ * @example
120
+ * const cp = this.client.spawn("echo", ["test"]);
121
+ * cp.stdout.on("data", (data) => console.log(data.toString()));
122
+ * cp.on("exit", (code) => console.log("exited with", code));
101
123
* @param command
102
124
* @param args Arguments
103
125
* @param options Options to execute for the command
@@ -108,7 +130,7 @@ export class Client {
108
130
109
131
/**
110
132
* Fork a module.
111
- * @param modulePath Path of the module
133
+ * @param modulePath Path of the module
112
134
* @param args Args to add for the module
113
135
* @param options Options to execute
114
136
*/
@@ -156,6 +178,10 @@ export class Client {
156
178
return serverProc ;
157
179
}
158
180
181
+ /**
182
+ * Handles a message from the server. All incoming server messages should be
183
+ * routed through here.
184
+ */
159
185
private handleMessage ( message : ServerMessage ) : void {
160
186
if ( message . hasEvalDone ( ) ) {
161
187
this . evalDoneEmitter . emit ( message . getEvalDone ( ) ! ) ;
0 commit comments