-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathclient.min.js
31 lines (31 loc) · 14.5 KB
/
client.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
var $jscomp={scope:{},getGlobal:function(h){return"undefined"!=typeof window&&window===h?h:"undefined"!=typeof global?global:h}};$jscomp.global=$jscomp.getGlobal(this);$jscomp.initSymbol=function(){$jscomp.global.Symbol||($jscomp.global.Symbol=$jscomp.Symbol);$jscomp.initSymbol=function(){}};$jscomp.symbolCounter_=0;$jscomp.Symbol=function(h){return"jscomp_symbol_"+h+$jscomp.symbolCounter_++};
$jscomp.initSymbolIterator=function(){$jscomp.initSymbol();$jscomp.global.Symbol.iterator||($jscomp.global.Symbol.iterator=$jscomp.global.Symbol("iterator"));$jscomp.initSymbolIterator=function(){}};
$jscomp.makeIterator=function(h){$jscomp.initSymbolIterator();if(h[$jscomp.global.Symbol.iterator])return h[$jscomp.global.Symbol.iterator]();if(!(h instanceof Array||"string"==typeof h||h instanceof String))throw new TypeError(h+" is not iterable");var d=0;return{next:function(){return d==h.length?{done:!0}:{done:!1,value:h[d++]}}}};$jscomp.arrayFromIterator=function(h){for(var d,g=[];!(d=h.next()).done;)g.push(d.value);return g};
$jscomp.arrayFromIterable=function(h){return h instanceof Array?h:$jscomp.arrayFromIterator($jscomp.makeIterator(h))};$jscomp.arrayFromArguments=function(h){for(var d=[],g=0;g<h.length;g++)d.push(h[g]);return d};
$jscomp.inherits=function(h,d){function g(){}g.prototype=d.prototype;h.prototype=new g;h.prototype.constructor=h;for(var l in d)if($jscomp.global.Object.defineProperties){var c=$jscomp.global.Object.getOwnPropertyDescriptor(d,l);void 0!==c&&$jscomp.global.Object.defineProperty(h,l,c)}else h[l]=d[l]};
(function(h){"object"===typeof exports&&"undefined"!==typeof module?module.exports=h():"function"===typeof define&&define.amd?define([],h):("undefined"!==typeof window?window:"undefined"!==typeof global?global:"undefined"!==typeof self?self:this).bridge=h()})(function(){return function d(g,l,c){function m(f,r){if(!l[f]){if(!g[f]){var k="function"==typeof require&&require;if(!r&&k)return k(f,!0);if(n)return n(f,!0);k=Error("Cannot find module '"+f+"'");throw k.code="MODULE_NOT_FOUND",k;}k=l[f]={exports:{}};
g[f][0].call(k.exports,function(b){var a=g[f][1][b];return m(a?a:b)},k,k.exports,d,g,l,c)}return l[f].exports}for(var n="function"==typeof require&&require,f=0;f<c.length;f++)m(c[f]);return m}({1:[function(d,g,l){var c=g.exports=self.bridge||{};c.client=d("../src/client");"u"!="undefined"[0]?(void 0)([],function(){return c}):self.bridge=c},{"../src/client":2}],2:[function(d,g,l){function c(a,t,b){if(!(this instanceof c))return new c(a,t,b);"object"==typeof a&&(t=a.endpoint,b=a.timeout,a=a.service);
this.id=r();this.service=a;this.timeout=b;this.endpoint=t||this.endpoint;if(!this.endpoint)throw m(1);this.setPort(this.endpoint);this.pending=new Set;this.receiver=q.receiver(this.id).on("_push",this.onPush.bind(this));k("initialized",a)}function m(a,b){for(var e=[],p=1;p<arguments.length;++p)e[p-1]=arguments[p];return Error({1:"an endpoint must be defined",2:'Unable to establish a connection with "'+e[0]+'". Either the target endpoint is not alive or the Service is not `.listen()`ing.',3:'Method "'+
e[0]+"\" didn't get a response. Either the target endpoint is not alive or the Service is not `.listen()`ing."}[a])}var n=d("./message/port-adaptors"),f=d("./emitter"),q=d("./message"),r=d("./utils").uuid;g.exports=c;var k={0:function(){},1:function(a){return performance.mark("["+self.constructor.name+"][Client] - "+a)},2:function(a,b){for(var e=[],p=1;p<arguments.length;++p)e[p-1]=arguments[p];console.log.apply(console,[].concat(["[Client]"+("["+self.constructor.name+"]["+location.pathname+"]")+
' - "'+a+'"'],$jscomp.arrayFromIterable(e)))}}[0],b=self.constructor.name;c.prototype={connect:function(){var a=this;k("connect");if(this.connected)return this.connected;k("connecting...",this.service);var t=new MessageChannel;this.channel=t.port1;this.channel.start();var e={clientId:this.id,service:this.service,originEnv:b};return this.connected=this.message("_connect").set("transfer",[t.port2]).set("data",e).listen(t.port1).send().then(function(b){k("connected",b);b.event.target===a.channel?a.setPort(a.channel):
(a.channel.close(),delete a.channel);a.receiver.listen(a.port)}).catch(function(b){"timeout"==(b&&b.message)&&(b=m(2,a.service),console.error(b.message));throw b;})},disconnect:function(a){var b=this;if(!this.connected)return Promise.resolve();k("disconnecting ...");a={noRespond:a&&a.noRespond,data:this.id};this.cancelPending();return this.message("_disconnect").set(a).send().then(function(){return b.onDisconnected()})},method:function(a,b){for(var e=[],c=1;c<arguments.length;++c)e[c-1]=arguments[c];
var d=this;return this.connect().then(function(){k("method",a);return d.message("_method").set({recipient:d.service,data:{name:a,args:e}}).send()}).then(function(a){return a.value}).catch(function(b){"timeout"==(b&&b.message)&&(b=m(3,a),console.error(b.message));throw b;})},plugin:function(a){a(this,{Emitter:f,uuid:r});return this},message:function(a){var b=this;k("create message",a);var e=q(a).set("port",this.port).set("timeout",this.timeout).on("response",function(){return b.pending.delete(e)}).on("cancel",
function(){return b.pending.delete(e)});this.pending.add(e);return e},cancelPending:function(){k("cancel pending");this.pending.forEach(function(a){a.cancel()});this.pending.clear()},pendingResponded:function(){var a=[];this.pending.forEach(function(b){return a.push(b.responded)});return Promise.all(a)},onPush:function(a){k("on push",a.data);this._emit(a.data.type,a.data.data)},onDisconnected:function(){var a=this;delete this.connected;this.pendingResponded().then(function(){k("disconnected");a.channel&&
a.channel.close();a._emit("disconnected")})},setPort:function(a){k("set port");this.port=n(a)},destroy:function(){var a=this;return this.disconnect().then(function(){a.destroyed||(k("destroy"),a.destroyed=!0,a.receiver.destroy(),a._off(),a.port=a.endpoint=a.receiver=null)})},_on:f.prototype.on,_off:f.prototype.off,_emit:f.prototype.emit};c.prototype.on=function(a,b){var e=this;this.connect().then(function(){k("bind on",a);f.prototype.on.call(e,a,b);e.message("_on").set("noRespond",!0).set("data",
{name:a,clientId:e.id}).send(e.port)});return this};c.prototype.off=function(a,b){var e=this;this.connect().then(function(){f.prototype.off.call(e,a,b);e.message("_off").set("noRespond",!0).set("data",{name:a,clientId:e.id}).send(e.port)});return this}},{"./emitter":3,"./message":4,"./message/port-adaptors":5,"./utils":6}],3:[function(d,g,l){function c(d){if(d)return Object.assign(d,c.prototype)}g.exports=c;c.prototype={on:function(c,d){this._callbacks||(this._callbacks={});this._callbacks[c]||(this._callbacks[c]=
[]);this._callbacks[c].push(d);return this},off:function(c,d){if(this._callbacks)switch(arguments.length){case 0:this._callbacks={};break;case 1:delete this._callbacks[c];break;default:var f=this._callbacks[c];if(!f)return;var g=f.indexOf(d);~g&&f.splice(g,1)}return this},emit:function(c,d){if(this._callbacks)for(var f=this._callbacks[c]||[],f=f.concat(this._callbacks["*"]||[]),g=0;g<f.length;g++)f[g].call(this,d,c);return this}};d=c.prototype;d.off=d.off;d.on=d.on},{}],4:[function(d,g,l){function c(a){this.cancelled=
!1;this.listeners=[];this.deferred=r();this.listen=this.listen.bind(this);this.onMessage=this.onMessage.bind(this);this.onTimeout=this.onTimeout.bind(this);"object"===typeof a?this.setupInbound(a):this.setupOutbound(a);b("initialized",this.type)}function m(a){this.name=a;this.ports=new Set;this.onMessage=this.onMessage.bind(this);this.listen=this.listen.bind(this);this.unlisten=this.unlisten.bind(this);b("receiver initialized",a)}function n(a,b){for(var e=1;e<arguments.length;++e);return Error({1:".send() can only be called once",
2:"response already sent for this message",3:"a port must be defined",4:"timeout"}[a])}var f=d("./port-adaptors"),q=d("../emitter");d=d("../utils");var r=d.deferred,k=d.uuid;l=g.exports=function(a){return new c(a)};l.receiver=function(a,b){return new m(a,b)};l.Receiver=m;l.Message=c;var b={0:function(){},1:function(a){return performance.mark("["+self.constructor.name+"][Message] - "+a)},2:function(a,b){for(var e=[],c=1;c<arguments.length;++c)e[c-1]=arguments[c];console.log.apply(console,[].concat(["[Message]"+
("["+self.constructor.name+"]["+location.pathname+"]")+' - "'+a+'"'],$jscomp.arrayFromIterable(e)))}}[0];c.prototype={setupOutbound:function(a){this.id=k();this.type=a;this.sent=!1;this.recipient="*"},setupInbound:function(a){b("inbound");this.hasResponded=!1;this.setSourcePort(a.source||a.target);this.event=a;Object.assign(this,a.data)},setSourcePort:function(a){b("set source",a.constructor.name);this.sourcePort=f(a,{ready:!0});return this},set:function(a,c){b("set",a,c);"object"==typeof a?Object.assign(this,
a):this[a]=c;return this},serialize:function(){return{id:this.id,type:this.type,data:this.data,recipient:this.recipient,noRespond:this.noRespond}},preventDefault:function(){b("prevent default");this.defaultPrevented=!0},send:function(a){b("send",this.type);if(this.sent)throw n(1);var c=this.serialize(),e=!this.noRespond;this.port=a?f(a):this.port;if(!this.port)throw n(3);e?(this.listen(this.port),this.setResponseTimeout()):this.deferred.resolve();this.port.postMessage(c,this.getTransfer());b("sent",
c);return this.deferred.promise},setResponseTimeout:function(){!1!==this.timeout&&(this._timer=setTimeout(this.onTimeout,this.timeout||2E3))},clearResponseTimeout:function(){clearTimeout(this._timer)},getTransfer:function(){return this.transfer||this.event&&this.event.ports},onMessage:function(a){if(a.data.response&&a.data.id===this.id&&!this.cancelled)this.onResponse(a)},onTimeout:function(){b("response timeout",this.type);this.silentTimeout||this.deferred.reject(n(4));this.teardown()},listen:function(a){b("add response listener",
a);a=f(a);a.addListener(this.onMessage,this.listen);this.listeners.push(a);return this},unlisten:function(){var a=this;b("remove response listeners");this.listeners.forEach(function(b){return b.removeListener(a.onMessage)});this.listeners=[]},cancel:function(){this.teardown();this.cancelled=!0;this.emit("cancel")},teardown:function(){this.clearResponseTimeout();this.unlisten()},respond:function(a){function c(a){b("resolve",a);d({type:"resolve",value:a})}function e(a){a:switch(a&&a.constructor.name){case "DOMException":case "Error":a=
{message:a.message};break a;case "DOMError":a={message:a.message,name:a.name};break a}b("reject",a);d({type:"reject",value:a})}function d(a){f.response=a;f.sourcePort.postMessage({id:f.id,response:a},f.transfer);b("responded with:",a)}b("respond",a,this.id);if(this.hasResponded)throw n(2);if(this.sourcePort&&!this.noRespond){this.hasResponded=!0;var f=this;this.error&&e(this.error);Promise.resolve(a).then(c,e).catch(e)}},forward:function(a){var c=this;b("forward");return this.set("silentTimeout",
!0).send(a).then(function(a){return c.respond(a.value)})},onResponse:function(a){b("on response",a.data);var c=a.data.response,e="reject"==c.type?c.value:c;c.event=a;this.response=c;this.teardown();this.deferred[this.response.type](e);this.emit("response",c)}};q(c.prototype);m.prototype={listen:function(a){b("listen");a=f(a||self,{receiver:!0});if(!this.ports.has(a))return a.addListener(this.onMessage,this.listen),this.ports.add(a),this},unlisten:function(){var a=this;b("unlisten");this.ports.forEach(function(b){return b.removeListener(a.onMessage)})},
onMessage:function(a){if(a.data.id&&a.data.type&&this.isRecipient(a.data.recipient)&&(b("receiver on message",a.data),a=new c(a),this.emit("message",a),!a.defaultPrevented))try{this.emit(a.type,a)}catch(d){throw a.error=d,a.respond(),d;}},isRecipient:function(a){return a==this.name||"*"==a||"*"==this.name},destroy:function(){this.unlisten();delete this.name;return this}};q(m.prototype)},{"../emitter":3,"../utils":6,"./port-adaptors":5}],5:[function(d,g,l){function c(b){this.target=b}function m(b,
a,c){b.addEventListener(a,c)}function n(b,a,c){a={data:a,source:self};c&&(a.ports=c);b.dispatchEvent(new MessageEvent("message",a))}var f=d("../utils").deferred;g.exports=function(b,a){if(!b)throw Error({1:"target is undefined"}[1]);if(b&&b.addListener)return b;var d=r[b.constructor.name];return d?d(b,a):new c(b,a)};var q=c.prototype={constructor:c,addListener:function(b){this.target.addEventListener("message",b)},removeListener:function(b){this.target.removeEventListener("message",b)},postMessage:function(b,
a){this.target.postMessage(b,a)}},r={HTMLIFrameElement:function(b){var a=k(b);return{addListener:function(a){window.addEventListener("message",a)},removeListener:function(a){window.removeEventListener("message",a)},postMessage:function(c,e){a.then(function(){return n(b.contentWindow,c,e)})}}},BroadcastChannel:function(b,a){function c(){var a=f();b.postMessage("ready?");m(b,"message",function u(c){"ready"==c.data&&(b.removeEventListener("message",u),a.resolve())});return a.promise}var e=a&&a.receiver,
d=a&&a.ready,d=d||e?Promise.resolve():c();e&&(b.postMessage("ready"),m(b,"message",function(a){"ready?"==a.data&&b.postMessage("ready")}));return{target:b,addListener:q.addListener,removeListener:q.removeListener,postMessage:function(a,c){d.then(function(){return b.postMessage(a,c)})}}},Window:function(b,a){var c=a&&a.ready||b===parent||b===self,c=c?Promise.resolve():k(b);return{addListener:function(a){window.addEventListener("message",a)},removeListener:function(a){window.removeEventListener("message",
a)},postMessage:function(a,d){c.then(function(){return n(b,a,d)})}}},SharedWorker:function(b){b.port.start();return new c(b.port)},SharedWorkerGlobalScope:function(){var b=[];return{postMessage:function(){},addListener:function(a,c){this.onconnect=function(a){a=a.ports[0];b.push(a);a.start();c(a)};self.addEventListener("connect",this.onconnect)},removeListener:function(a){self.removeEventListener("connect",this.onconnect);b.forEach(function(b){b.close();b.removeEventListener("message",a)})}}}},k=
function(){if("undefined"!=typeof window){var b=window.opener||window.parent,a=new WeakSet;b!=self&&("loading"===document.readyState?m(window,"DOMContentLoaded",function e(){window.removeEventListener("DOMContentLoaded",e);n(b,"load")}):n(b,"load"));m(self,"message",function(b){return"load"==b.data&&a.add(b.source)});return function(b){var c=b.contentWindow||b;if(a.has(c)||c==window.parent)return Promise.resolve();var d=f();m(window,"message",function u(a){"load"==a.data&&a.source==c&&(window.removeEventListener("message",
u),d.resolve())});return d.promise}}}()},{"../utils":6}],6:[function(d,g,l){l.uuid=function(){return"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,function(c){var d=16*Math.random()|0;return("x"==c?d:d&3|8).toString(16)})};l.deferred=function(){var c={};c.promise=new Promise(function(d,g){c.resolve=d;c.reject=g});return c}},{}]},{},[1])(1)});