-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
242 lines (212 loc) · 5.21 KB
/
index.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/**
* Created by narendrasisodiya on 06/08/15.
*/
var ENVIRONMENT_IS_WORKER = typeof importScripts === 'function';
var MESSAGE_TYPE = {
RETURN_MESSAGE: "returnMessage",
DEPART_WITH_SENDBACK_ID: "departWithSendBackId",
DEPART: "depart"
};
function setUpGlobalMessagePass() {
if (ENVIRONMENT_IS_WORKER === false && window._globalMessagePassForWorkerLessEnv_ === undefined) {
//Define only once !
var local = {};
local.registerBL = function (callback) {
local.BL = callback;
};
local.registerUI = function (callback) {
local.UI = callback;
};
local.sendToBL = function (message) {
local.BL(message);
};
local.sendToUI = function (message) {
local.UI(message);
};
window._globalMessagePassForWorkerLessEnv_ = local;
}
}
class FakeWorker {
constructor(BLL) {
if (BLL === undefined) {
throw "Send BLL Variable Either True of False";
}
this.BLL = BLL;
}
addEventListener(topic, callback) {
if (this.BLL) {
window._globalMessagePassForWorkerLessEnv_.registerBL(callback);
} else {
window._globalMessagePassForWorkerLessEnv_.registerUI(callback);
}
}
postMessage(message) {
if (this.BLL) {
window._globalMessagePassForWorkerLessEnv_.sendToUI({
data: message
});
} else {
window._globalMessagePassForWorkerLessEnv_.sendToBL({
data: message
});
}
}
}
class BaseAdapter {
constructor() {
this.isBridgeReady = false;
this._evtBus = {};
this._unsubObj = [];
this._returnCallback = [];
this.BLLayer = true;
}
_processRawMessage(message) {
console.log("Message Received", message);
if (message.type === MESSAGE_TYPE.RETURN_MESSAGE) {
var c = this._returnCallback[message.sendBackId];
if (typeof c === "function") {
c(message.payload);
this._returnCallback[message.sendBackId] = null;
} else {
console.error("message contains sendBackID which do not have any corrosponding callback");
}
} else {
var {path} = message;
//Some Message comes from URL Worker thread, we need to process it. message is raw message. payload is inside raw message.
// User is only interested in payload.
var f = this._evtBus[path];
var THAT = this;
if (f !== undefined && f.length !== 0) {
f.map((v, i)=> {
v(message.payload, function (sendBackData) {
console.log("send Back data is", sendBackData);
THAT.worker.postMessage({
payload: sendBackData,
type: MESSAGE_TYPE.RETURN_MESSAGE,
sendBackId: message.sendBackId
});
});
});
}
}
}
on(path, callback) {
if (this._evtBus[path] === undefined) {
this._evtBus[path] = [];
}
var index = this._evtBus[path].push(callback) - 1;
var unSubIndex = this._unsubObj.push({
path: path,
index: index
}) - 1;
return unSubIndex;
}
off(unSubIndex) {
try {
var {path , index} = this._unsubObj[unSubIndex];
this._evtBus[path][index] = null;
} catch (ex) {
}
}
post(path, payload, callback) {
if (callback === undefined) {
this.worker.postMessage({
path: path,
payload: payload,
type: MESSAGE_TYPE.DEPART
});
} else {
var id = this._registerSendBack(path, callback);
this.worker.postMessage({
path: path,
payload: payload,
type: MESSAGE_TYPE.DEPART_WITH_SENDBACK_ID,
sendBackId: id
});
}
}
_registerSendBack(path, callback) {
//when a Raw Message comes with type : "return", we need to find its corresponding callback.
var x = this._returnCallback.push(callback);
return x - 1;
}
}
class LocalAdapter extends BaseAdapter {
constructor(url) {
super();
if (url !== undefined) {
this._loadScript(url);
this.BLLayer = false;
}
if (window._globalMessagePassForWorkerLessEnv_ === undefined) {
setUpGlobalMessagePass();
}
this.worker = new FakeWorker(this.BLLayer);
this.worker.addEventListener('message', (e) => {
this._processRawMessage(e.data);
}, false);
}
_loadScript(url) {
var script = document.createElement('script');
script.src = url;
script.onload = () => {
this._onScriptLoaded();
};
document.head.appendChild(script);
}
_onScriptLoaded() {
this.isBridgeReady = true;
if (this._onReadyCallback !== undefined) {
this._onReadyCallback();
this._onReadyCallback === undefined;
}
}
onReady(callback) {
if (this.isBridgeReady === true) {
callback();
} else {
this._onReadyCallback = callback;
}
}
}
class WorkerAdapter extends BaseAdapter {
constructor(url) {
super();
if (ENVIRONMENT_IS_WORKER === false) {
this.BLLayer = false;
this.worker = new Worker(url);
} else {
this.BLLayer = true;
this.worker = self;
}
this.worker.addEventListener('message', (e) => {
this._processRawMessage(e.data);
}, false);
}
onReady(callback) {
callback();//Immediatly execute callback - TODO
}
}
var BLLayerLoader = {
load: function (config) {
if (ENVIRONMENT_IS_WORKER === true) {
throw "This method should not be called from Worker";
return;
}
var {url, method} = config;
if (method === "Worker") {
return new WorkerAdapter(url);
}
if (method === "Local") {
return new LocalAdapter(url);
}
},
getBLBridge: function () {
if (ENVIRONMENT_IS_WORKER === true) {
return new WorkerAdapter();
} else {
return new LocalAdapter();
}
}
};
module.exports = BLLayerLoader;