/
client.js
176 lines (158 loc) · 4.68 KB
/
client.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
"use strict";
var Class = require("./class").Class;
var TypeSystem = require("./type-system").TypeSystem;
var values = require("./util").values;
var Promise = require("es6-promise").Promise;
var MessageEvent = require("./event").MessageEvent;
var specification = require("./specification/core.json");
function recoverActorDescriptions(error) {
console.warn("Failed to fetch protocol specification (see reason below). " +
"Using a fallback protocal specification!",
error);
return require("./specification/protocol.json");
}
// Type to represent superviser actor relations to actors they supervise
// in terms of lifetime management.
var Supervisor = Class({
constructor: function(id) {
this.id = id;
this.workers = [];
}
});
var Telemetry = Class({
add: function(id, ms) {
console.log("telemetry::", id, ms)
}
});
// Consider making client a root actor.
var Client = Class({
constructor: function() {
this.root = null;
this.telemetry = new Telemetry();
this.setupConnection();
this.setupLifeManagement();
this.setupTypeSystem();
},
setupConnection: function() {
this.requests = [];
},
setupLifeManagement: function() {
this.cache = Object.create(null);
this.graph = Object.create(null);
this.get = this.get.bind(this);
this.release = this.release.bind(this);
},
setupTypeSystem: function() {
this.typeSystem = new TypeSystem(this);
this.typeSystem.registerTypes(specification);
},
connect: function(port) {
var client = this;
return new Promise(function(resolve, reject) {
client.port = port;
port.onmessage = client.receive.bind(client);
client.onReady = resolve;
client.onFail = reject;
port.start();
});
},
send: function(packet) {
this.port.postMessage(packet);
},
request: function(packet) {
var client = this;
return new Promise(function(resolve, reject) {
client.requests.push(packet.to, { resolve: resolve, reject: reject });
client.send(packet);
});
},
receive: function(event) {
var packet = event.data;
if (!this.root) {
if (packet.from !== "root")
throw Error("Initial packet must be from root");
if (!("applicationType" in packet))
throw Error("Initial packet must contain applicationType field");
this.root = this.typeSystem.read("root", null, "root");
this.root
.protocolDescription()
.catch(recoverActorDescriptions)
.then(this.typeSystem.registerTypes.bind(this.typeSystem))
.then(this.onReady.bind(this, this.root), this.onFail);
} else {
var actor = this.get(packet.from) || this.root;
var event = actor.events[packet.type];
if (event) {
var message = new MessageEvent(packet.type, {
data: event.read(packet)
});
actor.dispatchEvent(message);
} else {
var index = this.requests.indexOf(actor.id);
if (index >= 0) {
var request = this.requests.splice(index, 2).pop();
if (packet.error)
request.reject(packet);
else
request.resolve(packet);
} else {
console.error(Error("Unexpected packet " + JSON.stringify(packet, 2, 2)),
packet,
this.requests.slice(0));
}
}
}
},
get: function(id) {
return this.cache[id];
},
supervisorOf: function(actor) {
for (var id in this.graph) {
if (this.graph[id].indexOf(actor.id) >= 0) {
return id;
}
}
},
workersOf: function(actor) {
return this.graph[actor.id];
},
supervise: function(actor, worker) {
var workers = this.workersOf(actor)
if (workers.indexOf(worker.id) < 0) {
workers.push(worker.id);
}
},
unsupervise: function(actor, worker) {
var workers = this.workersOf(actor);
var index = workers.indexOf(worker.id)
if (index >= 0) {
workers.splice(index, 1)
}
},
register: function(actor) {
var registered = this.get(actor.id);
if (!registered) {
this.cache[actor.id] = actor;
this.graph[actor.id] = [];
} else if (registered !== actor) {
throw new Error("Different actor with same id is already registered");
}
},
unregister: function(actor) {
if (this.get(actor.id)) {
delete this.cache[actor.id];
delete this.graph[actor.id];
}
},
release: function(actor) {
var supervisor = this.supervisorOf(actor);
if (supervisor)
this.unsupervise(supervisor, actor);
var workers = this.workersOf(actor)
if (workers) {
workers.map(this.get).forEach(this.release)
}
this.unregister(actor);
}
});
exports.Client = Client;