-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate-server.js
54 lines (51 loc) · 1.26 KB
/
create-server.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
let {eventCenter} = require('./create-event-center.js');
class FakeServerSystem{
constructor(port){
let that = this;
this._port = port;
this.eventDataMap = new Map();
this.eventMap = new Map();
this._port.onmessage = function(event){
let {data} = event;
let {eventIdentifier, eventData} = data;
let eventCallback = that.eventMap.get(eventIdentifier);
if(eventCallback !== undefined) {
eventCallback(eventData);
}
}
}
createEventData(eventIdentifier){
let eventData = this.eventDataMap.get(eventIdentifier);
if(eventData === undefined) {
return {data: {}};
} else {
return {data: eventData};
}
}
broadcastEvent(eventIdentifier, eventData){
this._port.postMessage({eventIdentifier, eventData});
return true;
}
listenForEvent(eventIdentifier, eventCallback){
this.eventMap.set(eventIdentifier, eventCallback);
return true;
}
registerEventData(eventIdentifier, eventData){
this.eventDataMap.set(eventIdentifier, eventData);
return true;
}
}
class FakeServer{
constructor(port){
this._port = port;
}
log(){
// no-op
}
registerSystem(){
let serverSystem = new FakeServerSystem(this._port);
this._serverSystem = serverSystem;
return serverSystem;
}
}
global.server = new FakeServer(eventCenter.createPort());