-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathservice.min.js
29 lines (29 loc) · 13.6 KB
/
service.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
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,f=[];!(d=h.next()).done;)f.push(d.value);return f};
$jscomp.arrayFromIterable=function(h){return h instanceof Array?h:$jscomp.arrayFromIterator($jscomp.makeIterator(h))};$jscomp.arrayFromArguments=function(h){for(var d=[],f=0;f<h.length;f++)d.push(h[f]);return d};
$jscomp.inherits=function(h,d){function f(){}f.prototype=d.prototype;h.prototype=new f;h.prototype.constructor=h;for(var l in d)if($jscomp.global.Object.defineProperties){var a=$jscomp.global.Object.getOwnPropertyDescriptor(d,l);void 0!==a&&$jscomp.global.Object.defineProperty(h,l,a)}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(f,l,a){function m(k,g){if(!l[k]){if(!f[k]){var e="function"==typeof require&&require;if(!g&&e)return e(k,!0);if(p)return p(k,!0);e=Error("Cannot find module '"+k+"'");throw e.code="MODULE_NOT_FOUND",e;}e=l[k]={exports:{}};
f[k][0].call(e.exports,function(c){var b=f[k][1][c];return m(b?b:c)},e,e.exports,d,f,l,a)}return l[k].exports}for(var p="function"==typeof require&&require,n=0;n<a.length;n++)m(a[n]);return m}({1:[function(d,f,l){var a=f.exports=self.bridge||{};a.service=d("../src/service");"u"!="undefined"[0]?(void 0)([],function(){return a}):self.bridge=a},{"../src/service":5}],2:[function(d,f,l){function a(d){if(d)return Object.assign(d,a.prototype)}f.exports=a;a.prototype={on:function(a,d){this._callbacks||(this._callbacks=
{});this._callbacks[a]||(this._callbacks[a]=[]);this._callbacks[a].push(d);return this},off:function(a,d){if(this._callbacks)switch(arguments.length){case 0:this._callbacks={};break;case 1:delete this._callbacks[a];break;default:var f=this._callbacks[a];if(!f)return;var k=f.indexOf(d);~k&&f.splice(k,1)}return this},emit:function(a,d){if(this._callbacks)for(var f=this._callbacks[a]||[],f=f.concat(this._callbacks["*"]||[]),k=0;k<f.length;k++)f[k].call(this,d,a);return this}};d=a.prototype;d.off=d.off;
d.on=d.on},{}],3:[function(d,f,l){function a(b){this.cancelled=!1;this.listeners=[];this.deferred=g();this.listen=this.listen.bind(this);this.onMessage=this.onMessage.bind(this);this.onTimeout=this.onTimeout.bind(this);"object"===typeof b?this.setupInbound(b):this.setupOutbound(b);c("initialized",this.type)}function m(b){this.name=b;this.ports=new Set;this.onMessage=this.onMessage.bind(this);this.listen=this.listen.bind(this);this.unlisten=this.unlisten.bind(this);c("receiver initialized",b)}function p(b,
c){for(var a=1;a<arguments.length;++a);return Error({1:".send() can only be called once",2:"response already sent for this message",3:"a port must be defined",4:"timeout"}[b])}var n=d("./port-adaptors"),k=d("../emitter");d=d("../utils");var g=d.deferred,e=d.uuid;l=f.exports=function(b){return new a(b)};l.receiver=function(b,c){return new m(b,c)};l.Receiver=m;l.Message=a;var c={0:function(){},1:function(b){return performance.mark("["+self.constructor.name+"][Message] - "+b)},2:function(b,c){for(var a=
[],g=1;g<arguments.length;++g)a[g-1]=arguments[g];console.log.apply(console,[].concat(["[Message]"+("["+self.constructor.name+"]["+location.pathname+"]")+' - "'+b+'"'],$jscomp.arrayFromIterable(a)))}}[0];a.prototype={setupOutbound:function(b){this.id=e();this.type=b;this.sent=!1;this.recipient="*"},setupInbound:function(b){c("inbound");this.hasResponded=!1;this.setSourcePort(b.source||b.target);this.event=b;Object.assign(this,b.data)},setSourcePort:function(b){c("set source",b.constructor.name);this.sourcePort=
n(b,{ready:!0});return this},set:function(b,a){c("set",b,a);"object"==typeof b?Object.assign(this,b):this[b]=a;return this},serialize:function(){return{id:this.id,type:this.type,data:this.data,recipient:this.recipient,noRespond:this.noRespond}},preventDefault:function(){c("prevent default");this.defaultPrevented=!0},send:function(b){c("send",this.type);if(this.sent)throw p(1);var a=this.serialize(),g=!this.noRespond;this.port=b?n(b):this.port;if(!this.port)throw p(3);g?(this.listen(this.port),this.setResponseTimeout()):
this.deferred.resolve();this.port.postMessage(a,this.getTransfer());c("sent",a);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(b){if(b.data.response&&b.data.id===this.id&&!this.cancelled)this.onResponse(b)},onTimeout:function(){c("response timeout",this.type);
this.silentTimeout||this.deferred.reject(p(4));this.teardown()},listen:function(b){c("add response listener",b);b=n(b);b.addListener(this.onMessage,this.listen);this.listeners.push(b);return this},unlisten:function(){var b=this;c("remove response listeners");this.listeners.forEach(function(c){return c.removeListener(b.onMessage)});this.listeners=[]},cancel:function(){this.teardown();this.cancelled=!0;this.emit("cancel")},teardown:function(){this.clearResponseTimeout();this.unlisten()},respond:function(b){function a(b){c("resolve",
b);d({type:"resolve",value:b})}function g(b){a:switch(b&&b.constructor.name){case "DOMException":case "Error":b={message:b.message};break a;case "DOMError":b={message:b.message,name:b.name};break a}c("reject",b);d({type:"reject",value:b})}function d(b){e.response=b;e.sourcePort.postMessage({id:e.id,response:b},e.transfer);c("responded with:",b)}c("respond",b,this.id);if(this.hasResponded)throw p(2);if(this.sourcePort&&!this.noRespond){this.hasResponded=!0;var e=this;this.error&&g(this.error);Promise.resolve(b).then(a,
g).catch(g)}},forward:function(b){var a=this;c("forward");return this.set("silentTimeout",!0).send(b).then(function(b){return a.respond(b.value)})},onResponse:function(b){c("on response",b.data);var a=b.data.response,g="reject"==a.type?a.value:a;a.event=b;this.response=a;this.teardown();this.deferred[this.response.type](g);this.emit("response",a)}};k(a.prototype);m.prototype={listen:function(b){c("listen");b=n(b||self,{receiver:!0});if(!this.ports.has(b))return b.addListener(this.onMessage,this.listen),
this.ports.add(b),this},unlisten:function(){var b=this;c("unlisten");this.ports.forEach(function(c){return c.removeListener(b.onMessage)})},onMessage:function(b){if(b.data.id&&b.data.type&&this.isRecipient(b.data.recipient)&&(c("receiver on message",b.data),b=new a(b),this.emit("message",b),!b.defaultPrevented))try{this.emit(b.type,b)}catch(g){throw b.error=g,b.respond(),g;}},isRecipient:function(b){return b==this.name||"*"==b||"*"==this.name},destroy:function(){this.unlisten();delete this.name;return this}};
k(m.prototype)},{"../emitter":2,"../utils":6,"./port-adaptors":4}],4:[function(d,f,l){function a(c){this.target=c}function m(c,b,a){c.addEventListener(b,a)}function p(c,b,a){b={data:b,source:self};a&&(b.ports=a);c.dispatchEvent(new MessageEvent("message",b))}var n=d("../utils").deferred;f.exports=function(c,b){if(!c)throw Error({1:"target is undefined"}[1]);if(c&&c.addListener)return c;var e=g[c.constructor.name];return e?e(c,b):new a(c,b)};var k=a.prototype={constructor:a,addListener:function(c){this.target.addEventListener("message",
c)},removeListener:function(c){this.target.removeEventListener("message",c)},postMessage:function(c,b){this.target.postMessage(c,b)}},g={HTMLIFrameElement:function(c){var b=e(c);return{addListener:function(b){window.addEventListener("message",b)},removeListener:function(b){window.removeEventListener("message",b)},postMessage:function(a,g){b.then(function(){return p(c.contentWindow,a,g)})}}},BroadcastChannel:function(c,b){function a(){var b=n();c.postMessage("ready?");m(c,"message",function q(a){"ready"==
a.data&&(c.removeEventListener("message",q),b.resolve())});return b.promise}var g=b&&b.receiver,e=b&&b.ready,e=e||g?Promise.resolve():a();g&&(c.postMessage("ready"),m(c,"message",function(b){"ready?"==b.data&&c.postMessage("ready")}));return{target:c,addListener:k.addListener,removeListener:k.removeListener,postMessage:function(b,a){e.then(function(){return c.postMessage(b,a)})}}},Window:function(a,b){var g=b&&b.ready||a===parent||a===self,g=g?Promise.resolve():e(a);return{addListener:function(b){window.addEventListener("message",
b)},removeListener:function(b){window.removeEventListener("message",b)},postMessage:function(b,e){g.then(function(){return p(a,b,e)})}}},SharedWorker:function(c){c.port.start();return new a(c.port)},SharedWorkerGlobalScope:function(){var a=[];return{postMessage:function(){},addListener:function(b,g){this.onconnect=function(b){b=b.ports[0];a.push(b);b.start();g(b)};self.addEventListener("connect",this.onconnect)},removeListener:function(b){self.removeEventListener("connect",this.onconnect);a.forEach(function(a){a.close();
a.removeEventListener("message",b)})}}}},e=function(){if("undefined"!=typeof window){var a=window.opener||window.parent,b=new WeakSet;a!=self&&("loading"===document.readyState?m(window,"DOMContentLoaded",function r(){window.removeEventListener("DOMContentLoaded",r);p(a,"load")}):p(a,"load"));m(self,"message",function(a){return"load"==a.data&&b.add(a.source)});return function(a){var g=a.contentWindow||a;if(b.has(g)||g==window.parent)return Promise.resolve();var c=n();m(window,"message",function q(b){"load"==
b.data&&b.source==g&&(window.removeEventListener("message",q),c.resolve())});return c.promise}}}()},{"../utils":6}],5:[function(d,f,l){function a(g){if(!(this instanceof a))return new a(g);n.Receiver.call(this,g);this.clients={};this.methods={};this.on("_disconnect",this.onDisconnect.bind(this)).on("_connect",this.onConnect.bind(this)).on("_method",this.onMethod.bind(this)).on("_off",this.onOff.bind(this)).on("_on",this.onOn.bind(this));this.destroy=this.destroy.bind(this);k("initialized",g)}function m(a){var e=
[].slice.call(arguments,1);return Error({4:'method "'+e[0]+"\" doesn't exist"}[a])}var p=d("./utils").uuid,n=d("./message");d=n.Receiver;f.exports=a;var k={0:function(){},1:function(a){return performance.mark("["+self.constructor.name+"][Service] - "+a)},2:function(a,e){for(var c=[],b=1;b<arguments.length;++b)c[b-1]=arguments[b];console.log.apply(console,[].concat(["[Service]"+("["+self.constructor.name+"]["+location.pathname+"]")+' - "'+a+'"'],$jscomp.arrayFromIterable(c)))}}[0];a.prototype=Object.create(d.prototype);
a.prototype.inWindow="Window"===constructor.name;a.prototype.method=function(a,e){this.methods[a]=e;return this};a.prototype.broadcast=function(a,e,c){var b=this;k("broadcast",a,e,c);this.eachClient(function(d){if(!c||~c.indexOf(d.id))k("broadcasting to",d.id),b.push(a,e,d.id,{noRespond:!0})});return this};a.prototype.push=function(a,e,c,b){b=b&&b.noRespond;var d=this.getClient(c);return n("_push").set({recipient:c,noRespond:b,data:{type:a,data:e}}).send(d.port)};a.prototype.eachClient=function(a){for(var e in this.clients)a(this.clients[e])};
a.prototype.getClient=function(a){return this.clients[a]};a.prototype.onConnect=function(a){k("connection attempt",a.data,this.name);var e=a.data,c=e.clientId;c&&e.service===this.name&&!this.clients[c]&&(this.emit("before-connect",a),a.defaultPrevented||(this.upgradeChannel(a),this.addClient(c,a.sourcePort),a.respond(),this.emit("connected",c),k("connected",c)))};a.prototype.upgradeChannel=function(a){if(!this.inWindow||"Window"!==a.data.originEnv){var e=a.event.ports;if(e=e&&e[0])a.setSourcePort(e),
this.listen(e),e.start();k("channel upgraded")}};a.prototype.onDisconnect=function(a){var e=this.clients[a.data];e&&(this.emit("before-disconnect",a),a.defaultPrevented||(this.removeClient(e.id),a.respond(),this.emit("disconnected",e.id),k("disconnected",e.id)))};a.prototype.onMethod=function(a){k("on method",a.data);this.emit("before-method",a);if(!a.defaultPrevented){var e=a.data,c=e.name,b=this.methods[c],d;if(!b)throw m(4,c);try{d=b.apply(this,e.args)}catch(f){a.error=f}a.respond(d)}};a.prototype.onOn=
function(a){k("on on",a.data);this.emit("on",a.data)};a.prototype.onOff=function(a){k("on off");this.emit("off",a.data)};a.prototype.addClient=function(a,e){this.clients[a]={id:a,port:e}};a.prototype.removeClient=function(a){delete this.clients[a]};a.prototype.plugin=function(a){a(this,{uuid:p});return this};a.prototype.disconnect=function(a){this.removeClient(a.id);n("disconnect").set({recipient:a.id,noRespond:!0}).send(a.port)};a.prototype.destroy=function(){delete this.clients;this.unlisten();
this.off()};f=a.prototype;f.broadcast=f.broadcast;f.destroy=f.destroy;f.method=f.method;f.plugin=f.plugin},{"./message":3,"./utils":6}],6:[function(d,f,l){l.uuid=function(){return"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,function(a){var d=16*Math.random()|0;return("x"==a?d:d&3|8).toString(16)})};l.deferred=function(){var a={};a.promise=new Promise(function(d,f){a.resolve=d;a.reject=f});return a}},{}]},{},[1])(1)});