-
Notifications
You must be signed in to change notification settings - Fork 1
/
protocol.js
56 lines (51 loc) · 1.51 KB
/
protocol.js
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
import BaseProtocol from "./obj.js";
/**
* Wire protocol API
*/
export default class JPCProtocol extends BaseProtocol {
/**
* @param startObject {Object} Will be returned to client in "start" function
*/
constructor(startObject) {
super();
this._startObject = startObject;
}
/**
* Call this before calling any of the other functions.
*
* @param startObject {Object} Will be returned to client in "start" function
*/
init() {
this.start(this._startObject);
}
async getRemoteStartObject() {
return await this.mapIncomingObjects(await this.callRemote("start"));
}
/**
* Incoming calls.
* Implements the wire protocol.
*
* @param method {string} the message name, e.g. "func", "get", "func-r" etc.
* @param listener {async function(payload {JSON}}
* What the listener function returns is sent back as result to the caller.
* If listener throws, sends the error message to the caller at the remote end.
*/
registerIncomingCall(method, listener) {
throw new Error("Implement this");
}
/**
* Outgoing calls.
* Implements the wire protocol.
*
* @param method {string} the message name, e.g. "func", "get" etc.
* @param payload {JSON} see value in PROTOCOL.md
* @returns {any} see value in PROTOCOL.md
* The payload of the corresponding answer.
* @throws {Error} if:
* - the remote end threw an exception
* - the connection disappeared
*/
async callRemote(method, payload) {
throw new Error("Implement this");
}
}