postalworker-es 4.0.2 → 4.0.4
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 +198 -219
- package/dist/PostalWorker.es.js +5 -3
- package/dist/PostalWorker.min.js +3 -3
- 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,106 @@ 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
|
-
|
|
242
|
-
|
|
282
|
+
|
|
283
|
+
if (_env._packages[address].content)
|
|
284
|
+
PostalWorker().fire(DELIVERY, {
|
|
285
|
+
address: address,
|
|
286
|
+
content: _env._packages[address].content
|
|
287
|
+
});
|
|
288
|
+
else
|
|
289
|
+
PostalWorker().fire(DELIVERY, {
|
|
290
|
+
address: address,
|
|
291
|
+
content: null
|
|
292
|
+
});
|
|
243
293
|
}
|
|
244
294
|
|
|
295
|
+
/**
|
|
296
|
+
*
|
|
297
|
+
* @param {*} msg
|
|
298
|
+
*/
|
|
245
299
|
function _pack(msg) {
|
|
246
300
|
const { address, content } = msg;
|
|
247
|
-
let addr =
|
|
301
|
+
let addr = _env._packages[address];
|
|
248
302
|
if (addr) addr.content = content;
|
|
249
303
|
|
|
250
|
-
|
|
304
|
+
PostalWorker().fire(DELIVERY, { address, content });
|
|
305
|
+
|
|
306
|
+
// for (const [key, bx] of Object.entries(_env._postBoxes)) {
|
|
307
|
+
// if (key === address && bx.handlers.length) {
|
|
308
|
+
// bx.handlers.forEach(handler => {
|
|
309
|
+
// if (handler) {
|
|
310
|
+
// handler({ address, value: _env._postBoxes[address].value, box });
|
|
311
|
+
// }
|
|
312
|
+
// });
|
|
313
|
+
// }
|
|
314
|
+
// }
|
|
315
|
+
|
|
316
|
+
for (const [key, pack] of Object.entries(_env._packages)) {
|
|
317
|
+
if (key === address && pack.handlers.length) {
|
|
318
|
+
pack.handlers.forEach(handler => {
|
|
319
|
+
if (handler) {
|
|
320
|
+
handler({ address, content: content });
|
|
321
|
+
}
|
|
322
|
+
});
|
|
323
|
+
}
|
|
324
|
+
}
|
|
251
325
|
}
|
|
252
326
|
|
|
253
327
|
/**
|
|
@@ -260,8 +334,8 @@ function _processChannelMessage(msg) {
|
|
|
260
334
|
// console.info(msg);
|
|
261
335
|
switch (msg.type) {
|
|
262
336
|
case FIRE:
|
|
263
|
-
if (
|
|
264
|
-
|
|
337
|
+
if (_env._events.has(msg.data.msgClass)) {
|
|
338
|
+
_env._events.get(msg.data.msgClass)(msg.data.message, msg.id);
|
|
265
339
|
}
|
|
266
340
|
break;
|
|
267
341
|
case LOAD:
|
|
@@ -290,14 +364,14 @@ function _processChannelMessage(msg) {
|
|
|
290
364
|
}
|
|
291
365
|
}
|
|
292
366
|
|
|
293
|
-
|
|
367
|
+
const PostalWorker = () => ({
|
|
294
368
|
/**
|
|
295
369
|
*
|
|
296
370
|
* @param msgClass
|
|
297
371
|
* @param action
|
|
298
372
|
*/
|
|
299
373
|
on: (msgClass, action) => {
|
|
300
|
-
|
|
374
|
+
_env._events.set(msgClass, action);
|
|
301
375
|
},
|
|
302
376
|
|
|
303
377
|
/**
|
|
@@ -305,7 +379,7 @@ self.PostalWorker = () => ({
|
|
|
305
379
|
* @param msgClass
|
|
306
380
|
*/
|
|
307
381
|
un: msgClass => {
|
|
308
|
-
|
|
382
|
+
_env._events.delete(msgClass);
|
|
309
383
|
},
|
|
310
384
|
|
|
311
385
|
/**
|
|
@@ -314,8 +388,8 @@ self.PostalWorker = () => ({
|
|
|
314
388
|
* @param msg
|
|
315
389
|
*/
|
|
316
390
|
fire: (msgClass, msg) => {
|
|
317
|
-
|
|
318
|
-
|
|
391
|
+
_env._channel.postMessage(
|
|
392
|
+
_env._stringify({
|
|
319
393
|
postal: true,
|
|
320
394
|
type: FIRE,
|
|
321
395
|
id: POSTAL_SHARED_WORKER,
|
|
@@ -326,7 +400,7 @@ self.PostalWorker = () => ({
|
|
|
326
400
|
})
|
|
327
401
|
);
|
|
328
402
|
// deprecated
|
|
329
|
-
//
|
|
403
|
+
// PostalWorker._postMessenger(
|
|
330
404
|
// S.FIRE,
|
|
331
405
|
// audience,
|
|
332
406
|
// {
|
|
@@ -352,22 +426,83 @@ self.PostalWorker = () => ({
|
|
|
352
426
|
* @param {*} errorCallback
|
|
353
427
|
*/
|
|
354
428
|
subscribe: (endpoint, callback, errorCallback) => {
|
|
355
|
-
if (!
|
|
356
|
-
|
|
357
|
-
|
|
429
|
+
if (!_env._subscriptions.get(endpoint)) {
|
|
430
|
+
_env._subscriptions.set(endpoint, new EventSource(endpoint));
|
|
431
|
+
_env._subscriptions.get(endpoint).onmessage = msg => {
|
|
358
432
|
callback(msg);
|
|
359
433
|
};
|
|
360
434
|
if (errorCallback) {
|
|
361
|
-
|
|
435
|
+
_env._subscriptions.get(endpoint).onerror = error => {
|
|
362
436
|
errorCallback(error);
|
|
363
437
|
};
|
|
364
438
|
}
|
|
365
439
|
}
|
|
366
440
|
},
|
|
367
441
|
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
442
|
+
postBox: (address, callback) => {
|
|
443
|
+
_registerPObox(
|
|
444
|
+
{
|
|
445
|
+
id: _env._id,
|
|
446
|
+
address
|
|
447
|
+
},
|
|
448
|
+
callback
|
|
449
|
+
);
|
|
450
|
+
},
|
|
451
|
+
|
|
452
|
+
/**
|
|
453
|
+
*
|
|
454
|
+
* @param {*} address
|
|
455
|
+
* @param {*} value
|
|
456
|
+
*/
|
|
457
|
+
post: (address, value) => {
|
|
458
|
+
_postBox({ address, value });
|
|
459
|
+
},
|
|
460
|
+
|
|
461
|
+
/**
|
|
462
|
+
*
|
|
463
|
+
* @param {*} address
|
|
464
|
+
*/
|
|
465
|
+
closeBox: address => {
|
|
466
|
+
_closeBox({ address, id: _env._id });
|
|
467
|
+
},
|
|
468
|
+
|
|
469
|
+
/**
|
|
470
|
+
*
|
|
471
|
+
* @param {*} address
|
|
472
|
+
* @param {*} handling
|
|
473
|
+
*/
|
|
474
|
+
package: (address, handling) => {
|
|
475
|
+
// id, address
|
|
476
|
+
_preparePackage(
|
|
477
|
+
{
|
|
478
|
+
id: _env._id,
|
|
479
|
+
address: address
|
|
480
|
+
},
|
|
481
|
+
handling
|
|
482
|
+
);
|
|
483
|
+
},
|
|
484
|
+
|
|
485
|
+
/**
|
|
486
|
+
*
|
|
487
|
+
* @param {*} address
|
|
488
|
+
* @param {*} content
|
|
489
|
+
*/
|
|
490
|
+
pack: (address, content) => {
|
|
491
|
+
let xhRequest = new XMLHttpRequest();
|
|
492
|
+
xhRequest.onload = function () {
|
|
493
|
+
let reader = new FileReader();
|
|
494
|
+
reader.onloadend = function () {
|
|
495
|
+
_pack({ address, content: reader.result });
|
|
496
|
+
};
|
|
497
|
+
reader.readAsDataURL(xhRequest.response);
|
|
498
|
+
};
|
|
499
|
+
xhRequest.open("GET", content);
|
|
500
|
+
xhRequest.responseType = "blob";
|
|
501
|
+
xhRequest.send();
|
|
502
|
+
},
|
|
503
|
+
|
|
504
|
+
// temp
|
|
505
|
+
env: () => _env
|
|
371
506
|
});
|
|
372
507
|
|
|
373
508
|
/**
|
|
@@ -376,175 +511,19 @@ self.PostalWorker = () => ({
|
|
|
376
511
|
*/
|
|
377
512
|
onconnect = () => {
|
|
378
513
|
// New messaging method using broadcast channel
|
|
379
|
-
if (!
|
|
380
|
-
|
|
514
|
+
if (!_env._channel.onmessage)
|
|
515
|
+
_env._channel.onmessage = event => {
|
|
381
516
|
const data = JSON.parse(event.data);
|
|
382
517
|
_processChannelMessage(data);
|
|
383
518
|
};
|
|
384
519
|
|
|
385
520
|
// Announce alive to broadcast channel
|
|
386
|
-
|
|
387
|
-
|
|
521
|
+
_env._channel.postMessage(
|
|
522
|
+
_env._stringify({
|
|
388
523
|
postal: true,
|
|
389
524
|
type: WORKERREGISTER
|
|
390
525
|
})
|
|
391
526
|
);
|
|
392
527
|
};
|
|
393
528
|
|
|
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
529
|
}());
|
package/dist/PostalWorker.es.js
CHANGED
|
@@ -86,6 +86,8 @@ static registerDelivery(e){e._on(DELIVERY,s=>{const t=e;t._delivery(s)})}}/**
|
|
|
86
86
|
* @Author: Russ Stratfull
|
|
87
87
|
* @Contributors: Sakshi Dheer, & François Wauquier
|
|
88
88
|
*/
|
|
89
|
+
// todo: Research how to implement this polyfill: https://github.com/okikio/sharedworker
|
|
90
|
+
// import SharedWorkerPolyfill from "./SharedWorkerPolyfill";
|
|
89
91
|
let _config=!1,_stringify,_worker=!1,_parentWindow=!1,_subscriptions=new Set,_events=new Map,_crossEvents=new Map,_openingWindows=new Map,_windows=new Map,_broadcastNetwork=new Set,_channel=new BroadcastChannel(POSTAL_WORKER),_queue=[],_POboxes=new Map,_closures=new Map,_packages=new Map;
|
|
90
92
|
// Define the PostalWorker
|
|
91
93
|
class PostalWorker{/**
|
|
@@ -124,13 +126,13 @@ _getSubscriber(e){let s=e.split("://"),t=s[0];if(void 0!==s[1]){let e=s[1].split
|
|
|
124
126
|
_resolveWorker(){
|
|
125
127
|
// Browser supports SharedWorker
|
|
126
128
|
let e=!!window.SharedWorker;
|
|
127
|
-
//
|
|
129
|
+
// Polyfill on failure
|
|
128
130
|
return e=Boolean(e),e?(_worker=this._startSharedWorker())||(_worker=this._startDedicatedWorker()):_worker=this._startDedicatedWorker(),_worker}/**
|
|
129
131
|
* Attempt to start SharedWorker thread or fail and return false
|
|
130
132
|
* @return {SharedWorker | boolean}
|
|
131
133
|
* @private
|
|
132
134
|
*/
|
|
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 */
|
|
135
|
+
_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
136
|
// this stuff is no longer tested and does not work
|
|
135
137
|
// (no more messaging, only the connection is being established here)
|
|
136
138
|
// but leaving it in to polyfill older browsers potentially
|
|
@@ -158,7 +160,7 @@ _startDedicatedWorker(){window.console.info("_startDedicatedWorker... (not compl
|
|
|
158
160
|
_getPostalRoute(){let e=Array.from(document.querySelectorAll(SCRIPT)).filter(e=>e.src?e.src.match(POSTAL_WORKER):[]).filter(e=>null!==e);if(
|
|
159
161
|
// What about if more than one postalworker is found?
|
|
160
162
|
// 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=","")}
|
|
163
|
+
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
164
|
// Otherwise, look next to where the PostalWorker script is located
|
|
163
165
|
return 1===e.length?e[0].src.replace(/PostalWorker\.min\.js.*$/,"").replace(/PostalWorker\.js.*$/,""):""}/**
|
|
164
166
|
* Process window messaging events "data" by "type"
|
package/dist/PostalWorker.min.js
CHANGED
|
@@ -64,8 +64,8 @@ this._resolveWorker()}/**
|
|
|
64
64
|
return s(e,[{key:"_getSubscriber",value:function(e){var t=e.split("://"),r=t[0];if(void 0!==t[1]){var o=t[1].split("/")[0];return r.concat("://").concat(o)}return e}},{key:"_resolveWorker",value:function(){
|
|
65
65
|
// Browser supports SharedWorker
|
|
66
66
|
var e=!!window.SharedWorker;
|
|
67
|
-
//
|
|
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 */
|
|
67
|
+
// Polyfill 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"),{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