forked from NorthernOceanS/NormaConstructor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create-client.js
54 lines (51 loc) · 1.26 KB
/
create-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
let {eventCenter} = require('./create-event-center.js');
class FakeClientSystem{
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 FakeClient{
constructor(port){
this._port = port;
}
log(){
// no-op
}
registerSystem(){
let clientSystem = new FakeClientSystem(this._port);
this._clientSystem = clientSystem;
return clientSystem;
}
}
global.client = new FakeClient(eventCenter.createPort());