postalworker-es 4.0.3 → 4.0.5
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 +5 -0
- package/dist/PostalWorker.es.js +24 -6
- package/dist/PostalWorker.min.js +52 -34
- package/package.json +2 -2
package/dist/PostalWorker.es.js
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
function throwsMessage(e){return"[Throws: "+(e?e.message:"?")+"]"}function safeGetValueFromPropertyOnObject(e,s){if(hasProp.call(e,s))try{return e[s]}catch(e){return throwsMessage(e)}return e[s]}function ensureProperties(e){// store references to objects we have seen before
|
|
7
7
|
function s(e){if(null===e||"object"!=typeof e)return e;if(-1!==t.indexOf(e))return"[Circular]";if(t.push(e),"function"==typeof e.toJSON)try{var n=s(e.toJSON());return t.pop(),n}catch(e){return throwsMessage(e)}if(Array.isArray(e)){var o=e.map(s);return t.pop(),o}var r=Object.keys(e).reduce(function(t,n){
|
|
8
8
|
// prevent faulty defined getter properties
|
|
9
|
-
return t[n]=s(safeGetValueFromPropertyOnObject(e,n)),t},{});return t.pop(),r}var t=[];return s(e)}function main(e){return _PostalWorker||(_PostalWorker=new PostalWorker(e,index))}const POSTAL_WORKER="PostalWorker",POSTAL_SHARED_WORKER="PostalSharedWorker",ON="ON",UN="UN",FIRE="FIRE",CROSSFIRE="CROSSFIRE",BACKFIRE="BACKFIRE",CHILDREGISTER="CHILDREGISTER",WORKERREGISTER="WORKERREGISTER",REGISTERID="REGISTERID",UNREGISTERID="UNREGISTERID",BROADCASTNODE="BROADCASTNODE",CROSSNODE="CROSSNODE",ERROR="ERROR",MESSAGE="message",SCRIPT="script",JS="js",LOAD="LOAD",RESPONSE="RESPONSE",SET_ADDRESS="SET_ADDRESS",CONNECT="CONNECT",DISCONNECT="DISCONNECT",POBOX="POBOX",POST="POST",COLLECT="COLLECT",CLOSEBOX="CLOSEBOX",BOXCLOSED="BOXCLOSED",PACKAGE="PACKAGE",PACK="PACK",DELIVERY="DELIVERY",_BLANK="_blank";class Deprecated{static registerWorker(e,s,t,n){e.port.onmessage=(e=>{
|
|
9
|
+
return t[n]=s(safeGetValueFromPropertyOnObject(e,n)),t},{});return t.pop(),r}var t=[];return s(e)}function main(e){return _PostalWorker||(_PostalWorker=new PostalWorker(e,index))}const POSTAL_WORKER="PostalWorker",POSTAL_SHARED_WORKER="PostalSharedWorker",POLYFILL_SHARED_WORKER="PolyfillSharedWorker",ON="ON",UN="UN",FIRE="FIRE",CROSSFIRE="CROSSFIRE",BACKFIRE="BACKFIRE",CHILDREGISTER="CHILDREGISTER",WORKERREGISTER="WORKERREGISTER",REGISTERID="REGISTERID",UNREGISTERID="UNREGISTERID",BROADCASTNODE="BROADCASTNODE",CROSSNODE="CROSSNODE",ERROR="ERROR",MESSAGE="message",SCRIPT="script",JS="js",LOAD="LOAD",RESPONSE="RESPONSE",SET_ADDRESS="SET_ADDRESS",CONNECT="CONNECT",DISCONNECT="DISCONNECT",POBOX="POBOX",POST="POST",COLLECT="COLLECT",CLOSEBOX="CLOSEBOX",BOXCLOSED="BOXCLOSED",PACKAGE="PACKAGE",PACK="PACK",DELIVERY="DELIVERY",_BLANK="_blank";class Deprecated{static registerWorker(e,s,t,n){e.port.onmessage=(e=>{
|
|
10
10
|
// Handle messages sent from worker by type
|
|
11
11
|
switch(e.data.type){case FIRE:// If the legacy messaging is used, it will still work here
|
|
12
12
|
// Send the message
|
|
@@ -93,7 +93,7 @@ class PostalWorker{/**
|
|
|
93
93
|
* @param configuration
|
|
94
94
|
* @param safeJsonStringify
|
|
95
95
|
*/
|
|
96
|
-
constructor(e,s){this.uniqueNumber=0,this.id=this._uniqueNumber(),this.workerOnline=!1,_config=e||!1,_stringify=s,
|
|
96
|
+
constructor(e,s){this.uniqueNumber=0,this.id=this._uniqueNumber(),this.sharedWorkerSupported=!1,this.workerOnline=!1,_config=e||!1,_stringify=s,
|
|
97
97
|
// Public API
|
|
98
98
|
PostalHelper.rootMethods(this),PostalHelper.utilityMethods(this),
|
|
99
99
|
// Attach global handlers
|
|
@@ -123,9 +123,7 @@ _getSubscriber(e){let s=e.split("://"),t=s[0];if(void 0!==s[1]){let e=s[1].split
|
|
|
123
123
|
*/
|
|
124
124
|
_resolveWorker(){
|
|
125
125
|
// Browser supports SharedWorker
|
|
126
|
-
let e=!!window.SharedWorker;
|
|
127
|
-
// Fallback on failure
|
|
128
|
-
return e=Boolean(e),e?(_worker=this._startSharedWorker())||(_worker=this._startDedicatedWorker()):_worker=this._startDedicatedWorker(),_worker}/**
|
|
126
|
+
let e=!!window.SharedWorker;return e=Boolean(e),e?(this.sharedWorkerSupported=!0,_worker=this._polyfillSharedWorker()):(this.sharedWorkerSupported=!1,_worker=this._polyfillSharedWorker()),_worker}/**
|
|
129
127
|
* Attempt to start SharedWorker thread or fail and return false
|
|
130
128
|
* @return {SharedWorker | boolean}
|
|
131
129
|
* @private
|
|
@@ -146,7 +144,27 @@ window.console.warn("PostalWorker - Unable to start SharedWorker"),window.consol
|
|
|
146
144
|
* Start a basic web worker (not available at this time)
|
|
147
145
|
* @private
|
|
148
146
|
*/
|
|
149
|
-
|
|
147
|
+
_polyfillSharedWorker(){let e,s=this._getPostalRoute();try{e=new Worker(s.concat("PolyfillSharedWorker").concat(".").concat(JS),{name:POSTAL_WORKER}),console.info(e),
|
|
148
|
+
// /* !!! DEPRECATED messaging route !!! but leaving in place as fallback for older browsers */
|
|
149
|
+
// // this stuff is no longer tested and does not work
|
|
150
|
+
// // (no more messaging, only the connection is being established here)
|
|
151
|
+
// // but leaving it in to polyfill older browsers potentially
|
|
152
|
+
// let OK = _stringify({
|
|
153
|
+
// postal: true,
|
|
154
|
+
// type: S.RESPONSE,
|
|
155
|
+
// id: this.id,
|
|
156
|
+
// status: true
|
|
157
|
+
// });
|
|
158
|
+
// Deprecated.registerWorker(worker, OK, _events, this);
|
|
159
|
+
// /* !!! PRIMARY messaging - This should always be used first */
|
|
160
|
+
_channel.onmessage=(e=>{
|
|
161
|
+
// Handle messages sent from worker by type
|
|
162
|
+
const s=JSON.parse(e.data);if(!0!==s.postal)return;// Not a postalWorker message
|
|
163
|
+
switch(s.type){case WORKERREGISTER:if(
|
|
164
|
+
// worker startup
|
|
165
|
+
this.workerOnline=!0,this._mapToBroadcastNetwork(this.id),_queue.length){const e=()=>_queue.length;for(;e();)_queue.shift()()}break;case FIRE:s.data.msgClass&&_events.get(s.data.msgClass)&&_events.get(s.data.msgClass)(s.data.message);break;case ERROR:console.error(s.data)}})}catch(e){
|
|
166
|
+
// todo: Add intelligent error handling, including destination options
|
|
167
|
+
window.console.warn("PostalWorker - Unable to start SharedWorker"),window.console.debug(e)}return e||!1}/**
|
|
150
168
|
* Resolve route to use to find the sharedworker file
|
|
151
169
|
* Priority is:
|
|
152
170
|
* 1. Configuration passed to library
|
package/dist/PostalWorker.min.js
CHANGED
|
@@ -1,19 +1,19 @@
|
|
|
1
1
|
/* eslint-disable */
|
|
2
|
-
!function(){"use strict";function e(e){return"[Throws: "+(e?e.message:"?")+"]"}function t(t,r){if(
|
|
2
|
+
!function(){"use strict";function e(e){return"[Throws: "+(e?e.message:"?")+"]"}function t(t,r){if(W.call(t,r))try{return t[r]}catch(t){return e(t)}return t[r]}function r(r){// store references to objects we have seen before
|
|
3
3
|
function o(r){if(null===r||"object"!=typeof r)return r;if(-1!==a.indexOf(r))return"[Circular]";if(a.push(r),"function"==typeof r.toJSON)try{var n=o(r.toJSON());return a.pop(),n}catch(t){return e(t)}if(Array.isArray(r)){var s=r.map(o);return a.pop(),s}var i=Object.keys(r).reduce(function(e,a){
|
|
4
4
|
// prevent faulty defined getter properties
|
|
5
5
|
return e[a]=o(t(r,a)),e},{});return a.pop(),i}var a=[];return o(r)}/**
|
|
6
6
|
* PostalWorker String Constants
|
|
7
7
|
* @type {string}
|
|
8
8
|
*/
|
|
9
|
-
var o="CHILDREGISTER",a="message",n=function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")},s=function(){function e(e,t){for(var r=0;r<t.length;r++){var o=t[r];o.enumerable=o.enumerable||!1,o.configurable=!0,"value"in o&&(o.writable=!0),Object.defineProperty(e,o.key,o)}}return function(t,r,o){return r&&e(t.prototype,r),o&&e(t,o),t}}(),i=function(){function e(e,t){var r=[],o=!0,a=!1,n=void 0;try{for(var s,i=e[Symbol.iterator]();!(o=(s=i.next()).done)&&(r.push(s.value),!t||r.length!==t);o=!0);}catch(e){a=!0,n=e}finally{try{!o&&i.return&&i.return()}finally{if(a)throw n}}return r}return function(t,r){if(Array.isArray(t))return t;if(Symbol.iterator in Object(t))return e(t,r);throw new TypeError("Invalid attempt to destructure non-iterable instance")}}(),
|
|
9
|
+
var o="CHILDREGISTER",a="message",n=function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")},s=function(){function e(e,t){for(var r=0;r<t.length;r++){var o=t[r];o.enumerable=o.enumerable||!1,o.configurable=!0,"value"in o&&(o.writable=!0),Object.defineProperty(e,o.key,o)}}return function(t,r,o){return r&&e(t.prototype,r),o&&e(t,o),t}}(),i=function(){function e(e,t){var r=[],o=!0,a=!1,n=void 0;try{for(var s,i=e[Symbol.iterator]();!(o=(s=i.next()).done)&&(r.push(s.value),!t||r.length!==t);o=!0);}catch(e){a=!0,n=e}finally{try{!o&&i.return&&i.return()}finally{if(a)throw n}}return r}return function(t,r){if(Array.isArray(t))return t;if(Symbol.iterator in Object(t))return e(t,r);throw new TypeError("Invalid attempt to destructure non-iterable instance")}}(),l=function(){function e(){n(this,e)}return s(e,null,[{key:"registerWorker",value:function(t,r,o,a){t.port.onmessage=function(t){
|
|
10
10
|
// Handle messages sent from worker by type
|
|
11
11
|
switch(t.data.type){case"FIRE":
|
|
12
12
|
// If the legacy messaging is used, it will still work here
|
|
13
13
|
// Send the message
|
|
14
14
|
t.data.data.msgClass&&o.get(t.data.data.msgClass)&&o.get(t.data.data.msgClass)(t.data.data.message),
|
|
15
15
|
// Let worker know message was received
|
|
16
|
-
t.currentTarget.postMessage(r);break;case"SET_ADDRESS":e.address=t.data.data;break;case"ERROR":console.error(t.data.data);break;default:console.warn(t)}}}}]),e}(),
|
|
16
|
+
t.currentTarget.postMessage(r);break;case"SET_ADDRESS":e.address=t.data.data;break;case"ERROR":console.error(t.data.data);break;default:console.warn(t)}}}}]),e}(),u=function(){function e(){n(this,e)}return s(e,null,[{key:"rootMethods",/**
|
|
17
17
|
* Primary PostalWorker public methods
|
|
18
18
|
* @param {*} module
|
|
19
19
|
*/
|
|
@@ -27,32 +27,32 @@ e._on("CONNECT",function(t){e._mapToBroadcastNetwork(t.id)}),e._crossOn("CONNECT
|
|
|
27
27
|
// console.info("crossOn CONNECT", msg);
|
|
28
28
|
var r=e,o=r._getOpeningWindows(),a=[];if(o){
|
|
29
29
|
// wins.size should be 1 but todo: support multiple?
|
|
30
|
-
var n=!0,s=!1,
|
|
30
|
+
var n=!0,s=!1,l=void 0;try{for(var u,c=o[Symbol.iterator]();!(n=(u=c.next()).done);n=!0){var d=i(u.value,2),p=d[0],f=d[1];r._mapToWindow(t.id,f,t.subscriber),a.push(p)}}catch(e){s=!0,l=e}finally{try{!n&&c.return&&c.return()}finally{if(s)throw l}}var h=!0,v=!1,g=void 0;try{for(var k,y=a[Symbol.iterator]();!(h=(k=y.next()).done);h=!0){var w=k.value;r._getOpeningWindows().delete(w)}}catch(e){v=!0,g=e}finally{try{!h&&y.return&&y.return()}finally{if(v)throw g}}}}),
|
|
31
31
|
// Disconnect
|
|
32
32
|
e._on("DISCONNECT",function(t){e._unmapFromWindow(t.id)}),e._crossOn("DISCONNECT",function(t){e._unmapFromWindow(t.id)})}},{key:"registerCollection",value:function(e){
|
|
33
33
|
// let last;
|
|
34
34
|
e._on("COLLECT",function(t){
|
|
35
35
|
// const serialized = JSON.stringify(msg);
|
|
36
36
|
//if (last !== serialized) {
|
|
37
|
-
e._collectBox(t)})}},{key:"unregisterCollection",value:function(e){e._un("COLLECT")}},{key:"registerClosure",value:function(e,t,r){var o=r;e._getEvents().get("BOXCLOSED")||e._on("BOXCLOSED",function(t){e._clearBox(t)}),e._lastCollection(t,o)}},{key:"registerDelivery",value:function(e){e._on("DELIVERY",function(t){e._delivery(t)})}}]),e}(),c=!1,d=void 0,p=!1,f=!1,
|
|
37
|
+
e._collectBox(t)})}},{key:"unregisterCollection",value:function(e){e._un("COLLECT")}},{key:"registerClosure",value:function(e,t,r){var o=r;e._getEvents().get("BOXCLOSED")||e._on("BOXCLOSED",function(t){e._clearBox(t)}),e._lastCollection(t,o)}},{key:"registerDelivery",value:function(e){e._on("DELIVERY",function(t){e._delivery(t)})}}]),e}(),c=!1,d=void 0,p=!1,f=!1,h=new Set,v=new Map,g=new Map,k=new Map,y=new Map,w=new Set,m=new BroadcastChannel("PostalWorker"),b=[],_=new Map,S=new Map,C=new Map,O=function(){/**
|
|
38
38
|
* Initialize object with configuration & setup the worker and listeners
|
|
39
39
|
* @param configuration
|
|
40
40
|
* @param safeJsonStringify
|
|
41
41
|
*/
|
|
42
|
-
function e(t,r){n(this,e),this.uniqueNumber=0,this.id=this._uniqueNumber(),this.workerOnline=!1,c=t||!1,d=r,
|
|
42
|
+
function e(t,r){n(this,e),this.uniqueNumber=0,this.id=this._uniqueNumber(),this.sharedWorkerSupported=!1,this.workerOnline=!1,c=t||!1,d=r,
|
|
43
43
|
// Public API
|
|
44
|
-
|
|
44
|
+
u.rootMethods(this),u.utilityMethods(this),
|
|
45
45
|
// Attach global handlers
|
|
46
|
-
|
|
46
|
+
u.attachHandlers(this),
|
|
47
47
|
// Does this window have a parent?
|
|
48
48
|
window.self!==window.top&&(
|
|
49
49
|
// If it does, include additional public methods and then register it
|
|
50
50
|
// and let it know this window is ready to receive messages
|
|
51
|
-
|
|
51
|
+
u.backMethod(this),f=this._getSubscriber(document.referrer),this._backFire(o)),
|
|
52
52
|
// Add event listener to incoming messages (from windows) and process with messageController
|
|
53
53
|
window.addEventListener(a,this._messageController),
|
|
54
54
|
// Subscribe to internal message classes
|
|
55
|
-
|
|
55
|
+
u.internalSubscriptions(this),
|
|
56
56
|
// Resolve worker threading
|
|
57
57
|
this._resolveWorker()}/**
|
|
58
58
|
* Get root URL for subscriptions
|
|
@@ -63,66 +63,84 @@ this._resolveWorker()}/**
|
|
|
63
63
|
*/
|
|
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
|
-
var e=!!window.SharedWorker;
|
|
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"),{name:"PostalWorker"});/* !!! DEPRECATED messaging route !!! but leaving in place as fallback for older browsers */
|
|
66
|
+
var e=!!window.SharedWorker;return e=Boolean(e),e?(this.sharedWorkerSupported=!0,p=this._polyfillSharedWorker()):(this.sharedWorkerSupported=!1,p=this._polyfillSharedWorker()),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
67
|
// this stuff is no longer tested and does not work
|
|
70
68
|
// (no more messaging, only the connection is being established here)
|
|
71
69
|
// but leaving it in to polyfill older browsers potentially
|
|
72
|
-
var o=d({postal:!0,type:"RESPONSE",id:this.id,status:!0});
|
|
70
|
+
var o=d({postal:!0,type:"RESPONSE",id:this.id,status:!0});l.registerWorker(t,o,v,this),/* !!! PRIMARY messaging - This should always be used first */
|
|
73
71
|
m.onmessage=function(t){
|
|
74
72
|
// Handle messages sent from worker by type
|
|
75
73
|
var r=JSON.parse(t.data);if(!0===r.postal)// Not a postalWorker message
|
|
76
74
|
switch(r.type){case"WORKERREGISTER":if(
|
|
77
75
|
// worker startup
|
|
78
|
-
e.workerOnline=!0,e._mapToBroadcastNetwork(e.id),b.length)for(;function(){return b.length}();)b.shift()();break;case"FIRE":r.data.msgClass&&
|
|
76
|
+
e.workerOnline=!0,e._mapToBroadcastNetwork(e.id),b.length)for(;function(){return b.length}();)b.shift()();break;case"FIRE":r.data.msgClass&&v.get(r.data.msgClass)&&v.get(r.data.msgClass)(r.data.message);break;case"ERROR":console.error(r.data)}}}catch(e){
|
|
79
77
|
// todo: Add intelligent error handling, including destination options
|
|
80
|
-
window.console.warn("PostalWorker - Unable to start SharedWorker"),window.console.debug(e)}return t||!1}},{key:"
|
|
78
|
+
window.console.warn("PostalWorker - Unable to start SharedWorker"),window.console.debug(e)}return t||!1}},{key:"_polyfillSharedWorker",value:function(){var e=this,t=void 0,r=this._getPostalRoute();try{t=new Worker(r.concat("PolyfillSharedWorker").concat(".").concat("js"),{name:"PostalWorker"}),console.info(t),
|
|
79
|
+
// /* !!! DEPRECATED messaging route !!! but leaving in place as fallback for older browsers */
|
|
80
|
+
// // this stuff is no longer tested and does not work
|
|
81
|
+
// // (no more messaging, only the connection is being established here)
|
|
82
|
+
// // but leaving it in to polyfill older browsers potentially
|
|
83
|
+
// let OK = _stringify({
|
|
84
|
+
// postal: true,
|
|
85
|
+
// type: S.RESPONSE,
|
|
86
|
+
// id: this.id,
|
|
87
|
+
// status: true
|
|
88
|
+
// });
|
|
89
|
+
// Deprecated.registerWorker(worker, OK, _events, this);
|
|
90
|
+
// /* !!! PRIMARY messaging - This should always be used first */
|
|
91
|
+
m.onmessage=function(t){
|
|
92
|
+
// Handle messages sent from worker by type
|
|
93
|
+
var r=JSON.parse(t.data);if(!0===r.postal)// Not a postalWorker message
|
|
94
|
+
switch(r.type){case"WORKERREGISTER":if(
|
|
95
|
+
// worker startup
|
|
96
|
+
e.workerOnline=!0,e._mapToBroadcastNetwork(e.id),b.length)for(;function(){return b.length}();)b.shift()();break;case"FIRE":r.data.msgClass&&v.get(r.data.msgClass)&&v.get(r.data.msgClass)(r.data.message);break;case"ERROR":console.error(r.data)}}}catch(e){
|
|
97
|
+
// todo: Add intelligent error handling, including destination options
|
|
98
|
+
window.console.warn("PostalWorker - Unable to start SharedWorker"),window.console.debug(e)}return t||!1}},{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
99
|
// What about if more than one postalworker is found?
|
|
82
100
|
// This should be pointed out...
|
|
83
101
|
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
102
|
// console.info(msg);
|
|
85
103
|
switch(t.type){case"CROSSFIRE":
|
|
86
104
|
// Is this a parent we don't yet know about?
|
|
87
|
-
|
|
105
|
+
y.has(t.id)||y.set(t.id,{win:e.source,subscriber:e.origin}),
|
|
88
106
|
// Is this window known by the module? If not, add it to the registry
|
|
89
|
-
|
|
107
|
+
y.has(t.id)||(y.set(t.id,{win:e.source,subscriber:e.origin}),h.add(e.origin)),
|
|
90
108
|
// Is this a cross event?
|
|
91
109
|
g.has(t.data.msgClass)&&
|
|
92
110
|
// If so, invoke registered callback against message
|
|
93
111
|
g.get(t.data.msgClass)(t.data.message);break;case"BACKFIRE":
|
|
94
112
|
// Children register themselves with the parent
|
|
95
|
-
t.data.msgClass===o?
|
|
113
|
+
t.data.msgClass===o?y.has(t.data.id)?(
|
|
96
114
|
// When a duplicate attempts to register, delete the previous resource
|
|
97
115
|
// This accounts for iframe reloading
|
|
98
|
-
|
|
99
|
-
|
|
116
|
+
y.delete(t.id),// _windows.delete(e.source);
|
|
117
|
+
y.set(t.id,{win:e.origin,subscriber:e.origin})):(
|
|
100
118
|
// if (!_windows.has(e.source)) {
|
|
101
|
-
|
|
102
|
-
|
|
119
|
+
y.set(t.id,{win:e.source,subscriber:e.origin}),//_windows.set(e.source, e.origin);
|
|
120
|
+
h.add(e.origin)):g.has(t.data.msgClass)&&
|
|
103
121
|
// Invoke registered callback
|
|
104
122
|
g.get(t.data.msgClass)(t.data.message);break;case"ERROR":window.console.error(t.data.message);break;default:window.console.error("PostalWorker - Unexpected message event"),window.console.debug({type:"_messageController",event:e})}}}},{key:"_on",value:function(e,t){if(e&&t){
|
|
105
123
|
// Send message to worker thread
|
|
106
124
|
var r=d({postal:!0,type:"ON",id:this.id,data:{msgClass:e,action:t}});
|
|
107
125
|
// Update registry
|
|
108
|
-
return p?m.postMessage(r):b.push(function(){return m.postMessage(r)}),
|
|
126
|
+
return p?m.postMessage(r):b.push(function(){return m.postMessage(r)}),v.set(e,t),this}}},{key:"_un",value:function(e){try{
|
|
109
127
|
// Send message to worker thread
|
|
110
128
|
var t=d({postal:!0,type:"UN",id:this.id,data:e});p?m.postMessage(t):b.push(function(){return m.postMessage(t)}),
|
|
111
129
|
// Update registry
|
|
112
|
-
|
|
130
|
+
v.delete(e)}catch(e){return window.console.error(e),!1}return!0}},{key:"_fire",value:function(e,t,r){var o=d({postal:!0,type:"FIRE",id:this.id,data:{msgClass:e,message:t,audience:r}});return this.workerOnline&&p?m.postMessage(o):b.push(function(){return m.postMessage(o)}),!0}},{key:"_crossOn",value:function(e,t,r,o,a){var n=void 0,s=void 0,i="",l="";if(r){
|
|
113
131
|
// It also cannot be the parent window as that is already taken
|
|
114
132
|
if(
|
|
115
133
|
// postMessage only needs the root protocol+domain to send messages
|
|
116
134
|
n=this._getSubscriber(r),f&&n!==f)return window.console.warn("PostalWorker - Unable to crossOn parentWindow"),this;if(
|
|
117
135
|
// Register subscription
|
|
118
|
-
!1===
|
|
136
|
+
!1===h.has(n)&&
|
|
119
137
|
// If subscriber doesn't exist
|
|
120
|
-
|
|
138
|
+
h.add(n),
|
|
121
139
|
// Open window for subscription
|
|
122
|
-
s=o||"_blank",a){var
|
|
140
|
+
s=o||"_blank",a){var u=!0,c=!1,d=void 0;try{for(var p,v=Object.entries(a)[Symbol.iterator]();!(u=(p=v.next()).done);u=!0){var y=p.value;l=l.concat(i+" "+y[0]+"="+y[1]),""===i&&(i=",")}}catch(e){c=!0,d=e}finally{try{!u&&v.return&&v.return()}finally{if(c)throw d}}k.set(n,window.open(r,s,l))}else
|
|
123
141
|
// Open window (basic)
|
|
124
142
|
// _windows.set(window.open(subscriber, winName), rootSubscriber);
|
|
125
|
-
|
|
143
|
+
k.set(n,window.open(r,s));
|
|
126
144
|
// Register associated action in registry
|
|
127
145
|
g.set(e,t)}else
|
|
128
146
|
// No subscriber means to ONLY listen but not to open the window reference
|
|
@@ -133,17 +151,17 @@ g.delete(e),t){
|
|
|
133
151
|
// Get root subscriber
|
|
134
152
|
var r=this._getSubscriber(t);
|
|
135
153
|
// Remove from subscriptions
|
|
136
|
-
|
|
154
|
+
h.delete(r);
|
|
137
155
|
// If a reference to a window exists
|
|
138
|
-
var o=[],a=!0,n=!1,s=void 0;try{for(var u
|
|
156
|
+
var o=[],a=!0,n=!1,s=void 0;try{for(var l,u=y[Symbol.iterator]();!(a=(l=u.next()).done);a=!0){var c=i(l.value,2),d=c[0],p=c[1];p.subscriber===this._getSubscriber(t)&&(p.win.close(),o.push(d))}}catch(e){n=!0,s=e}finally{try{!a&&u.return&&u.return()}finally{if(n)throw s}}o.forEach(function(e){y.delete(e)})}return this}},{key:"_crossFire",value:function(e,t){var r=d({postal:!0,type:"CROSSFIRE",id:this.id,data:{msgClass:e,message:t}});if(0===y.size&&window.opener&&window.opener.postMessage(r,"*"),
|
|
139
157
|
// Parent window is part of the crossFire group
|
|
140
|
-
f&&window.top.postMessage(r,f),
|
|
158
|
+
f&&window.top.postMessage(r,f),y.size>0){var o=!0,a=!1,n=void 0;try{for(var s,l=y[Symbol.iterator]();!(o=(s=l.next()).done);o=!0){var u=i(s.value,2)[1];u.win.postMessage(r,u.subscriber)}}catch(e){a=!0,n=e}finally{try{!o&&l.return&&l.return()}finally{if(a)throw n}}}return this}},{key:"_fireAll",value:function(e,t){return this._fire(e,t),this._crossFire(e,t),this}},{key:"_backFire",value:function(e,t){if(f&&e){var r=d({postal:!0,type:"BACKFIRE",id:this.id,data:{msgClass:e,message:t}});try{window.top.postMessage(r,f)}catch(e){window.console.warn("PostalWorker - Unable to backFire"),window.console.debug({msg:r,parentWindow:f,error:e})}}return this}},{key:"_load",value:function(e){var t=d({postal:!0,type:"LOAD",id:this.id,data:e});this.workerOnline?p?
|
|
141
159
|
// Otherwise, send the message to load the library
|
|
142
160
|
m.postMessage(t):b.push(function(){return m.postMessage(t)}):
|
|
143
161
|
// If the worker has not completed startup yet, queue the message
|
|
144
|
-
b.push(function(){m.postMessage(t)})}},{key:"_getOpeningWindows",value:function(){return
|
|
162
|
+
b.push(function(){m.postMessage(t)})}},{key:"_getOpeningWindows",value:function(){return k}},{key:"_getWindows",value:function(){return y}},{key:"_getCrossEvents",value:function(){return g}},{key:"_getSubscriptions",value:function(){return h}},{key:"_getWindowsBySubscriber",value:function(e){var t=[],r=!0,o=!1,a=void 0;try{for(var n,s=y[Symbol.iterator]();!(r=(n=s.next()).done);r=!0){var l=i(n.value,2)[1];e===l.subscriber&&t.push(l)}}catch(e){o=!0,a=e}finally{try{!r&&s.return&&s.return()}finally{if(o)throw a}}return t}},{key:"_getEvents",value:function(){return v}},{key:"_uniqueNumber",value:function(){var e=Date.now();return e<=this._uniqueNumber.previous?e=++this._uniqueNumber.previous:this._uniqueNumber.previous=e,e}},{key:"_mapToWindow",value:function(e,t,r){y.set(e,{win:t,subscriber:r}),m.postMessage(d({postal:!0,type:"REGISTERID",id:e,nodeType:"CROSSNODE"}))}},{key:"_unmapFromWindow",value:function(e){y.delete(e),m.postMessage(d({postal:!0,type:"UNREGISTERID",id:e}))}},{key:"_mapToBroadcastNetwork",value:function(e){w.add(e),m.postMessage(d({postal:!0,type:"REGISTERID",id:e,nodeType:"BROADCASTNODE"}))}},{key:"_unmapFromBroadcastNetwork",value:function(e){w.delete(e),m.postMessage(d({postal:!0,type:"UNREGISTERID",id:e}))}},{key:"_postBox",value:function(e,t){var r=d({postal:!0,type:"POBOX",id:this.id,address:e});this.workerOnline?p&&m.postMessage(r):b.push(function(){return m.postMessage(r)}),_.size||u.registerCollection(this),_.get(e)||_.set(e,t)}},{key:"_closeBox",value:function(e){var t=_.get(e);u.registerClosure(this,e,t),_.delete(e),m.postMessage(d({postal:!0,type:"CLOSEBOX",id:this.id,address:e}))}},{key:"_post",value:function(e,t){var r=d({postal:!0,type:"POST",id:this.id,address:e,value:t});this.workerOnline&&p?m.postMessage(r):b.push(function(){return m.postMessage(r)})}},{key:"_collectBox",value:function(e){var t=e.address,r=e.value,o=e.box,a=_.get(t);a&&a({address:t,value:r,box:o})}},{key:"_lastCollection",value:function(e,t){S.set(e,t)}},{key:"_clearBox",value:function(e){var t=e.id,r=e.address,o=e.box;if(t===this.id){if(S.get(r)){S.get(r)({address:r,value:null,box:o}),S.delete(r)}_.delete(r),S.size||_.size||u.unregisterCollection(this)}}},{key:"_package",value:function(e,t){var r=d({postal:!0,type:"PACKAGE",id:this.id,address:e});this.workerOnline?p&&m.postMessage(r):b.push(function(){return m.postMessage(r)}),C.size||u.registerDelivery(this),C.get(e)||C.set(e,t)}},{key:"_pack",value:function(e,t){var r=new XMLHttpRequest,o=this;r.onload=function(){var t=new FileReader;t.onloadend=function(){var r=d({postal:!0,type:"PACK",id:o.id,address:e,content:t.result});o.workerOnline?p&&m.postMessage(r):b.push(function(){m.postMessage(r)})},t.readAsDataURL(r.response)},r.open("GET",t),r.responseType="blob",r.send()}},{key:"_delivery",value:function(e){var t=e.address,r=e.content,o=C.get(t);o&&o({address:t,content:r})}}]),e}(),W=Object.prototype.hasOwnProperty,E=function(e,t,o){return JSON.stringify(r(e),t,o)},R=r;E.ensureProperties=R;/**
|
|
145
163
|
* PostalWorker Main Source
|
|
146
164
|
* (Legacy Version - Adds global PostalWorker method to window global scope)
|
|
147
165
|
* @Author: Russ Stratfull - 2018 - 2023
|
|
148
166
|
*/
|
|
149
|
-
var
|
|
167
|
+
var x=void 0;!function(){x=new O({},E)}(),window.PostalWorker=function(){return x}}();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "postalworker-es",
|
|
3
|
-
"version": "4.0.
|
|
3
|
+
"version": "4.0.5",
|
|
4
4
|
"description": "A JavaScript WebWorker & postMessage library that connects UI elements, Web Workers, & Cross-Site Windows",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"module": "dist/PostalWorker.es.js",
|
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
"types": "dist/index.d.ts",
|
|
13
13
|
"scripts": {
|
|
14
14
|
"build": "rollup -c rollup.es.config.js && rollup -c rollup.min.config.js",
|
|
15
|
-
"build-worker": "rollup -c rollup.worker.config.js",
|
|
15
|
+
"build-worker": "rollup -c rollup.worker.config.js && rollup -c rollup.polyfill.config.js",
|
|
16
16
|
"_transpile-test": "rollup -c rollup.test.js",
|
|
17
17
|
"test": "npm run _transpile-test && mocha",
|
|
18
18
|
"coverage": "cross-env NODE_ENV=test nyc npm test",
|