postalworker-es 4.0.1 → 4.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/PostalSharedWorker.js +193 -219
- package/dist/PostalWorker.es.js +2 -2
- package/dist/PostalWorker.min.js +2 -2
- package/package.json +1 -1
|
@@ -96,7 +96,8 @@ index.ensureProperties = ensureProperties_1;
|
|
|
96
96
|
* @Contributors: Sakshi Dheer, & Francois Wauquier
|
|
97
97
|
*/
|
|
98
98
|
|
|
99
|
-
|
|
99
|
+
const _env = {
|
|
100
|
+
_id: "SharedWorker",
|
|
100
101
|
_members: {},
|
|
101
102
|
_stringify: index,
|
|
102
103
|
_events: new Map(),
|
|
@@ -104,7 +105,8 @@ self._env = {
|
|
|
104
105
|
_channel: new BroadcastChannel(POSTAL_WORKER),
|
|
105
106
|
_subscriptions: new Map(),
|
|
106
107
|
_postBoxes: {},
|
|
107
|
-
_packages: {}
|
|
108
|
+
_packages: {},
|
|
109
|
+
PostalWorker: {}
|
|
108
110
|
};
|
|
109
111
|
|
|
110
112
|
/**
|
|
@@ -112,18 +114,35 @@ self._env = {
|
|
|
112
114
|
* @param {*} msg
|
|
113
115
|
*/
|
|
114
116
|
function _loadLibrary(msg) {
|
|
115
|
-
let scrpt =
|
|
117
|
+
let scrpt = _env._scripts.get(msg.data);
|
|
116
118
|
// if script already exists, don't reload it
|
|
117
119
|
if (scrpt) {
|
|
118
120
|
// just add window id to it
|
|
119
121
|
if (!scrpt.includes(msg.id)) scrpt.push(msg.id);
|
|
120
122
|
} else {
|
|
121
123
|
try {
|
|
122
|
-
|
|
123
|
-
|
|
124
|
+
// Thank you chatGpt for this one
|
|
125
|
+
// Create a closure and importScripts in the closure
|
|
126
|
+
// this allows binding the imported script to the current scope
|
|
127
|
+
// and PostalWorker can be passed securely to it, a little like
|
|
128
|
+
// import in es modules
|
|
129
|
+
const closure = (() => {
|
|
130
|
+
importScripts(msg.data);
|
|
131
|
+
return () => {
|
|
132
|
+
return {
|
|
133
|
+
usePrivate: init
|
|
134
|
+
};
|
|
135
|
+
};
|
|
136
|
+
})();
|
|
137
|
+
const importedScript = closure();
|
|
138
|
+
// This is the magic
|
|
139
|
+
importedScript.usePrivate(PostalWorker);
|
|
140
|
+
|
|
141
|
+
_env._scripts.set(msg.data, [msg.id]);
|
|
124
142
|
} catch (e) {
|
|
125
|
-
|
|
126
|
-
|
|
143
|
+
console.error(e);
|
|
144
|
+
_env._channel.postMessage(
|
|
145
|
+
_env._stringify({
|
|
127
146
|
postal: true,
|
|
128
147
|
type: "ERROR",
|
|
129
148
|
data: `PostalWorkerWorker Error: Unable to load file ${msg.data}`
|
|
@@ -139,7 +158,7 @@ function _loadLibrary(msg) {
|
|
|
139
158
|
*/
|
|
140
159
|
function _addNode(msg) {
|
|
141
160
|
const { id, nodeType } = msg;
|
|
142
|
-
|
|
161
|
+
_env._members[id] = nodeType;
|
|
143
162
|
}
|
|
144
163
|
|
|
145
164
|
/**
|
|
@@ -148,8 +167,8 @@ function _addNode(msg) {
|
|
|
148
167
|
*/
|
|
149
168
|
function _removeNode(msg) {
|
|
150
169
|
const { id } = msg;
|
|
151
|
-
delete
|
|
152
|
-
Object.entries(
|
|
170
|
+
delete _env._members[id];
|
|
171
|
+
Object.entries(_env._postBoxes).forEach(box => {
|
|
153
172
|
const [, node] = box;
|
|
154
173
|
if (node.members.includes(id))
|
|
155
174
|
node.members.splice(node.members.indexOf(id), 1);
|
|
@@ -159,30 +178,42 @@ function _removeNode(msg) {
|
|
|
159
178
|
/**
|
|
160
179
|
*
|
|
161
180
|
* @param {*} msg
|
|
181
|
+
* @param {Function} callback - optional callback
|
|
162
182
|
*/
|
|
163
|
-
function _registerPObox(msg) {
|
|
183
|
+
function _registerPObox(msg, callback) {
|
|
164
184
|
const { id, address } = msg;
|
|
165
|
-
let addr =
|
|
185
|
+
let addr = _env._postBoxes[address];
|
|
166
186
|
if (addr) {
|
|
167
187
|
if (!addr.members.includes(id)) addr.members.push(id);
|
|
188
|
+
if (!addr.handlers.length) addr.handlers.push(callback);
|
|
168
189
|
} else {
|
|
169
|
-
|
|
190
|
+
_env._postBoxes[address] = {
|
|
170
191
|
members: [id],
|
|
171
|
-
handlers: [],
|
|
192
|
+
handlers: callback ? [callback] : [],
|
|
172
193
|
value: null
|
|
173
194
|
};
|
|
174
195
|
}
|
|
175
196
|
|
|
176
|
-
const box = Object.entries(
|
|
197
|
+
const box = Object.entries(_env._postBoxes).reduce((store, current) => {
|
|
177
198
|
store[current[0]] = current[1].value;
|
|
178
199
|
return store;
|
|
179
200
|
}, {});
|
|
180
201
|
|
|
181
|
-
|
|
202
|
+
PostalWorker().fire(COLLECT, {
|
|
182
203
|
address: address,
|
|
183
|
-
value:
|
|
204
|
+
value: _env._postBoxes[address].value,
|
|
184
205
|
box
|
|
185
206
|
});
|
|
207
|
+
// Is there a worker handler for this item?
|
|
208
|
+
for (const [key, bx] of Object.entries(_env._postBoxes)) {
|
|
209
|
+
if (key === address && bx.handlers.length) {
|
|
210
|
+
bx.handlers.forEach(handler => {
|
|
211
|
+
if (handler) {
|
|
212
|
+
handler({ address, value: _env._postBoxes[address].value, box });
|
|
213
|
+
}
|
|
214
|
+
});
|
|
215
|
+
}
|
|
216
|
+
}
|
|
186
217
|
}
|
|
187
218
|
|
|
188
219
|
/**
|
|
@@ -191,63 +222,101 @@ function _registerPObox(msg) {
|
|
|
191
222
|
*/
|
|
192
223
|
function _postBox(msg) {
|
|
193
224
|
const { address, value } = msg;
|
|
194
|
-
let addr =
|
|
225
|
+
let addr = _env._postBoxes[address];
|
|
195
226
|
if (addr) addr.value = value;
|
|
196
227
|
// todo: box not created case?
|
|
197
|
-
const box = Object.entries(
|
|
228
|
+
const box = Object.entries(_env._postBoxes).reduce((store, current) => {
|
|
198
229
|
store[current[0]] = current[1].value;
|
|
199
230
|
return store;
|
|
200
231
|
}, {});
|
|
201
|
-
|
|
232
|
+
PostalWorker().fire(COLLECT, { address, value, box });
|
|
233
|
+
// Is there a worker handler for this item?
|
|
234
|
+
for (const [key, bx] of Object.entries(_env._postBoxes)) {
|
|
235
|
+
if (key === address && bx.handlers.length) {
|
|
236
|
+
bx.handlers.forEach(handler => {
|
|
237
|
+
if (handler) handler({ address, value, box });
|
|
238
|
+
});
|
|
239
|
+
}
|
|
240
|
+
}
|
|
202
241
|
}
|
|
203
242
|
|
|
243
|
+
/**
|
|
244
|
+
*
|
|
245
|
+
* @param {*} msg
|
|
246
|
+
*/
|
|
204
247
|
function _closeBox(msg) {
|
|
205
248
|
const { id, address } = msg;
|
|
206
|
-
let addr =
|
|
249
|
+
let addr = _env._postBoxes[address];
|
|
207
250
|
if (addr) {
|
|
208
251
|
addr.members = addr.members.filter(m => m !== id);
|
|
252
|
+
if (id === _env._id) addr.handlers = [];
|
|
209
253
|
if (!addr.members.length) {
|
|
210
|
-
delete
|
|
254
|
+
delete _env._postBoxes[address];
|
|
211
255
|
}
|
|
212
256
|
const value = null;
|
|
213
|
-
const box = Object.entries(
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
{}
|
|
219
|
-
);
|
|
220
|
-
self.PostalWorker().fire(BOXCLOSED, { id, address, value, box });
|
|
257
|
+
const box = Object.entries(_env._postBoxes).reduce((store, current) => {
|
|
258
|
+
store[current[0]] = current[1].value;
|
|
259
|
+
return store;
|
|
260
|
+
}, {});
|
|
261
|
+
PostalWorker().fire(BOXCLOSED, { id, address, value, box });
|
|
221
262
|
}
|
|
222
263
|
}
|
|
223
264
|
|
|
224
|
-
|
|
265
|
+
/**
|
|
266
|
+
*
|
|
267
|
+
* @param {*} msg
|
|
268
|
+
* @param {*} callback
|
|
269
|
+
*/
|
|
270
|
+
function _preparePackage(msg, callback) {
|
|
225
271
|
const { id, address } = msg;
|
|
226
|
-
let pack =
|
|
272
|
+
let pack = _env._packages[address];
|
|
227
273
|
if (pack) {
|
|
228
274
|
if (!pack.members.includes(id)) pack.members.push(id);
|
|
275
|
+
if (!pack.handlers.length) pack.handlers.push(callback);
|
|
229
276
|
} else
|
|
230
|
-
|
|
277
|
+
_env._packages[address] = {
|
|
231
278
|
members: [id],
|
|
232
|
-
handlers: [],
|
|
279
|
+
handlers: callback ? [callback] : [],
|
|
233
280
|
content: null
|
|
234
281
|
};
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
// content: self._env._packages[address].content
|
|
242
|
-
// });
|
|
282
|
+
|
|
283
|
+
if (_env._packages[address].content)
|
|
284
|
+
PostalWorker().fire(DELIVERY, {
|
|
285
|
+
address: address,
|
|
286
|
+
content: _env._packages[address].content
|
|
287
|
+
});
|
|
243
288
|
}
|
|
244
289
|
|
|
290
|
+
/**
|
|
291
|
+
*
|
|
292
|
+
* @param {*} msg
|
|
293
|
+
*/
|
|
245
294
|
function _pack(msg) {
|
|
246
295
|
const { address, content } = msg;
|
|
247
|
-
let addr =
|
|
296
|
+
let addr = _env._packages[address];
|
|
248
297
|
if (addr) addr.content = content;
|
|
249
298
|
|
|
250
|
-
|
|
299
|
+
PostalWorker().fire(DELIVERY, { address, content });
|
|
300
|
+
|
|
301
|
+
// for (const [key, bx] of Object.entries(_env._postBoxes)) {
|
|
302
|
+
// if (key === address && bx.handlers.length) {
|
|
303
|
+
// bx.handlers.forEach(handler => {
|
|
304
|
+
// if (handler) {
|
|
305
|
+
// handler({ address, value: _env._postBoxes[address].value, box });
|
|
306
|
+
// }
|
|
307
|
+
// });
|
|
308
|
+
// }
|
|
309
|
+
// }
|
|
310
|
+
|
|
311
|
+
for (const [key, pack] of Object.entries(_env._packages)) {
|
|
312
|
+
if (key === address && pack.handlers.length) {
|
|
313
|
+
pack.handlers.forEach(handler => {
|
|
314
|
+
if (handler) {
|
|
315
|
+
handler({ address, content: content });
|
|
316
|
+
}
|
|
317
|
+
});
|
|
318
|
+
}
|
|
319
|
+
}
|
|
251
320
|
}
|
|
252
321
|
|
|
253
322
|
/**
|
|
@@ -260,8 +329,8 @@ function _processChannelMessage(msg) {
|
|
|
260
329
|
// console.info(msg);
|
|
261
330
|
switch (msg.type) {
|
|
262
331
|
case FIRE:
|
|
263
|
-
if (
|
|
264
|
-
|
|
332
|
+
if (_env._events.has(msg.data.msgClass)) {
|
|
333
|
+
_env._events.get(msg.data.msgClass)(msg.data.message, msg.id);
|
|
265
334
|
}
|
|
266
335
|
break;
|
|
267
336
|
case LOAD:
|
|
@@ -290,14 +359,14 @@ function _processChannelMessage(msg) {
|
|
|
290
359
|
}
|
|
291
360
|
}
|
|
292
361
|
|
|
293
|
-
|
|
362
|
+
const PostalWorker = () => ({
|
|
294
363
|
/**
|
|
295
364
|
*
|
|
296
365
|
* @param msgClass
|
|
297
366
|
* @param action
|
|
298
367
|
*/
|
|
299
368
|
on: (msgClass, action) => {
|
|
300
|
-
|
|
369
|
+
_env._events.set(msgClass, action);
|
|
301
370
|
},
|
|
302
371
|
|
|
303
372
|
/**
|
|
@@ -305,7 +374,7 @@ self.PostalWorker = () => ({
|
|
|
305
374
|
* @param msgClass
|
|
306
375
|
*/
|
|
307
376
|
un: msgClass => {
|
|
308
|
-
|
|
377
|
+
_env._events.delete(msgClass);
|
|
309
378
|
},
|
|
310
379
|
|
|
311
380
|
/**
|
|
@@ -314,8 +383,8 @@ self.PostalWorker = () => ({
|
|
|
314
383
|
* @param msg
|
|
315
384
|
*/
|
|
316
385
|
fire: (msgClass, msg) => {
|
|
317
|
-
|
|
318
|
-
|
|
386
|
+
_env._channel.postMessage(
|
|
387
|
+
_env._stringify({
|
|
319
388
|
postal: true,
|
|
320
389
|
type: FIRE,
|
|
321
390
|
id: POSTAL_SHARED_WORKER,
|
|
@@ -326,7 +395,7 @@ self.PostalWorker = () => ({
|
|
|
326
395
|
})
|
|
327
396
|
);
|
|
328
397
|
// deprecated
|
|
329
|
-
//
|
|
398
|
+
// PostalWorker._postMessenger(
|
|
330
399
|
// S.FIRE,
|
|
331
400
|
// audience,
|
|
332
401
|
// {
|
|
@@ -352,22 +421,83 @@ self.PostalWorker = () => ({
|
|
|
352
421
|
* @param {*} errorCallback
|
|
353
422
|
*/
|
|
354
423
|
subscribe: (endpoint, callback, errorCallback) => {
|
|
355
|
-
if (!
|
|
356
|
-
|
|
357
|
-
|
|
424
|
+
if (!_env._subscriptions.get(endpoint)) {
|
|
425
|
+
_env._subscriptions.set(endpoint, new EventSource(endpoint));
|
|
426
|
+
_env._subscriptions.get(endpoint).onmessage = msg => {
|
|
358
427
|
callback(msg);
|
|
359
428
|
};
|
|
360
429
|
if (errorCallback) {
|
|
361
|
-
|
|
430
|
+
_env._subscriptions.get(endpoint).onerror = error => {
|
|
362
431
|
errorCallback(error);
|
|
363
432
|
};
|
|
364
433
|
}
|
|
365
434
|
}
|
|
366
435
|
},
|
|
367
436
|
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
437
|
+
postBox: (address, callback) => {
|
|
438
|
+
_registerPObox(
|
|
439
|
+
{
|
|
440
|
+
id: _env._id,
|
|
441
|
+
address
|
|
442
|
+
},
|
|
443
|
+
callback
|
|
444
|
+
);
|
|
445
|
+
},
|
|
446
|
+
|
|
447
|
+
/**
|
|
448
|
+
*
|
|
449
|
+
* @param {*} address
|
|
450
|
+
* @param {*} value
|
|
451
|
+
*/
|
|
452
|
+
post: (address, value) => {
|
|
453
|
+
_postBox({ address, value });
|
|
454
|
+
},
|
|
455
|
+
|
|
456
|
+
/**
|
|
457
|
+
*
|
|
458
|
+
* @param {*} address
|
|
459
|
+
*/
|
|
460
|
+
closeBox: address => {
|
|
461
|
+
_closeBox({ address, id: _env._id });
|
|
462
|
+
},
|
|
463
|
+
|
|
464
|
+
/**
|
|
465
|
+
*
|
|
466
|
+
* @param {*} address
|
|
467
|
+
* @param {*} handling
|
|
468
|
+
*/
|
|
469
|
+
package: (address, handling) => {
|
|
470
|
+
// id, address
|
|
471
|
+
_preparePackage(
|
|
472
|
+
{
|
|
473
|
+
id: _env._id,
|
|
474
|
+
address: address
|
|
475
|
+
},
|
|
476
|
+
handling
|
|
477
|
+
);
|
|
478
|
+
},
|
|
479
|
+
|
|
480
|
+
/**
|
|
481
|
+
*
|
|
482
|
+
* @param {*} address
|
|
483
|
+
* @param {*} content
|
|
484
|
+
*/
|
|
485
|
+
pack: (address, content) => {
|
|
486
|
+
let xhRequest = new XMLHttpRequest();
|
|
487
|
+
xhRequest.onload = function () {
|
|
488
|
+
let reader = new FileReader();
|
|
489
|
+
reader.onloadend = function () {
|
|
490
|
+
_pack({ address, content: reader.result });
|
|
491
|
+
};
|
|
492
|
+
reader.readAsDataURL(xhRequest.response);
|
|
493
|
+
};
|
|
494
|
+
xhRequest.open("GET", content);
|
|
495
|
+
xhRequest.responseType = "blob";
|
|
496
|
+
xhRequest.send();
|
|
497
|
+
},
|
|
498
|
+
|
|
499
|
+
// temp
|
|
500
|
+
env: () => _env
|
|
371
501
|
});
|
|
372
502
|
|
|
373
503
|
/**
|
|
@@ -376,175 +506,19 @@ self.PostalWorker = () => ({
|
|
|
376
506
|
*/
|
|
377
507
|
onconnect = () => {
|
|
378
508
|
// New messaging method using broadcast channel
|
|
379
|
-
if (!
|
|
380
|
-
|
|
509
|
+
if (!_env._channel.onmessage)
|
|
510
|
+
_env._channel.onmessage = event => {
|
|
381
511
|
const data = JSON.parse(event.data);
|
|
382
512
|
_processChannelMessage(data);
|
|
383
513
|
};
|
|
384
514
|
|
|
385
515
|
// Announce alive to broadcast channel
|
|
386
|
-
|
|
387
|
-
|
|
516
|
+
_env._channel.postMessage(
|
|
517
|
+
_env._stringify({
|
|
388
518
|
postal: true,
|
|
389
519
|
type: WORKERREGISTER
|
|
390
520
|
})
|
|
391
521
|
);
|
|
392
522
|
};
|
|
393
523
|
|
|
394
|
-
// from connect:
|
|
395
|
-
// // // Deprecating
|
|
396
|
-
// let address = uniqueNumber();
|
|
397
|
-
// let src = event.source,
|
|
398
|
-
// port = {
|
|
399
|
-
// address: address,
|
|
400
|
-
// session: src,
|
|
401
|
-
// tries: 10
|
|
402
|
-
// };
|
|
403
|
-
// self.PostalWorker.ports.push(port);
|
|
404
|
-
// src.start();
|
|
405
|
-
// // src.addEventListener(S.MESSAGE, event => {
|
|
406
|
-
// // self.PostalWorker._registerMessage(
|
|
407
|
-
// // event,
|
|
408
|
-
// // src,
|
|
409
|
-
// // self.PostalWorker.ports
|
|
410
|
-
// // );
|
|
411
|
-
// // });
|
|
412
|
-
// let startup = {
|
|
413
|
-
// type: S.SET_ADDRESS,
|
|
414
|
-
// data: address
|
|
415
|
-
// };
|
|
416
|
-
// src.postMessage(startup);
|
|
417
|
-
|
|
418
|
-
// from object:
|
|
419
|
-
/**
|
|
420
|
-
* Listen for an event and execute the provided callback
|
|
421
|
-
* Events:
|
|
422
|
-
* remove - remove from messaging community/queue
|
|
423
|
-
*
|
|
424
|
-
* @param event
|
|
425
|
-
* @param callback
|
|
426
|
-
*/
|
|
427
|
-
// addListener: (event, callback) => {
|
|
428
|
-
// self.PostalWorker.listeners.set(S.REMOVE, callback);
|
|
429
|
-
// },
|
|
430
|
-
/** Deprecated
|
|
431
|
-
* Register message events
|
|
432
|
-
* @param event
|
|
433
|
-
* @param port
|
|
434
|
-
* @param ports
|
|
435
|
-
* @private
|
|
436
|
-
*/
|
|
437
|
-
// _registerMessage: (event, port, ports) => {
|
|
438
|
-
// if (event.data && event.type) {
|
|
439
|
-
// let msg = JSON.parse(event.data),
|
|
440
|
-
// range;
|
|
441
|
-
// switch (msg.type) {
|
|
442
|
-
// // Responses keep the worker aware of which connections are current
|
|
443
|
-
// case S.RESPONSE:
|
|
444
|
-
// if (msg.status) {
|
|
445
|
-
// let lastRequest = self.PostalWorker.ports.find(
|
|
446
|
-
// prt => prt.session === event.currentTarget
|
|
447
|
-
// );
|
|
448
|
-
// if (lastRequest && lastRequest.tries) lastRequest.tries++;
|
|
449
|
-
// }
|
|
450
|
-
// break;
|
|
451
|
-
// case S.ON:
|
|
452
|
-
// // todo: Not ported over from previous version yet
|
|
453
|
-
// // but what do we do with "on" from a window in the worker anyways???
|
|
454
|
-
// break;
|
|
455
|
-
// case S.UN:
|
|
456
|
-
// // todo: Not ported over from previous version yet
|
|
457
|
-
// break;
|
|
458
|
-
// case S.FIRE:
|
|
459
|
-
// console.info("fire worker");
|
|
460
|
-
// // Broadcast to windows/tabs
|
|
461
|
-
// range = msg.data.audience || S.ALL; // public, private, ALL todo: direct port messaging...
|
|
462
|
-
// self.PostalWorker._postMessenger(S.FIRE, range, msg.data, port);
|
|
463
|
-
// // Invoke registered event on this thread
|
|
464
|
-
// if (self.PostalWorker.events.has(msg.data.msgClass)) {
|
|
465
|
-
// let address, index, src;
|
|
466
|
-
// for (let p of self.PostalWorker.ports) {
|
|
467
|
-
// if (p.session === event.currentTarget) {
|
|
468
|
-
// address = p.address;
|
|
469
|
-
// index = self.PostalWorker.ports.indexOf(p);
|
|
470
|
-
// src = p.session;
|
|
471
|
-
// }
|
|
472
|
-
// }
|
|
473
|
-
// self.PostalWorker.events.get(msg.data.msgClass)(
|
|
474
|
-
// msg.data.message,
|
|
475
|
-
// { index: index, address: address, src: src }
|
|
476
|
-
// );
|
|
477
|
-
// }
|
|
478
|
-
// break;
|
|
479
|
-
// // todo...
|
|
480
|
-
// case S.LOAD:
|
|
481
|
-
// if (self.PostalWorker.scripts.has(msg.data)) {
|
|
482
|
-
// return;
|
|
483
|
-
// }
|
|
484
|
-
// // Wrap importScripts in try catch to report errors back to the main window
|
|
485
|
-
// // Attemp to load requested library
|
|
486
|
-
// try {
|
|
487
|
-
// importScripts(msg.data);
|
|
488
|
-
// self.PostalWorker.scripts.add(msg.data);
|
|
489
|
-
// } catch (e) {
|
|
490
|
-
// event.currentTarget.postMessage({
|
|
491
|
-
// type: "ERROR",
|
|
492
|
-
// data: `PostalWorkerWorker Error: Unable to load file ${msg.data}`
|
|
493
|
-
// });
|
|
494
|
-
// }
|
|
495
|
-
// break;
|
|
496
|
-
// case S.SET_ADDRESS: {
|
|
497
|
-
// for (let p of self.PostalWorker.ports) {
|
|
498
|
-
// if (p.session === event.currentTarget) {
|
|
499
|
-
// p.address = msg.data;
|
|
500
|
-
// }
|
|
501
|
-
// }
|
|
502
|
-
// let addressChange = {
|
|
503
|
-
// type: S.SET_ADDRESS,
|
|
504
|
-
// data: msg.data
|
|
505
|
-
// };
|
|
506
|
-
// event.currentTarget.postMessage(addressChange);
|
|
507
|
-
// }
|
|
508
|
-
// }
|
|
509
|
-
// }
|
|
510
|
-
// },
|
|
511
|
-
/**
|
|
512
|
-
* This function invokes a callback whenever a window is removed from the messaging queue
|
|
513
|
-
* To configure the worker to do this, use the "addListener" method and provide the event "remove"
|
|
514
|
-
* as the first argument
|
|
515
|
-
* @param removals
|
|
516
|
-
* @private
|
|
517
|
-
*/
|
|
518
|
-
// _invokeRemoveCallback: removals => {
|
|
519
|
-
// for (let r of removals) {
|
|
520
|
-
// self.PostalWorker.listeners.get(S.REMOVE)(r);
|
|
521
|
-
// }
|
|
522
|
-
// },
|
|
523
|
-
/**
|
|
524
|
-
* The messenger method used to post messages to the windows
|
|
525
|
-
* @param type
|
|
526
|
-
* @param audience
|
|
527
|
-
* @param msg
|
|
528
|
-
* @param port
|
|
529
|
-
* @private
|
|
530
|
-
*/
|
|
531
|
-
// _postMessenger: (type, audience, msg, port) => {
|
|
532
|
-
// let notification = {
|
|
533
|
-
// type: type,
|
|
534
|
-
// data: msg
|
|
535
|
-
// };
|
|
536
|
-
// switch (audience) {
|
|
537
|
-
// case S.PRIVATE:
|
|
538
|
-
// notification = {type: type,data: msg};
|
|
539
|
-
// _port.tries++;
|
|
540
|
-
// _port.session.postMessage(notification);
|
|
541
|
-
// break;
|
|
542
|
-
// case S.PUBLIC:
|
|
543
|
-
// self.PostalWorker.channel.postMessage(notification);
|
|
544
|
-
// break;
|
|
545
|
-
// default: // ALL
|
|
546
|
-
// self.PostalWorker.channel.postMessage(notification);
|
|
547
|
-
// }
|
|
548
|
-
// }
|
|
549
|
-
|
|
550
524
|
}());
|
package/dist/PostalWorker.es.js
CHANGED
|
@@ -130,7 +130,7 @@ return e=Boolean(e),e?(_worker=this._startSharedWorker())||(_worker=this._startD
|
|
|
130
130
|
* @return {SharedWorker | boolean}
|
|
131
131
|
* @private
|
|
132
132
|
*/
|
|
133
|
-
_startSharedWorker(){let e,s=this._getPostalRoute();try{e=new SharedWorker(s.concat("PostalSharedWorker").concat(".").concat(JS),POSTAL_WORKER);/* !!! DEPRECATED messaging route !!! but leaving in place as fallback for older browsers */
|
|
133
|
+
_startSharedWorker(){let e,s=this._getPostalRoute();try{e=new SharedWorker(s.concat("PostalSharedWorker").concat(".").concat(JS),{name:POSTAL_WORKER});/* !!! DEPRECATED messaging route !!! but leaving in place as fallback for older browsers */
|
|
134
134
|
// this stuff is no longer tested and does not work
|
|
135
135
|
// (no more messaging, only the connection is being established here)
|
|
136
136
|
// but leaving it in to polyfill older browsers potentially
|
|
@@ -158,7 +158,7 @@ _startDedicatedWorker(){window.console.info("_startDedicatedWorker... (not compl
|
|
|
158
158
|
_getPostalRoute(){let e=Array.from(document.querySelectorAll(SCRIPT)).filter(e=>e.src?e.src.match(POSTAL_WORKER):[]).filter(e=>null!==e);if(
|
|
159
159
|
// What about if more than one postalworker is found?
|
|
160
160
|
// This should be pointed out...
|
|
161
|
-
e.length>1&&window.console.warn('PostalWorker - Discovered more than 1 script tag matching "PostalWorker"'),_config.PostalRoute)return _config.PostalRoute;if(e[0].src.match("PostalRoute")){let s=e[0].src.match(/PostalRoute=.*&/)?e[0].src.match(/PostalRoute=.*&/)[0].replace("&",""):e[0].src.match(/PostalRoute=.*$/)[0];return s.match("PostalRoute=.*$")[0].replace("PostalRoute=","")}
|
|
161
|
+
e.length>1&&window.console.warn('PostalWorker - Discovered more than 1 script tag matching "PostalWorker"'),_config.PostalRoute)return _config.PostalRoute;if(e[0]&&e[0].src&&e[0].src.match("PostalRoute")){let s=e[0].src.match(/PostalRoute=.*&/)?e[0].src.match(/PostalRoute=.*&/)[0].replace("&",""):e[0].src.match(/PostalRoute=.*$/)[0];return s.match("PostalRoute=.*$")[0].replace("PostalRoute=","")}
|
|
162
162
|
// Otherwise, look next to where the PostalWorker script is located
|
|
163
163
|
return 1===e.length?e[0].src.replace(/PostalWorker\.min\.js.*$/,"").replace(/PostalWorker\.js.*$/,""):""}/**
|
|
164
164
|
* Process window messaging events "data" by "type"
|
package/dist/PostalWorker.min.js
CHANGED
|
@@ -65,7 +65,7 @@ return s(e,[{key:"_getSubscriber",value:function(e){var t=e.split("://"),r=t[0];
|
|
|
65
65
|
// Browser supports SharedWorker
|
|
66
66
|
var e=!!window.SharedWorker;
|
|
67
67
|
// Fallback on failure
|
|
68
|
-
return e=Boolean(e),e?(p=this._startSharedWorker())||(p=this._startDedicatedWorker()):p=this._startDedicatedWorker(),p}},{key:"_startSharedWorker",value:function(){var e=this,t=void 0,r=this._getPostalRoute();try{t=new SharedWorker(r.concat("PostalSharedWorker").concat(".").concat("js"),"PostalWorker");/* !!! DEPRECATED messaging route !!! but leaving in place as fallback for older browsers */
|
|
68
|
+
return e=Boolean(e),e?(p=this._startSharedWorker())||(p=this._startDedicatedWorker()):p=this._startDedicatedWorker(),p}},{key:"_startSharedWorker",value:function(){var e=this,t=void 0,r=this._getPostalRoute();try{t=new SharedWorker(r.concat("PostalSharedWorker").concat(".").concat("js"),{name:"PostalWorker"});/* !!! DEPRECATED messaging route !!! but leaving in place as fallback for older browsers */
|
|
69
69
|
// this stuff is no longer tested and does not work
|
|
70
70
|
// (no more messaging, only the connection is being established here)
|
|
71
71
|
// but leaving it in to polyfill older browsers potentially
|
|
@@ -80,7 +80,7 @@ e.workerOnline=!0,e._mapToBroadcastNetwork(e.id),b.length)for(;function(){return
|
|
|
80
80
|
window.console.warn("PostalWorker - Unable to start SharedWorker"),window.console.debug(e)}return t||!1}},{key:"_startDedicatedWorker",value:function(){window.console.info("_startDedicatedWorker... (not complete)")}},{key:"_getPostalRoute",value:function(){var e=Array.from(document.querySelectorAll("script")).filter(function(e){return e.src?e.src.match("PostalWorker"):[]}).filter(function(e){return null!==e});
|
|
81
81
|
// What about if more than one postalworker is found?
|
|
82
82
|
// This should be pointed out...
|
|
83
|
-
return e.length>1&&window.console.warn('PostalWorker - Discovered more than 1 script tag matching "PostalWorker"'),c.PostalRoute?c.PostalRoute:e[0].src.match("PostalRoute")?(e[0].src.match(/PostalRoute=.*&/)?e[0].src.match(/PostalRoute=.*&/)[0].replace("&",""):e[0].src.match(/PostalRoute=.*$/)[0]).match("PostalRoute=.*$")[0].replace("PostalRoute=",""):1===e.length?e[0].src.replace(/PostalWorker\.min\.js.*$/,"").replace(/PostalWorker\.js.*$/,""):""}},{key:"_messageController",value:function(e){if(!(!e.data||e.data.match&&e.data.match("webpack")||e.data.type&&e.data.type.match("webpack")||e.data.source&&e.data.source.match("react"))){var t=JSON.parse(e.data);if(t.postal&&!0===t.postal)// Not a postalWorker message
|
|
83
|
+
return e.length>1&&window.console.warn('PostalWorker - Discovered more than 1 script tag matching "PostalWorker"'),c.PostalRoute?c.PostalRoute:e[0]&&e[0].src&&e[0].src.match("PostalRoute")?(e[0].src.match(/PostalRoute=.*&/)?e[0].src.match(/PostalRoute=.*&/)[0].replace("&",""):e[0].src.match(/PostalRoute=.*$/)[0]).match("PostalRoute=.*$")[0].replace("PostalRoute=",""):1===e.length?e[0].src.replace(/PostalWorker\.min\.js.*$/,"").replace(/PostalWorker\.js.*$/,""):""}},{key:"_messageController",value:function(e){if(!(!e.data||e.data.match&&e.data.match("webpack")||e.data.type&&e.data.type.match("webpack")||e.data.source&&e.data.source.match("react"))){var t=JSON.parse(e.data);if(t.postal&&!0===t.postal)// Not a postalWorker message
|
|
84
84
|
// console.info(msg);
|
|
85
85
|
switch(t.type){case"CROSSFIRE":
|
|
86
86
|
// Is this a parent we don't yet know about?
|
package/package.json
CHANGED