postalworker-es 4.0.8 → 5.0.0
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/README.md +152 -35
- package/dist/PostalSharedWorker.js +305 -513
- package/dist/PostalSharedWorker.js.map +7 -0
- package/dist/PostalWorker.min.js +2 -171
- package/dist/PostalWorker.min.js.map +7 -0
- package/dist/compat.d.ts +53 -0
- package/dist/core.d.ts +104 -0
- package/dist/cross.d.ts +32 -0
- package/dist/emitter.d.ts +15 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.js +860 -0
- package/dist/index.js.map +7 -0
- package/dist/protocol.d.ts +125 -0
- package/dist/types.d.ts +77 -0
- package/dist/worker-script.d.ts +44 -0
- package/dist/worker-script.js +8 -0
- package/dist/worker-script.js.map +7 -0
- package/package.json +47 -71
- package/dist/PolyfillSharedWorker.js +0 -525
- package/dist/PostalWorker.es.js +0 -408
package/dist/PostalWorker.es.js
DELETED
|
@@ -1,408 +0,0 @@
|
|
|
1
|
-
/* eslint-disable */
|
|
2
|
-
/**
|
|
3
|
-
* PostalWorker String Constants
|
|
4
|
-
* @type {string}
|
|
5
|
-
*/
|
|
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
|
-
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
|
-
// 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",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
|
-
// Handle messages sent from worker by type
|
|
11
|
-
switch(e.data.type){case FIRE:// If the legacy messaging is used, it will still work here
|
|
12
|
-
// Send the message
|
|
13
|
-
e.data.data.msgClass&&t.get(e.data.data.msgClass)&&t.get(e.data.data.msgClass)(e.data.data.message),
|
|
14
|
-
// Let worker know message was received
|
|
15
|
-
e.currentTarget.postMessage(s);break;case SET_ADDRESS:Deprecated.address=e.data.data;break;case ERROR:console.error(e.data.data);break;default:console.warn(e)}})}}/**
|
|
16
|
-
* Postal Helper defines the public API methods and creates window event handlers
|
|
17
|
-
* for the postal worker and add/remove internal postal subscriptions used by the library
|
|
18
|
-
* @author: Russ Stratfull
|
|
19
|
-
*/
|
|
20
|
-
class PostalHelper{/**
|
|
21
|
-
* Primary PostalWorker public methods
|
|
22
|
-
* @param {*} module
|
|
23
|
-
*/
|
|
24
|
-
static rootMethods(e){e.on=((s,t)=>e._on(s,t)),e.on.example=`PostalWorker().on('example', (msg) => console.info(msg))`,e.un=(s=>e._un(s)),e.un.example=`PostalWorker().un('example')`,e.fire=((s,t,n)=>e._fire(s,t,n)),e.fire.example=`PostalWorker().fire('example', 'Hello world')`,e.crossOn=((s,t,n,o,r)=>e._crossOn(s,t,n,o,r)),e.crossOn.example=`PostalWorker().crossOn('example', (msg) => console.info(msg), 'http://example.com', 'Example Name', )`,e.unCross=((s,t)=>e._unCross(s,t)),e.unCross.example=`PostalWorker().unCross('example', 'http://example.com')`,e.crossFire=((s,t)=>e._crossFire(s,t)),e.crossFire.example=`PostalWorker().crossFire('example', 'message to all windows/tabs cross launched')`,e.fireAll=((s,t)=>e._fireAll(s,t)),e.fireAll.example=`PostalWorker().fireAll('example', 'message to ALL windows/tabs/workers')`,e.load=(s=>e._load(s)),e.load.example=`PostalWorker().load('path/to/script.js')`,e.postBox=((s,t)=>e._postBox(s,t)),e.postBox.example=`PostalWorker().PObox('example', function(value) { console.log(value); })`,e.post=((s,t)=>e._post(s,t)),e.post.example=`PostalWorker().post('example', 'This is an example value')`,e.closeBox=(s=>e._closeBox(s)),e.closeBox.example=`PostalWorker().closeBox("example")`,e.package=((s,t)=>e._package(s,t)),e.package.example=`PostalWorker().package('example', (todo) => {})`,e.pack=((s,t)=>e._pack(s,t)),e.pack.example=`PostalWorker().pack('example', 'url/of/file.jpg')`}/**
|
|
25
|
-
* Special method added when script detects that the current window is an iframe
|
|
26
|
-
* @param {*} module
|
|
27
|
-
*/
|
|
28
|
-
static backMethod(e){e.backFire=((s,t)=>e._backFire(s,t)),e.backFire.example=`PostalWorker().backfire('example', 'Message to parent window')`}/**
|
|
29
|
-
* Utility methods which in the future could maybe be removed from public access
|
|
30
|
-
* @param {*} module
|
|
31
|
-
*/
|
|
32
|
-
static utilityMethods(e){}/**
|
|
33
|
-
* Attach global window event handlers needed by PostalWorker
|
|
34
|
-
* @param {*} module
|
|
35
|
-
*/
|
|
36
|
-
static attachHandlers(e){
|
|
37
|
-
// Beforewindow unloads, announce disconnect to community
|
|
38
|
-
window.addEventListener("beforeunload",()=>{const s=e;s._unmapFromBroadcastNetwork(s.id);s._fireAll(DISCONNECT,{id:s.id,subscriber:s._getSubscriber(window.location.href)})}),
|
|
39
|
-
// When window loads, announce connection to community
|
|
40
|
-
window.addEventListener("load",()=>{const s=e;s._fireAll(CONNECT,{id:s.id,subscriber:s._getSubscriber(window.location.href)})})}/**
|
|
41
|
-
* Establish internal usage of PostalWorker to create connect, disconnect, msgClasses
|
|
42
|
-
* @param {*} module
|
|
43
|
-
*/
|
|
44
|
-
static internalSubscriptions(e){
|
|
45
|
-
// Connect
|
|
46
|
-
e._on(CONNECT,s=>{
|
|
47
|
-
// console.info("CONNECT", msg);
|
|
48
|
-
const t=e;t._mapToBroadcastNetwork(s.id)}),e._crossOn(CONNECT,s=>{
|
|
49
|
-
// console.info("crossOn CONNECT", msg);
|
|
50
|
-
const t=e;let n=t._getOpeningWindows();//.get(msg.subscriber);
|
|
51
|
-
let o=[];if(n){
|
|
52
|
-
// wins.size should be 1 but todo: support multiple?
|
|
53
|
-
for(let[e,r]of n)t._mapToWindow(s.id,r,s.subscriber),o.push(e);for(let e of o)t._getOpeningWindows().delete(e)}}),
|
|
54
|
-
// Disconnect
|
|
55
|
-
e._on(DISCONNECT,s=>{
|
|
56
|
-
// console.info("DISCONNECT", msg);
|
|
57
|
-
const t=e;t._unmapFromWindow(s.id)}),e._crossOn(DISCONNECT,s=>{
|
|
58
|
-
// console.info("crossOn: DISCONNECT", msg);
|
|
59
|
-
const t=e;t._unmapFromWindow(s.id)})}/**
|
|
60
|
-
* Subscribe to postbox collection msgClass to get updates
|
|
61
|
-
* @param {*} module
|
|
62
|
-
*/
|
|
63
|
-
static registerCollection(e){
|
|
64
|
-
// let last;
|
|
65
|
-
e._on(COLLECT,s=>{const t=e;
|
|
66
|
-
// const serialized = JSON.stringify(msg);
|
|
67
|
-
//if (last !== serialized) {
|
|
68
|
-
t._collectBox(s)})}/**
|
|
69
|
-
* Unregister postbox collection msgClass
|
|
70
|
-
* @param {*} module
|
|
71
|
-
*/
|
|
72
|
-
static unregisterCollection(e){e._un(COLLECT)}/**
|
|
73
|
-
* Register for temporary msgClass to get final update when postbox is being closed
|
|
74
|
-
* @param {*} module
|
|
75
|
-
* @param {*} add
|
|
76
|
-
* @param {*} collect
|
|
77
|
-
*/
|
|
78
|
-
static registerClosure(e,s,t){const n=t;e._getEvents().get(BOXCLOSED)||e._on(BOXCLOSED,s=>{const t=e;t._clearBox(s)}),e._lastCollection(s,n)}/**
|
|
79
|
-
* Register for msgClass to receive package updates and deliver them
|
|
80
|
-
* @param {*} module
|
|
81
|
-
*/
|
|
82
|
-
static registerDelivery(e){e._on(DELIVERY,s=>{const t=e;t._delivery(s)})}}/**
|
|
83
|
-
* PostalWorker Post Messenger Event Bus Module (ES6)
|
|
84
|
-
* @description: Listen for and broadcast out messages by "message class"
|
|
85
|
-
* between windows/tabs & web workers using the postMessage API
|
|
86
|
-
* @Author: Russ Stratfull
|
|
87
|
-
* @Contributors: Sakshi Dheer, & François Wauquier
|
|
88
|
-
*/
|
|
89
|
-
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
|
-
// Define the PostalWorker
|
|
91
|
-
class PostalWorker{/**
|
|
92
|
-
* Initialize object with configuration & setup the worker and listeners
|
|
93
|
-
* @param configuration
|
|
94
|
-
* @param safeJsonStringify
|
|
95
|
-
*/
|
|
96
|
-
constructor(e,s){this.uniqueNumber=0,this.id=this._uniqueNumber(),this.sharedWorkerSupported=!1,this.workerOnline=!1,_config=e||!1,_stringify=s,
|
|
97
|
-
// Public API
|
|
98
|
-
PostalHelper.rootMethods(this),PostalHelper.utilityMethods(this),
|
|
99
|
-
// Attach global handlers
|
|
100
|
-
PostalHelper.attachHandlers(this),
|
|
101
|
-
// Does this window have a parent?
|
|
102
|
-
window.self!==window.top&&(
|
|
103
|
-
// If it does, include additional public methods and then register it
|
|
104
|
-
// and let it know this window is ready to receive messages
|
|
105
|
-
PostalHelper.backMethod(this),_parentWindow=this._getSubscriber(document.referrer),this._backFire(CHILDREGISTER)),
|
|
106
|
-
// Add event listener to incoming messages (from windows) and process with messageController
|
|
107
|
-
window.addEventListener(MESSAGE,this._messageController),
|
|
108
|
-
// Subscribe to internal message classes
|
|
109
|
-
PostalHelper.internalSubscriptions(this),
|
|
110
|
-
// Resolve worker threading
|
|
111
|
-
this._resolveWorker()}/**
|
|
112
|
-
* Get root URL for subscriptions
|
|
113
|
-
* (postMessage treats ALL urls the same off the same root)
|
|
114
|
-
* @param url
|
|
115
|
-
* @return {*}
|
|
116
|
-
* @private
|
|
117
|
-
*/
|
|
118
|
-
_getSubscriber(e){let s=e.split("://"),t=s[0];if(void 0!==s[1]){let e=s[1].split("/")[0];return t.concat("://").concat(e)}return e}/**
|
|
119
|
-
* Resolve what type of worker threading is available
|
|
120
|
-
* todo: this is very incomplete
|
|
121
|
-
* at the moment it only sets up the sharedworker - which is the priority in all cases anyways
|
|
122
|
-
* @private
|
|
123
|
-
*/
|
|
124
|
-
_resolveWorker(){
|
|
125
|
-
// Browser supports SharedWorker
|
|
126
|
-
let e=!!window.SharedWorker;
|
|
127
|
-
// TMP to work on dedicated worker
|
|
128
|
-
// _worker = this._polyfillSharedWorker();
|
|
129
|
-
// Fallback on failure
|
|
130
|
-
return e=Boolean(e),e?(this.sharedWorkerSupported=!0,(_worker=this._startSharedWorker())||(_worker=this._polyfillSharedWorker())):(this.sharedWorkerSupported=!1,_worker=this._polyfillSharedWorker()),_worker}/**
|
|
131
|
-
* Attempt to start SharedWorker thread or fail and return false
|
|
132
|
-
* @return {SharedWorker | boolean}
|
|
133
|
-
* @private
|
|
134
|
-
*/
|
|
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 */
|
|
136
|
-
// this stuff is no longer tested and does not work
|
|
137
|
-
// (no more messaging, only the connection is being established here)
|
|
138
|
-
// but leaving it in to polyfill older browsers potentially
|
|
139
|
-
let t=_stringify({postal:!0,type:RESPONSE,id:this.id,status:!0});Deprecated.registerWorker(e,t,_events,this),/* !!! PRIMARY messaging - This should always be used first */
|
|
140
|
-
_channel.onmessage=(e=>{
|
|
141
|
-
// Handle messages sent from worker by type
|
|
142
|
-
const s=JSON.parse(e.data);if(!0!==s.postal)return;// Not a postalWorker message
|
|
143
|
-
switch(s.type){case WORKERREGISTER:if(
|
|
144
|
-
// worker startup
|
|
145
|
-
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){
|
|
146
|
-
// todo: Add intelligent error handling, including destination options
|
|
147
|
-
window.console.warn("PostalWorker - Unable to start SharedWorker"),window.console.debug(e)}return e||!1}/**
|
|
148
|
-
* Start a basic web worker (not available at this time)
|
|
149
|
-
* @private
|
|
150
|
-
*/
|
|
151
|
-
_polyfillSharedWorker(){let e,s=this._getPostalRoute();try{e=new Worker(s.concat("PolyfillSharedWorker").concat(".").concat(JS),{name:POSTAL_WORKER}),
|
|
152
|
-
// /* !!! DEPRECATED messaging route !!! but leaving in place as fallback for older browsers */
|
|
153
|
-
// // this stuff is no longer tested and does not work
|
|
154
|
-
// // (no more messaging, only the connection is being established here)
|
|
155
|
-
// // but leaving it in to polyfill older browsers potentially
|
|
156
|
-
// let OK = _stringify({
|
|
157
|
-
// postal: true,
|
|
158
|
-
// type: S.RESPONSE,
|
|
159
|
-
// id: this.id,
|
|
160
|
-
// status: true
|
|
161
|
-
// });
|
|
162
|
-
// Deprecated.registerWorker(worker, OK, _events, this);
|
|
163
|
-
// /* !!! PRIMARY messaging - This should always be used first */
|
|
164
|
-
_channel.onmessage=(e=>{
|
|
165
|
-
// Handle messages sent from worker by type
|
|
166
|
-
const s=JSON.parse(e.data);if(!0!==s.postal)return;// Not a postalWorker message
|
|
167
|
-
switch(s.type){case WORKERREGISTER:if(
|
|
168
|
-
// worker startup
|
|
169
|
-
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){
|
|
170
|
-
// todo: Add intelligent error handling, including destination options
|
|
171
|
-
window.console.warn("PostalWorker - Unable to start SharedWorker"),window.console.debug(e)}return e||!1}/**
|
|
172
|
-
* Resolve route to use to find the sharedworker file
|
|
173
|
-
* Priority is:
|
|
174
|
-
* 1. Configuration passed to library
|
|
175
|
-
* 2. Find any script tag in document which is called PostalWorker and then see if PostalRoute is defined in the querystring
|
|
176
|
-
* 3. Assume the file is adjacent to where this script is being run from
|
|
177
|
-
* @return {String}
|
|
178
|
-
* @private
|
|
179
|
-
*/
|
|
180
|
-
_getPostalRoute(){let e=Array.from(document.querySelectorAll(SCRIPT)).filter(e=>e.src?e.src.match(POSTAL_WORKER):[]).filter(e=>null!==e);if(
|
|
181
|
-
// What about if more than one postalworker is found?
|
|
182
|
-
// This should be pointed out...
|
|
183
|
-
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=","")}
|
|
184
|
-
// Otherwise, look next to where the PostalWorker script is located
|
|
185
|
-
return 1===e.length?e[0].src.replace(/PostalWorker\.min\.js.*$/,"").replace(/PostalWorker\.js.*$/,""):""}/**
|
|
186
|
-
* Process window messaging events "data" by "type"
|
|
187
|
-
* @param e { data: JSON{ id: number, msgClass: string, message: any }, id: number, origin: string, source: port }
|
|
188
|
-
* @private
|
|
189
|
-
*/
|
|
190
|
-
_messageController(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"))){let s=JSON.parse(e.data);if(s.postal&&!0===s.postal)// Not a postalWorker message
|
|
191
|
-
// console.info(msg);
|
|
192
|
-
switch(s.type){case CROSSFIRE:
|
|
193
|
-
// Is this a parent we don't yet know about?
|
|
194
|
-
_windows.has(s.id)||_windows.set(s.id,{win:e.source,subscriber:e.origin}),
|
|
195
|
-
// Is this window known by the module? If not, add it to the registry
|
|
196
|
-
_windows.has(s.id)||(_windows.set(s.id,{win:e.source,subscriber:e.origin}),_subscriptions.add(e.origin)),
|
|
197
|
-
// Is this a cross event?
|
|
198
|
-
_crossEvents.has(s.data.msgClass)&&
|
|
199
|
-
// If so, invoke registered callback against message
|
|
200
|
-
_crossEvents.get(s.data.msgClass)(s.data.message);break;case BACKFIRE:
|
|
201
|
-
// Children register themselves with the parent
|
|
202
|
-
s.data.msgClass===CHILDREGISTER?_windows.has(s.data.id)?(
|
|
203
|
-
// When a duplicate attempts to register, delete the previous resource
|
|
204
|
-
// This accounts for iframe reloading
|
|
205
|
-
_windows.delete(s.id),// _windows.delete(e.source);
|
|
206
|
-
_windows.set(s.id,{win:e.origin,subscriber:e.origin})):(
|
|
207
|
-
// if (!_windows.has(e.source)) {
|
|
208
|
-
_windows.set(s.id,{win:e.source,subscriber:e.origin}),//_windows.set(e.source, e.origin);
|
|
209
|
-
_subscriptions.add(e.origin)):_crossEvents.has(s.data.msgClass)&&
|
|
210
|
-
// Invoke registered callback
|
|
211
|
-
_crossEvents.get(s.data.msgClass)(s.data.message);break;case ERROR:window.console.error(s.data.message);break;default:window.console.error("PostalWorker - Unexpected message event"),window.console.debug({type:"_messageController",event:e})}}}/**
|
|
212
|
-
* Register for events
|
|
213
|
-
* @param msgClass
|
|
214
|
-
* @param action
|
|
215
|
-
* @return {PostalWorker}
|
|
216
|
-
*/
|
|
217
|
-
_on(e,s){if(e&&s){
|
|
218
|
-
// Send message to worker thread
|
|
219
|
-
let t=_stringify({postal:!0,type:ON,id:this.id,data:{msgClass:e,action:s}});
|
|
220
|
-
// Update registry
|
|
221
|
-
return _worker?_channel.postMessage(t):_queue.push(()=>_channel.postMessage(t)),_events.set(e,s),this}}/**
|
|
222
|
-
* Unregister event
|
|
223
|
-
* @param msgClass
|
|
224
|
-
* @return {Boolean}
|
|
225
|
-
*/
|
|
226
|
-
_un(e){try{
|
|
227
|
-
// Send message to worker thread
|
|
228
|
-
let s=_stringify({postal:!0,type:UN,id:this.id,data:e});_worker?_channel.postMessage(s):_queue.push(()=>_channel.postMessage(s)),
|
|
229
|
-
// Update registry
|
|
230
|
-
_events.delete(e)}catch(e){return window.console.error(e),!1}return!0}/**
|
|
231
|
-
* Fire event
|
|
232
|
-
* @param msgClass
|
|
233
|
-
* @param msg
|
|
234
|
-
* @param audience - not being used currently
|
|
235
|
-
* @return {PostalWorker}
|
|
236
|
-
*/
|
|
237
|
-
_fire(e,s,t){let n=_stringify({postal:!0,type:FIRE,id:this.id,data:{msgClass:e,message:s,audience:t}});return this.workerOnline&&_worker?_channel.postMessage(n):_queue.push(()=>_channel.postMessage(n)),!0}/**
|
|
238
|
-
* Register for cross launched windows or iframes messages
|
|
239
|
-
* @param msgClass {string}
|
|
240
|
-
* @param action {function}
|
|
241
|
-
* @param subscriber {string}
|
|
242
|
-
* @param name {string}
|
|
243
|
-
* @param windowparams {WindowParameters}
|
|
244
|
-
* @return {PostalWorker}
|
|
245
|
-
*/
|
|
246
|
-
_crossOn(e,s,t,n,o){let r,i,a="",c="";if(t){
|
|
247
|
-
// It also cannot be the parent window as that is already taken
|
|
248
|
-
if(
|
|
249
|
-
// postMessage only needs the root protocol+domain to send messages
|
|
250
|
-
r=this._getSubscriber(t),_parentWindow&&r!==_parentWindow)return window.console.warn("PostalWorker - Unable to crossOn parentWindow"),this;if(
|
|
251
|
-
// Register subscription
|
|
252
|
-
!1===_subscriptions.has(r)&&
|
|
253
|
-
// If subscriber doesn't exist
|
|
254
|
-
_subscriptions.add(r),
|
|
255
|
-
// Open window for subscription
|
|
256
|
-
i=n||_BLANK,o){for(const e of Object.entries(o))c=c.concat(`${a} ${e[0]}=${e[1]}`),""===a&&(a=",");
|
|
257
|
-
// Open window (with extra params)
|
|
258
|
-
// debug: console.info(`window.open(${subscriber}, ${winName}, ${params})`);
|
|
259
|
-
// _windows.set(window.open(subscriber, winName, params), rootSubscriber);
|
|
260
|
-
_openingWindows.set(r,window.open(t,i,c))}else
|
|
261
|
-
// Open window (basic)
|
|
262
|
-
// _windows.set(window.open(subscriber, winName), rootSubscriber);
|
|
263
|
-
_openingWindows.set(r,window.open(t,i));
|
|
264
|
-
// Register associated action in registry
|
|
265
|
-
_crossEvents.set(e,s)}else
|
|
266
|
-
// No subscriber means to ONLY listen but not to open the window reference
|
|
267
|
-
_crossEvents.set(e,s);return this}/**
|
|
268
|
-
* Unregister cross launch/iframes
|
|
269
|
-
* @param msgClass
|
|
270
|
-
* @param subscriber
|
|
271
|
-
* @return {PostalWorker}
|
|
272
|
-
*/
|
|
273
|
-
_unCross(e,s){// todo: this should check if any other subscribers are using it...
|
|
274
|
-
if(
|
|
275
|
-
// Remove from cross events registry
|
|
276
|
-
_crossEvents.delete(e),s){
|
|
277
|
-
// Get root subscriber
|
|
278
|
-
let e=this._getSubscriber(s);
|
|
279
|
-
// Remove from subscriptions
|
|
280
|
-
_subscriptions.delete(e);
|
|
281
|
-
// If a reference to a window exists
|
|
282
|
-
let t=[];for(let[e,n]of _windows)n.subscriber===this._getSubscriber(s)&&(n.win.close(),t.push(e));t.forEach(e=>{_windows.delete(e)})}return this}/**
|
|
283
|
-
* Broadcast/fire across the windows/frames
|
|
284
|
-
* @param msgClass
|
|
285
|
-
* @param msg
|
|
286
|
-
* @return {PostalWorker}
|
|
287
|
-
*/
|
|
288
|
-
_crossFire(e,s){let t=_stringify({postal:!0,type:CROSSFIRE,id:this.id,data:{msgClass:e,message:s}});if(0===_windows.size&&window.opener&&window.opener.postMessage(t,"*"),
|
|
289
|
-
// Parent window is part of the crossFire group
|
|
290
|
-
_parentWindow&&window.top.postMessage(t,_parentWindow),_windows.size>0)for(let[,e]of _windows)e.win.postMessage(t,e.subscriber);return this}/**
|
|
291
|
-
* Fire to all the things!
|
|
292
|
-
* @param msgClass
|
|
293
|
-
* @param msg
|
|
294
|
-
* @return {PostalWorker}
|
|
295
|
-
*/
|
|
296
|
-
_fireAll(e,s){return this._fire(e,s),this._crossFire(e,s),this}/**
|
|
297
|
-
* Special fire type that is specifically from child to parent window
|
|
298
|
-
* @param msgClass
|
|
299
|
-
* @param msg
|
|
300
|
-
* @return {PostalWorker}
|
|
301
|
-
*/
|
|
302
|
-
_backFire(e,s){if(_parentWindow&&e){let t=_stringify({postal:!0,type:BACKFIRE,id:this.id,data:{msgClass:e,message:s}});try{window.top.postMessage(t,_parentWindow)}catch(e){window.console.warn("PostalWorker - Unable to backFire"),window.console.debug({msg:t,parentWindow:_parentWindow,error:e})}}return this}/**
|
|
303
|
-
* Load JavaScript library into worker thread
|
|
304
|
-
* todo: @Russ - support for multiple threads...
|
|
305
|
-
* @param library
|
|
306
|
-
*/
|
|
307
|
-
_load(e){const s=_stringify({postal:!0,type:LOAD,id:this.id,data:e});this.workerOnline?_worker?
|
|
308
|
-
// Otherwise, send the message to load the library
|
|
309
|
-
_channel.postMessage(s):_queue.push(()=>_channel.postMessage(s)):
|
|
310
|
-
// If the worker has not completed startup yet, queue the message
|
|
311
|
-
_queue.push(()=>{_channel.postMessage(s)})}/**
|
|
312
|
-
*
|
|
313
|
-
* @returns
|
|
314
|
-
*/
|
|
315
|
-
_getOpeningWindows(){return _openingWindows}/**
|
|
316
|
-
* Get current window references registered with postal system
|
|
317
|
-
* @return {Map<any, any>}
|
|
318
|
-
*/
|
|
319
|
-
_getWindows(){return _windows}/**
|
|
320
|
-
*
|
|
321
|
-
* @return {Map<any, any>}
|
|
322
|
-
*/
|
|
323
|
-
_getCrossEvents(){return _crossEvents}/**
|
|
324
|
-
*
|
|
325
|
-
* @return {Set<any>}
|
|
326
|
-
*/
|
|
327
|
-
_getSubscriptions(){return _subscriptions}/**
|
|
328
|
-
* @param subscriber
|
|
329
|
-
* @return {boolean | Array}
|
|
330
|
-
*/
|
|
331
|
-
_getWindowsBySubscriber(e){let s=[];for(let[,t]of _windows)e===t.subscriber&&s.push(t);return s}/**
|
|
332
|
-
*
|
|
333
|
-
* @return {Map<any, any>}
|
|
334
|
-
*/
|
|
335
|
-
_getEvents(){return _events}/**
|
|
336
|
-
* Generate unique number using timestamp + incrementing number
|
|
337
|
-
* @returns number
|
|
338
|
-
*/
|
|
339
|
-
_uniqueNumber(){let e=Date.now();return e<=this._uniqueNumber.previous?e=++this._uniqueNumber.previous:this._uniqueNumber.previous=e,e}/**
|
|
340
|
-
* Map window being opened to the window instance which is confirmed by first message
|
|
341
|
-
* @param {number} id
|
|
342
|
-
* @param {*} win
|
|
343
|
-
* @param {string} subscriber
|
|
344
|
-
*/
|
|
345
|
-
_mapToWindow(e,s,t){_windows.set(e,{win:s,subscriber:t}),_channel.postMessage(_stringify({postal:!0,type:REGISTERID,id:e,nodeType:CROSSNODE}))}/**
|
|
346
|
-
*
|
|
347
|
-
* @param {number} id
|
|
348
|
-
*/
|
|
349
|
-
_unmapFromWindow(e){_windows.delete(e),_channel.postMessage(_stringify({postal:!0,type:UNREGISTERID,id:e}))}/**
|
|
350
|
-
*
|
|
351
|
-
* @param {number} id
|
|
352
|
-
*/
|
|
353
|
-
_mapToBroadcastNetwork(e){_broadcastNetwork.add(e),_channel.postMessage(_stringify({postal:!0,type:REGISTERID,id:e,nodeType:BROADCASTNODE}))}/**
|
|
354
|
-
*
|
|
355
|
-
* @param {number} id
|
|
356
|
-
*/
|
|
357
|
-
_unmapFromBroadcastNetwork(e){_broadcastNetwork.delete(e),_channel.postMessage(_stringify({postal:!0,type:UNREGISTERID,id:e}))}/**
|
|
358
|
-
*
|
|
359
|
-
* @param {*} address
|
|
360
|
-
* @param {Function} handler - { update, box }
|
|
361
|
-
*/
|
|
362
|
-
_postBox(e,s){const t=_stringify({postal:!0,type:POBOX,id:this.id,address:e});this.workerOnline?_worker&&_channel.postMessage(t):_queue.push(()=>_channel.postMessage(t)),_POboxes.size||PostalHelper.registerCollection(this),_POboxes.get(e)||_POboxes.set(e,s)}/**
|
|
363
|
-
*
|
|
364
|
-
* @param {*} address
|
|
365
|
-
*/
|
|
366
|
-
_closeBox(e){const s=_POboxes.get(e);PostalHelper.registerClosure(this,e,s),_POboxes.delete(e),_channel.postMessage(_stringify({postal:!0,type:CLOSEBOX,id:this.id,address:e}))}
|
|
367
|
-
// _getPostBoxes() {
|
|
368
|
-
// return _POboxes;
|
|
369
|
-
// }
|
|
370
|
-
/**
|
|
371
|
-
*
|
|
372
|
-
* @param {*} address
|
|
373
|
-
* @param {*} value
|
|
374
|
-
*/
|
|
375
|
-
_post(e,s){const t=_stringify({postal:!0,type:POST,id:this.id,address:e,value:s});this.workerOnline&&_worker?_channel.postMessage(t):_queue.push(()=>_channel.postMessage(t))}/**
|
|
376
|
-
*
|
|
377
|
-
* @param {*} drop
|
|
378
|
-
*/
|
|
379
|
-
_collectBox(e){const{address:address,value:value,box:box}=e;let s=_POboxes.get(address);s&&s({address:address,value:value,box:box})}/**
|
|
380
|
-
*
|
|
381
|
-
* @param {*} address
|
|
382
|
-
* @param {*} collect
|
|
383
|
-
*/
|
|
384
|
-
_lastCollection(e,s){_closures.set(e,s)}/**
|
|
385
|
-
*
|
|
386
|
-
* @param {*} msg
|
|
387
|
-
*/
|
|
388
|
-
_clearBox(e){const{id:id,address:address,box:box}=e;if(id===this.id){if(_closures.get(address)){_closures.get(address)({address:address,value:null,box:box}),_closures.delete(address)}_POboxes.delete(address),_closures.size||_POboxes.size||PostalHelper.unregisterCollection(this)}}/**
|
|
389
|
-
*
|
|
390
|
-
* @param {*} address
|
|
391
|
-
* @param {*} content
|
|
392
|
-
* @param {*} handling
|
|
393
|
-
*/
|
|
394
|
-
_package(e,s){const t=_stringify({postal:!0,type:PACKAGE,id:this.id,address:e});this.workerOnline?_worker&&_channel.postMessage(t):_queue.push(()=>_channel.postMessage(t)),_packages.size||PostalHelper.registerDelivery(this),_packages.get(e)||_packages.set(e,s)}/**
|
|
395
|
-
*
|
|
396
|
-
* @param {*} address
|
|
397
|
-
* @param {*} content
|
|
398
|
-
*/
|
|
399
|
-
_pack(e,s){let t=new XMLHttpRequest,n=this;t.onload=function(){let s=new FileReader;s.onloadend=function(){const t=_stringify({postal:!0,type:PACK,id:n.id,address:e,content:s.result});n.workerOnline?_worker&&_channel.postMessage(t):_queue.push(()=>{_channel.postMessage(t)})},s.readAsDataURL(t.response)},t.open("GET",s),t.responseType="blob",t.send()}/**
|
|
400
|
-
*
|
|
401
|
-
* @param {*} msg
|
|
402
|
-
*/
|
|
403
|
-
_delivery(e){const{address:address,content:content}=e;let s=_packages.get(address);s&&s({address:address,content:content})}}var hasProp=Object.prototype.hasOwnProperty,index=function(e,s,t){return JSON.stringify(ensureProperties(e),s,t)},ensureProperties_1=ensureProperties;index.ensureProperties=ensureProperties_1;/**
|
|
404
|
-
* PostalWorker Main Source (ES6 Version - Exports )
|
|
405
|
-
* Build ES6 module version
|
|
406
|
-
* @Author: Russ Stratfull - 2018
|
|
407
|
-
*/
|
|
408
|
-
let _PostalWorker;export default main;
|