UNPKG

cross-domain-emitter

Version:
17 lines (14 loc) 24 kB
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t=function(e,n){return t=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var n in e)Object.prototype.hasOwnProperty.call(e,n)&&(t[n]=e[n])},t(e,n)};function e(e,n){if("function"!=typeof n&&null!==n)throw new TypeError("Class extends value "+String(n)+" is not a constructor or null");function r(){this.constructor=e}t(e,n),e.prototype=null===n?Object.create(n):(r.prototype=n.prototype,new r)}var n=function(){return n=Object.assign||function(t){for(var e,n=1,r=arguments.length;n<r;n++)for(var i in e=arguments[n])Object.prototype.hasOwnProperty.call(e,i)&&(t[i]=e[i]);return t},n.apply(this,arguments)},r=function(){},i=function(){function t(t,e,n,r){this.uuid="",this.name="",this.data=null,this.uuid=t,this.name=e,this.data=n,this.timestamp=Date.now(),r&&(this.option=r)}return t.prototype.serialize=function(){return JSON.stringify(this)},t.deserialize=function(e){if(!e)return new t("","unknown");try{var n=JSON.parse(e),r=new t(n.uuid,n.name,n.data,n.option);return r.timestamp=n.timestamp,r}catch(n){return console.log(n),new t("","unknown",e)}},t.copyFrom=function(e){return new t(e.uuid,e.name,e.data,e.option)},t}();function o(t){return"[object Object]"==Object.prototype.toString.call(t)}function s(t){return"[object Function]"==Object.prototype.toString.call(t)}function a(t){return null==t||""===t}function c(t,e,n){if(!a(t))for(var r,i=t.length,o=0;o<i;o++){if(r=t[o],!a(e))if(e.call(n,r,o,t))return}}"undefined"!=typeof globalThis?globalThis:"undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self&&self;var u={exports:{}};u.exports=function(){ /*! ***************************************************************************** Copyright (c) Microsoft Corporation. Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ***************************************************************************** */ function t(t,e,n){if(n||2===arguments.length)for(var r,i=0,o=e.length;i<o;i++)!r&&i in e||(r||(r=Array.prototype.slice.call(e,0,i)),r[i]=e[i]);return t.concat(r||Array.prototype.slice.call(e))}function e(t){return"[object Number]"==Object.prototype.toString.call(t)}function n(t){return"[object Object]"==Object.prototype.toString.call(t)}function r(t){return"[object Array]"==Object.prototype.toString.call(t)}function i(t){return null==t}function o(t,e,s,a){if(void 0===s&&(s=!1),void 0===a&&(a=!0),!a&&i(e))return t;if(r(l=e)||n(l)){if(n(e))for(var c in s&&(n(t)||(t={})),e)t[c]=o(t[c],e[c],!0,a);else if(r(e)){s&&(r(t)||(t=[]));for(var u=e.length,h=0;h<u;h++)t[h]=o(t[h],e[h],!0,a)}}else(a||i(t))&&(t=e);var l;return t}var s=function(){function t(t){this.name="",this.handlers=[],this.name=t}return t.prototype.pushHandler=function(t,e,n){this.handlers.push({method:t,times:n,context:e})},t.prototype.deleHandler=function(t){var e=null;return this.walkHandlers((function(n,r,i){if(n.method===t)return e=i.splice(r,1),!0})),e},t.prototype.executeHandlers=function(t,n){void 0===t&&(t=!0),void 0===n&&(n=!1);for(var r=[],i=2;i<arguments.length;i++)r[i-2]=arguments[i];for(var s=this.handlers,a=null,c=0;c<s.length;c++){a=s[c];var u=void 0,h=void 0;if(u=n?o([],r):r,e(a.times)?(a.times>0&&(h=a.method.apply(a.context,u),a.times--),a.times<=0&&(s.splice(c,1),c--)):h=a.method.apply(a.context,u),h&&t)break}},t.prototype.walkHandlers=function(t){!function(t,e,n){if(!i(t))for(var r,o=t.length,s=0;s<o;s++)if(r=t[s],!i(e)&&e.call(n,r,s,t))return}(this.handlers,(function(e,n,r){return t(e,n,r)}))},t}(),a=function(){function n(){this.events={},this.trigger=this.emit}return n.prototype.on=function(t,n,i,o){e(i)&&(i=o=i);for(var a,c=r(t)?t:(a=t,a?a.replace(/(^[ \t\n\r]+)|([ \t\n\r]+$)/g,""):a).split(/\s+/),u=this.events,h=c.length,l=0;l<h;l++){var p=c[l];u[p]||(u[p]=new s(p)),u[p].pushHandler(n,i,o)}return this},n.prototype.once=function(t,e,n){return this.on(t,e,1,n)},n.prototype.emit=function(e){for(var n,r=[],i=1;i<arguments.length;i++)r[i-1]=arguments[i];return this.events[e]&&(n=this.events[e]).executeHandlers.apply(n,t([!0,!1],r,!1)),this},n.prototype.emitOneCopy=function(e){for(var n,r=[],i=1;i<arguments.length;i++)r[i-1]=arguments[i];return this.events[e]&&(n=this.events[e]).executeHandlers.apply(n,t([!0,!0],r,!1)),this},n.prototype.broadCast=function(e){for(var n,r=[],i=1;i<arguments.length;i++)r[i-1]=arguments[i];return this.events[e]&&(n=this.events[e]).executeHandlers.apply(n,t([!1,!1],r,!1)),this},n.prototype.broadCastOneCopy=function(e){for(var n,r=[],i=1;i<arguments.length;i++)r[i-1]=arguments[i];return this.events[e]&&(n=this.events[e]).executeHandlers.apply(n,t([!1,!0],r,!1)),this},n.prototype.off=function(t,e){return this.events[t]&&(e?this.events[t].deleHandler(e):delete this.events[t]),this},n.prototype.clear=function(){return this.events={},this},n}();return a}();let h=(t=21)=>crypto.getRandomValues(new Uint8Array(t)).reduce(((t,e)=>t+=(e&=63)<36?e.toString(36):e<62?(e-26).toString(36).toUpperCase():e>62?"-":"_"),"");function l(t){return void 0===t&&(t=21),h(t)}function p(t,e){t.addHandler(e),e.addTransceiver(t)}function d(t,e){t.removeHandler(e),e.removeTransceiver(t)}var f=function(t){function n(){var e=null!==t&&t.apply(this,arguments)||this;return e.id=l(),e.transceivers=[],e.emitters=[],e}return e(n,t),n.prototype.emit=function(e,i,o,s){if(o||(o={}),o.emitterId||(o.emitterId=this.id),!o||!0===o.local)return t.prototype.emit.call(this,e,i,o),this;if(o&&!1===o.local||t.prototype.emit.call(this,e,i,o),s)return"[object Array]"==Object.prototype.toString.call(s)?c(s,(function(t){t instanceof r?t&&t.checkStatus()&&t.send(e,i,o):t instanceof n&&t.onMessage({data:i,name:e,option:o})})):s instanceof r?s.checkStatus()&&s.send(e,i,o):s instanceof n&&s.onMessage({data:i,name:e,option:o}),this;this.emitters.forEach((function(t){t.onMessage({data:i,name:e,option:o})}));for(var a=this.transceivers,u=0;u<a.length;u++){var h=a[u];h&&h.checkStatus()?h.send(e,i,o):(h&&h.stop&&h.stop(),a.splice(u,1),u--)}return this},n.prototype.bindTransceiver=function(t){p(t,this)},n.prototype.addTransceiver=function(t){var e=!0;c(this.transceivers,(function(n){if(n===t)return e=!1,!0})),e&&this.transceivers.push(t)},n.prototype.removeTransceiver=function(t){var e=this;c(this.transceivers,(function(n,r){if(n===t)return e.transceivers.splice(r,1),!0}))},n.prototype.unBindTransceiver=function(t){d(t,this)},n.prototype.clearTransceiver=function(){this.transceivers=[]},n.prototype.onMessage=function(e){t.prototype.emit.call(this,e.name,e.data,e.option)},n.prototype.addEmitter=function(t){var e=!0;c(this.emitters,(function(n){if(n===t)return e=!1,!0})),e&&this.emitters.push(t)},n.prototype.removeEmitter=function(t){var e=this;c(this.emitters,(function(n,r){if(n===t)return e.emitters.splice(r,1),!0}))},n}(u.exports);var v,y=function(t){function n(){var e=null!==t&&t.apply(this,arguments)||this;return e.store={},e}return e(n,t),n.prototype.set=function(t,e){var n=this.store[t];this.store[t]=e,this.emit(t,{newVal:e,oldVal:n})},n.prototype.get=function(t){return this.store[t]},n}(f);!function(t){t[t.close=0]="close",t[t.open=1]="open"}(v||(v={}));var m,T=function(t){function n(e){var n=t.call(this)||this;return n.status=v.close,n.handlers=[],n.context=window,n.keyPrefix="",n.filter=void 0,n.uuid="",n.context=e.win,n.filter=e.filter,e.keyPrefix&&(n.keyPrefix=e.keyPrefix),n.uuid=h(),n}return e(n,t),n.prototype.isValidName=function(t){return!(this.keyPrefix&&(!t||0!==t.indexOf(this.keyPrefix)))},n.prototype.getRealName=function(t){return t.replace(this.keyPrefix,"")},n.prototype.messageHandler=function(t){var e=t.key,n=this.context.localStorage.getItem(e);if(n&&this.isValidName(e)){var r=this.getRealName(e),o=i.deserialize(n);o.name=r,o.uuid!==this.uuid&&(this.filter&&!this.filter(t)||(c(this.handlers,(function(t){t.onMessage(o)})),this.context.localStorage.removeItem(e)))}},n.prototype.send=function(t,e,n){var r=this.getEventName(t),o=new i(this.uuid,t,e,n).serialize();return this.context.localStorage.setItem(r,o),this},n.prototype.getEventName=function(t){return this.keyPrefix+t},n.prototype.start=function(){this.status===v.close&&(this.status=v.open,this.context.addEventListener("storage",this.messageHandler.bind(this)))},n.prototype.stop=function(){this.status=v.close,this.context.removeEventListener("storage",this.messageHandler.bind(this))},n.prototype.clearHandler=function(){this.handlers=[]},n.prototype.addHandler=function(t){var e=!0;c(this.handlers,(function(n){if(n===t)return e=!1,!0})),e&&this.handlers.push(t)},n.prototype.removeHandler=function(t){var e=this;c(this.handlers,(function(n,r){if(n===t)return e.handlers.splice(r,1),!0}))},n.prototype.checkStatus=function(){return this.status===v.open&&!!this.context},n.prototype.readHistoryMessage=function(){var t=this.context.localStorage;for(var e in t){var n=new StorageEvent("history");n.initStorageEvent(e),this.messageHandler(n)}},n}(r);function g(t){return new T(t)}!function(t){t[t.close=0]="close",t[t.open=1]="open",t[t.connecting=3]="connecting",t[t.error=4]="error"}(m||(m={}));var w=function(t){function r(e){var n=t.call(this)||this;return n.handlers=[],n.context=window,n.status=m.close,n.host="",n.allowHost=[],n.id="",n.uuidKey="",n.uuidValue="",n.maxRetryTimes=3,n.retryTimes=-1,n.reconnectInterval=500,n.timeOutId=null,n.connectError=void 0,n._messageHandlerWrap=null,n.connectedCall=[],n.context=e.win,n.host=e.host,n.allowHost=e.allowHost,n.id=e.idGenerator?s(e.idGenerator)?e.idGenerator():e.idGenerator:l(),n}return e(r,t),r.prototype.messageHandler=function(t){if(this.checkStatus()){if(this.allowHost&&this.allowHost.includes(t.origin)||this.host===t.origin||"*"===this.host){var e=null;if("[object String]"==Object.prototype.toString.call(t.data)?e=i.deserialize(t.data):o(t.data)&&t.data.name&&(e=i.copyFrom(t.data)),o(e)&&e.uuid===this.uuidValue){if("connect"===e.name)return void this.send("connected");if("connected"===e.name)return void this.connected();e.option||(e.option={}),e.option.event=t,e.option.transceiver=this,c(this.handlers,(function(t){t.onMessage(e)}))}}}else this.stop()},r.prototype.send=function(t,e,r){return this.uuidValue?(this.status===m.close||(null!=window.structuredClone?this.context.postMessage(new i(this.uuidValue,t,e,r),this.host):this.context.postMessage(new i(this.uuidValue,t,e,n(n({},r),{transceiver:void 0,event:void 0})).serialize(),this.host)),this):(this.connectError=new Error("uuidValue is empty."),this)},r.prototype.start=function(){this.status!==m.close&&this.status!==m.error||(this.status=m.connecting,this._messageHandlerWrap=this.messageHandler.bind(this),window.addEventListener("message",this._messageHandlerWrap,!1),this.connect())},r.prototype.stop=function(){this.status=m.close,this.resetProperties(),this._messageHandlerWrap&&window.removeEventListener("message",this._messageHandlerWrap,!1)},r.prototype.clearHandler=function(){this.handlers=[]},r.prototype.addHandler=function(t){var e=!0;c(this.handlers,(function(n){if(n===t)return e=!1,!0})),e&&this.handlers.push(t)},r.prototype.removeHandler=function(t){var e=this;c(this.handlers,(function(n,r){if(n===t)return e.handlers.splice(r,1),!0}))},r.prototype.checkStatus=function(){return!(!this.context||!this.context.parent)},r.prototype.setUUID=function(t){this.uuidValue=t},r.prototype.getUUID=function(){return this.uuidValue},r.prototype.setUUIDKey=function(t){this.uuidKey=t},r.prototype.getUUIDKey=function(){return this.uuidKey},r.prototype.changeOption=function(t){this.context=t.win,this.host=t.host,this.allowHost=t.allowHost},r.prototype.connect=function(){var t=this;this.retryTimes<=this.maxRetryTimes?(this.retryTimes++,this.send("connect"),this.timeOutId=setTimeout((function(){t.connect()}),this.reconnectInterval)):(this.connectError=new Error("connection retry fail."),this.connectedFailure())},r.prototype.connected=function(){this.resetProperties(),this.status=m.open,this.excuteConnectedCall()},r.prototype.excuteConnectedCall=function(t){for(;this.connectedCall.length>0;){var e=this.connectedCall.shift();e&&e(t)}},r.prototype.connectedFailure=function(){this.status=m.error,this.excuteConnectedCall(this.connectError),this.resetProperties()},r.prototype.onConnected=function(t){switch(this.status){case m.open:return void t();case m.error:return void t(this.connectError);default:this.connectedCall.push(t)}},r.prototype.resetProperties=function(){this.retryTimes=-1,this.timeOutId&&(clearTimeout(this.timeOutId),this.timeOutId=null)},r}(r);var x,b=function(t){function n(e){var n=t.call(this)||this;return n.uuidKey="",n.store=new y,n.windowTransceivers=new Map,n.uuidKey=e,n}return e(n,t),n.prototype.addWindowTransceiver=function(t){var e=this.windowTransceivers.get(t.win);return e?e.changeOption(t):(e=function(t){return new w(t)}(t),this.windowTransceivers.set(t.win,e),this.bindTransceiver(e),this.store.bindTransceiver(e)),e},n.prototype.removeWindowTransceiver=function(t){t.stop(),this.windowTransceivers.delete(t.context),this.unBindTransceiver(t),this.store.unBindTransceiver(t)},n.prototype.removeWindowTransceiverByWindow=function(t){var e=this.windowTransceivers.get(t);e&&this.removeWindowTransceiver(e)},n.prototype.findWindowTransceiverByWindowIds=function(t){var e=this,n=[];return c(t,(function(t){var r=e.transceivers.find((function(e){if(e.id===t)return!0}));r&&n.push(r)})),n},n.prototype.clearWindowTransceiver=function(){var t=this;this.windowTransceivers.forEach((function(e){t.removeWindowTransceiver(e)}))},n}(f),S=function(t){function n(){return null!==t&&t.apply(this,arguments)||this}return e(n,t),n.prototype.createIframeTransceiver=function(t){var e=this.getUUID(window.location.href),n=this.addWindowTransceiver({win:window.parent,host:t});return n.setUUID(e),n},n.prototype.getUUID=function(t){var e=function(t){var e={},n=t.split("?")[1];if(n)for(var r=n.split("&"),i=0;i<r.length;i++){var o=r[i].split("=");e[o[0]]=o[1]}return e}(t);return e[this.uuidKey]},n}(b),C=function(t){function r(){return null!==t&&t.apply(this,arguments)||this}return e(r,t),r.prototype.addIframeTransceiver=function(t){var e=t.uuidValue?t.uuidValue:l(),r=t.uuidKey?t.uuidKey:this.uuidKey;if(t.url){var i=this.addUUIDtoUrl(t.url,r,e);t.iframe.src=i}var o=this.addWindowTransceiver(n({win:t.iframe.contentWindow},t));return o.setUUID(e),o.setUUIDKey(r),o},r.prototype.changeIframeTransceiver=function(t,e){var n=e.uuidValue?e.uuidValue:t.getUUID(),r=e.uuidKey?e.uuidKey:t.getUUIDKey();if(e.url){var i=this.addUUIDtoUrl(e.url,r,n);e.iframe.src=i}return t.changeOption({win:e.iframe.contentWindow,host:e.host,allowHost:e.allowHost}),t},r.prototype.removeIframeTransceiver=function(t){t.contentWindow&&this.removeWindowTransceiverByWindow(t.contentWindow)},r.prototype.addUUIDtoUrl=function(t,e,n){t.indexOf("?")<0&&(t+="?");var r=e+"="+n;return/\?$/.test(t)?t+=r:t=t+"&"+r,t},r}(b);!function(t){t[t.close=0]="close",t[t.open=1]="open"}(x||(x={}));var H,O=function(t){function n(e){var n=t.call(this)||this;return n.status=x.close,n.handlers=[],n.filter=void 0,n.broadcastChannel=null,n.filter=e.filter,n.channelName=e.channelName||"cross-domain-emitter-broadcast-channel",n.uuid=l(),n.broadcastChannel=new BroadcastChannel(n.channelName),n}return e(n,t),n.prototype.messageHandler=function(t){if(!this.filter||this.filter(t))try{var e=t.data;if(e&&"object"==typeof e&&e.uuid!==this.uuid){var n=new i(e.uuid,e.name,e.data,e.option);n.timestamp=e.timestamp,c(this.handlers,(function(t){t.onMessage(n)}))}}catch(t){console.error("Failed to handle broadcast message:",t)}},n.prototype.send=function(t,e,n){if(this.status!==x.open||!this.broadcastChannel)return this;var r={uuid:this.uuid,name:t,data:e,option:n,timestamp:Date.now()};try{this.broadcastChannel.postMessage(r)}catch(t){console.error("Failed to send message via BroadcastChannel:",t)}return this},n.prototype.start=function(){this.status===x.close&&(this.broadcastChannel||(this.broadcastChannel=new BroadcastChannel(this.channelName)),this.status=x.open,this.broadcastChannel.onmessage=this.messageHandler.bind(this),this.send("__tab_connected",{tabId:this.uuid},{local:!0}))},n.prototype.stop=function(){this.status===x.open&&(this.status=x.close,this.send("__tab_disconnected",{tabId:this.uuid},{local:!0}),this.broadcastChannel&&(this.broadcastChannel.close(),this.broadcastChannel=null))},n.prototype.clearHandler=function(){this.handlers=[]},n.prototype.addHandler=function(t){var e=!0;c(this.handlers,(function(n){if(n===t)return e=!1,!0})),e&&this.handlers.push(t)},n.prototype.removeHandler=function(t){var e=this;c(this.handlers,(function(n,r){if(n===t)return e.handlers.splice(r,1),!0}))},n.prototype.checkStatus=function(){return this.status===x.open&&!!this.broadcastChannel},n.isBroadcastChannelSupported=function(){return"undefined"!=typeof BroadcastChannel},n}(r);function k(t){return new O(t)}exports.TransceiverType=void 0,(H=exports.TransceiverType||(exports.TransceiverType={}))[H.broadcastChannel=0]="broadcastChannel",H[H.localStorage=1]="localStorage";var B=function(){function t(t){var e;this.localStorageTransceiver=null,this.broadcastChannelTransceiver=null,this.currentTransceiver=null,this.currentType=exports.TransceiverType.localStorage,this.isStarted=!1,this.handlers=[],this.context=t.win||window,this.filter=t.filter,this.channelName=t.channelName||"cross-domain-emitter-same-origin",this.keyPrefix=t.keyPrefix||"same-origin-",this.uuid=l(),this.preferBroadcastChannel=null===(e=t.preferBroadcastChannel)||void 0===e||e,this.initializeTransceivers()}return t.prototype.initializeTransceivers=function(){if(t.isBroadcastChannelSupported()&&this.preferBroadcastChannel)return this.broadcastChannelTransceiver=k({filter:this.filter,channelName:this.channelName}),this.currentTransceiver=this.broadcastChannelTransceiver,void(this.currentType=exports.TransceiverType.broadcastChannel);this.localStorageTransceiver=g({win:this.context,filter:this.filter,keyPrefix:this.keyPrefix}),this.currentTransceiver=this.localStorageTransceiver,this.currentType=exports.TransceiverType.localStorage},t.prototype.send=function(t,e,n){return this.isStarted&&this.currentTransceiver?(this.currentTransceiver.send(t,e,n),this):this},t.prototype.start=function(){return this.isStarted||(this.isStarted=!0,this.currentTransceiver&&this.currentTransceiver.start()),this},t.prototype.stop=function(){return this.isStarted?(this.isStarted=!1,this.currentTransceiver&&this.currentTransceiver.stop(),this):this},t.prototype.restart=function(){return this.stop(),this.start(),this},t.prototype.addHandler=function(t){return this.handlers.includes(t)||(this.handlers.push(t),this.currentTransceiver&&this.currentTransceiver.addHandler(t)),this},t.prototype.removeHandler=function(t){var e=this.handlers.indexOf(t);return e>-1&&(this.handlers.splice(e,1),this.currentTransceiver&&this.currentTransceiver.removeHandler(t)),this},t.prototype.clearHandler=function(){return this.handlers=[],this.currentTransceiver&&this.currentTransceiver.clearHandler(),this},t.prototype.checkStatus=function(){var t;return this.isStarted&&!!(null===(t=this.currentTransceiver)||void 0===t?void 0:t.checkStatus())},t.prototype.getCurrentTransceiverType=function(){return this.currentType},t.isBroadcastChannelSupported=function(){return O.isBroadcastChannelSupported()},t.prototype.switchToBroadcastChannel=function(){return!!t.isBroadcastChannelSupported()&&(this.currentType===exports.TransceiverType.broadcastChannel||(this.broadcastChannelTransceiver||(this.broadcastChannelTransceiver=k({filter:this.filter,channelName:this.channelName})),this.switchTransceiver(this.broadcastChannelTransceiver,exports.TransceiverType.broadcastChannel)),!0)},t.prototype.switchToLocalStorage=function(){return this.currentType===exports.TransceiverType.localStorage||(this.localStorageTransceiver||(this.localStorageTransceiver=g({win:this.context,filter:this.filter,keyPrefix:this.keyPrefix})),this.switchTransceiver(this.localStorageTransceiver,exports.TransceiverType.localStorage)),!0},t.prototype.switchTransceiver=function(t,e){this.currentTransceiver&&this.isStarted&&(this.currentTransceiver.stop(),this.currentTransceiver.clearHandler()),this.currentTransceiver=t,this.currentType=e,this.handlers.forEach((function(e){t.addHandler(e)})),this.isStarted&&t.start()},t}();function I(t){return new B(t)}var U=function(t){function r(e){void 0===e&&(e={});var n=t.call(this)||this;return n.channelName=e.channelName||"cross-domain-emitter-same-origin",n.keyPrefix=e.keyPrefix||"same-origin-",n.transceiver=I({win:window,filter:e.filter,channelName:n.channelName,keyPrefix:n.keyPrefix,preferBroadcastChannel:e.preferBroadcastChannel}),n.bindTransceiver(n.transceiver.currentTransceiver),n.transceiver.start(),n}return e(r,t),r.prototype.sendToOtherTabs=function(t,e,r){return this.emit(t,e,n(n({},r),{local:!1}))},r.prototype.sendToAllTabs=function(t,e,r){return this.emit(t,e,n(n({},r),{local:!0})),this.sendToOtherTabs(t,e,r)},r.prototype.onTabConnected=function(t){this.on("__tab_connected",(function(e){t(e.tabId)}))},r.prototype.onTabDisconnected=function(t){this.on("__tab_disconnected",(function(e){t(e.tabId)}))},r.prototype.getCurrentTransceiverType=function(){return this.transceiver.getCurrentTransceiverType()},r.prototype.isBroadcastChannelSupported=function(){return B.isBroadcastChannelSupported()},r.prototype.switchToBroadcastChannel=function(){var t=this.transceiver.currentTransceiver;return!!this.transceiver.switchToBroadcastChannel()&&(this.unBindTransceiver(t),this.bindTransceiver(this.transceiver.currentTransceiver),!0)},r.prototype.switchToLocalStorage=function(){var t=this.transceiver.currentTransceiver;return!!this.transceiver.switchToLocalStorage()&&(this.unBindTransceiver(t),this.bindTransceiver(this.transceiver.currentTransceiver),!0)},r.prototype.checkStatus=function(){return this.transceiver.checkStatus()},r.prototype.stop=function(){this.transceiver.stop(),this.unBindTransceiver(this.transceiver.currentTransceiver)},r.prototype.restart=function(){this.transceiver.restart()},r.prototype.refreshConnection=function(){this.transceiver.stop(),this.transceiver.start()},r}(f);exports.BaseTransceiver=r,exports.BroadcastChannelTransceiver=O,exports.EventBus=f,exports.LocalStorageTransceiver=T,exports.SameOriginEventBus=U,exports.SameOriginTransceiver=B,exports.Signal=i,exports.Store=y,exports.WindowClient=S,exports.WindowEventBus=b,exports.WindowServer=C,exports.WindowTransceiver=w,exports.createBroadcastChannelTransceiver=k,exports.createLocalStorageTransceiver=g,exports.createSameOriginEventBus=function(t){return new U(t)},exports.createSameOriginTransceiver=I,exports.createVirtualService=function(t,e){var n=e+"_req",r=e+"_update",i=null,o=new Set;return{get:function(){return new Promise((function(e){var r=n+"_"+l();o.add(r),t.once(r,(function(t){o.delete(r),e(t)})),t.emit(n,null,{returnEvent:r})}))},set:function(e,o){void 0===o&&(o=!0),o&&t.off(n),t.on(n,(function(r,i){var o=n;i&&i.returnEvent&&(o=i.returnEvent),s(e)?new Promise((function(t){e(r,i,t)})).then((function(e){t.emit(o,e)})):t.emit(o,e)})),s(e)?e(void 0,void 0,(function(e){t.emit(r,{newVal:e,oldVal:i}),i=e})):(t.emit(r,{newVal:e,oldVal:i}),i=e)},on:function(e){t.on(r,e)},off:function(e){t.off(r,e)},destroy:function(){o.forEach((function(e){t.off(e)})),t.off(n),t.off(r)}}},exports.hingeJointTransceiver=p,exports.unHingeJointTransceiver=d;