Skip to content

Commit 636a270

Browse files
committed
Document client functions
1 parent dad52e3 commit 636a270

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

packages/server/src/browser/client.ts

+27-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ import { Emitter } from "@coder/events";
44
import { logger, field } from "@coder/logger";
55
import { ChildProcess, SpawnOptions, ServerProcess } from "./command";
66

7+
/**
8+
* Client accepts an arbitrary connection intended to communicate with the Server.
9+
*/
710
export class Client {
811
private evalId: number = 0;
912
private evalDoneEmitter: Emitter<EvalDoneMessage> = new Emitter();
@@ -12,6 +15,9 @@ export class Client {
1215
private sessionId: number = 0;
1316
private sessions: Map<number, ServerProcess> = new Map();
1417

18+
/**
19+
* @param connection Established connection to the server
20+
*/
1521
public constructor(
1622
private readonly connection: ReadWriteConnection,
1723
) {
@@ -31,6 +37,18 @@ export class Client {
3137
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>;
3238
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>;
3339
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+
*/
3452
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> {
3553
const newEval = new NewEvalMessage();
3654
const id = this.evalId++;
@@ -98,6 +116,10 @@ export class Client {
98116

99117
/**
100118
* 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));
101123
* @param command
102124
* @param args Arguments
103125
* @param options Options to execute for the command
@@ -108,7 +130,7 @@ export class Client {
108130

109131
/**
110132
* Fork a module.
111-
* @param modulePath Path of the module
133+
* @param modulePath Path of the module
112134
* @param args Args to add for the module
113135
* @param options Options to execute
114136
*/
@@ -156,6 +178,10 @@ export class Client {
156178
return serverProc;
157179
}
158180

181+
/**
182+
* Handles a message from the server. All incoming server messages should be
183+
* routed through here.
184+
*/
159185
private handleMessage(message: ServerMessage): void {
160186
if (message.hasEvalDone()) {
161187
this.evalDoneEmitter.emit(message.getEvalDone()!);

0 commit comments

Comments
 (0)