Share Gaia assets and adapters, keep private profiles and development documents local, and update both public READMEs.
25 lines
12 KiB
JavaScript
Executable file
25 lines
12 KiB
JavaScript
Executable file
(function(q){"object"===typeof exports&&"undefined"!==typeof module?module.exports=q():"function"===typeof define&&define.amd?define([],q):("undefined"!==typeof window?window:"undefined"!==typeof global?global:"undefined"!==typeof self?self:this).bridge=q()})(function(){return function e(l,h,d){function k(f,p){if(!h[f]){if(!l[f]){var g="function"==typeof require&&require;if(!p&&g)return g(f,!0);if(m)return m(f,!0);g=Error("Cannot find module '"+f+"'");throw g.code="MODULE_NOT_FOUND",g;}g=h[f]={exports:{}};
|
|
l[f][0].call(g.exports,function(b){var a=l[f][1][b];return k(a?a:b)},g,g.exports,e,l,h,d)}return h[f].exports}for(var m="function"==typeof require&&require,f=0;f<d.length;f++)k(d[f]);return k}({1:[function(e,l,h){var d=l.exports=self.bridge||{};d.client=e("../src/client");"u"!="undefined"[0]?(void 0)([],function(){return d}):self.bridge=d},{"../src/client":2}],2:[function(e,l,h){function d(a,c,b){if(!(this instanceof d))return new d(a,c,b);"object"==typeof a&&(c=a.endpoint,b=a.timeout,a=a.service);
|
|
this.id=p();this.service=a;this.timeout=b;this.endpoint=c||this.endpoint;if(!this.endpoint)throw k(1);this.setPort(this.endpoint);this.pending=new Set;this.receiver=n.receiver(this.id).on("_push",this.onPush.bind(this));g("initialized",a)}function k(a,c){c=[].slice.call(arguments,1);return Error({1:"an endpoint must be defined",2:'Unable to establish a connection with "'+c[0]+'". Either the target endpoint is not alive or the Service is not `.listen()`ing.',3:'Method "'+c[0]+"\" didn't get a response. Either the target endpoint is not alive or the Service is not `.listen()`ing."}[a])}
|
|
var m=e("./message/port-adaptors"),f=e("./emitter"),n=e("./message"),p=e("./utils").uuid;l.exports=d;var g={0:function(){},1:function(a){return performance.mark("["+self.constructor.name+"][Client] - "+a)},2:function(a,c){c=[].slice.call(arguments,1);console.log.apply(console,[].concat(["[Client]"+("["+self.constructor.name+"]["+location.pathname+"]")+' - "'+a+'"'],c))}}[0],b=constructor.name;d.prototype={connect:function(){var a=this;g("connect");if(this.connected)return this.connected;g("connecting...",
|
|
this.service);var c=new MessageChannel;this.channel=c.port1;this.channel.start();var r={clientId:this.id,service:this.service,originEnv:b};return this.connected=this.message("_connect").set("transfer",[c.port2]).set("data",r).listen(c.port1).send().then(function(c){g("connected",c);c.event.target===a.channel?a.setPort(a.channel):(a.channel.close(),delete a.channel);a.receiver.listen(a.port)}).catch(function(c){"timeout"==(c&&c.message)&&(c=k(2,a.service),console.error(c.message));throw c;})},disconnect:function(a){var c=
|
|
this;if(!this.connected)return Promise.resolve();g("disconnecting ...");a={noRespond:a&&a.noRespond,data:this.id};this.cancelPending();return this.message("_disconnect").set(a).send().then(function(){return c.onDisconnected()})},method:function(a,c){var b=this;c=[].slice.call(arguments,1);return this.connect().then(function(){g("method",a);return b.message("_method").set({recipient:b.service,data:{name:a,args:c}}).send()}).then(function(a){return a.value}).catch(function(c){"timeout"==(c&&c.message)&&
|
|
(c=k(3,a),console.error(c.message));throw c;})},plugin:function(a){a(this,{Emitter:f,uuid:p});return this},message:function(a){var c=this;g("create message",a);var b=n(a).set("port",this.port).set("timeout",this.timeout).on("response",function(){return c.pending.delete(b)}).on("cancel",function(){return c.pending.delete(b)});this.pending.add(b);return b},cancelPending:function(){g("cancel pending");this.pending.forEach(function(a){a.cancel()});this.pending.clear()},pendingResponded:function(){var a=
|
|
[];this.pending.forEach(function(c){return a.push(c.responded)});return Promise.all(a)},onPush:function(a){g("on push",a.data);this._emit(a.data.type,a.data.data)},onDisconnected:function(){var a=this;delete this.connected;this.pendingResponded().then(function(){g("disconnected");a.channel&&a.channel.close();a._emit("disconnected")})},setPort:function(a){g("set port");this.port=m(a)},destroy:function(){var a=this;return this.disconnect().then(function(){a.destroyed||(g("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};d.prototype.on=function(a,c){var b=this;this.connect().then(function(){g("bind on",a);f.prototype.on.call(b,a,c);b.message("_on").set("noRespond",!0).set("data",{name:a,clientId:b.id}).send(b.port)});return this};d.prototype.off=function(a,c){var b=this;this.connect().then(function(){f.prototype.off.call(b,a,c);b.message("_off").set("noRespond",!0).set("data",{name:a,clientId:b.id}).send(b.port)});
|
|
return this}},{"./emitter":3,"./message":4,"./message/port-adaptors":5,"./utils":6}],3:[function(e,l,h){function d(k){if(k)return Object.assign(k,d.prototype)}l.exports=d;d.prototype={on:function(d,e){this._callbacks||(this._callbacks={});this._callbacks[d]||(this._callbacks[d]=[]);this._callbacks[d].push(e);return this},off:function(d,e){if(this._callbacks)switch(arguments.length){case 0:this._callbacks={};break;case 1:delete this._callbacks[d];break;default:var f=this._callbacks[d];if(!f)return;
|
|
var h=f.indexOf(e);~h&&f.splice(h,1)}return this},emit:function(d,e){if(this._callbacks)for(var f=this._callbacks[d]||[],f=f.concat(this._callbacks["*"]||[]),h=0;h<f.length;h++)f[h].call(this,e,d);return this}};e=d.prototype;e.off=e.off;e.on=e.on},{}],4:[function(e,l,h){function d(a){this.cancelled=!1;this.listeners=[];this.deferred=p();this.onMessage=this.onMessage.bind(this);this.onTimeout=this.onTimeout.bind(this);"object"===typeof a?this.setupInbound(a):this.setupOutbound(a);b("initialized",a)}
|
|
function k(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 m(a,c){c=[].slice.call(arguments,1);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=e("./port-adaptors"),n=e("../emitter");e=e("../utils");var p=e.deferred,g=e.uuid;h=l.exports=function(a){return new d(a)};h.receiver=
|
|
function(a,c){return new k(a,c)};h.Receiver=k;h.Message=d;var b={0:function(){},1:function(a){return performance.mark("["+self.constructor.name+"][Message] - "+a)},2:function(a,c){c=[].slice.call(arguments,1);console.log.apply(console,[].concat(["[Message]"+("["+self.constructor.name+"]["+location.pathname+"]")+' - "'+a+'"'],c))}}[0];d.prototype={setupOutbound:function(a){this.id=g();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 m(1);
|
|
var c=this.serialize(),d=!this.noRespond;this.port=a?f(a):this.port;if(!this.port)throw m(3);d?(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||1E3))},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(m(4));this.teardown()},listen:function(a){b("add response listener",a);a=f(a);a.addListener(this.onMessage);this.listeners.push(a);return this},unlisten:function(){var a=this;b("remove response listeners");this.listeners.forEach(function(c){return c.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);f({type:"resolve",value:a})}function d(a){a=a&&a.message||a;b("reject",a);f({type:"reject",value:a})}function f(a){e.response=a;e.sourcePort.postMessage({id:e.id,response:a},e.transfer);b("responded with:",a)}b("respond",a);if(this.hasResponded)throw m(2);if(this.sourcePort&&!this.noRespond){var e=this;this.hasResponded=!0;a instanceof Error&&d(a);Promise.resolve(a).then(c,
|
|
d).catch(d)}},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,d="reject"==c.type?c.value:c;c.event=a;this.response=c;this.teardown();this.deferred[this.response.type](d);this.emit("response",c)}};n(d.prototype);k.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(c){c.removeListener(a.onMessage,a.unlisten)})},onMessage:function(a){if(a.data.id&&a.data.type&&this.isRecipient(a.data.recipient)&&(b("receiver on message",a.data),a=new d(a),this.emit("message",a),!a.defaultPrevented))try{this.emit(a.type,a)}catch(c){throw a.respond(c),c;}},isRecipient:function(a){return a==this.name||"*"==a||"*"==this.name},destroy:function(){this.unlisten();delete this.name;return this}};
|
|
n(k.prototype)},{"../emitter":3,"../utils":6,"./port-adaptors":5}],5:[function(e,l,h){function d(b){this.target=b}function k(b,a,c){b.addEventListener(a,c)}function m(b,a,c){a={data:a,source:self};c&&(a.ports=c);b.dispatchEvent(new MessageEvent("message",a))}var f=e("../utils").deferred;l.exports=function(b,a){if(!b)throw Error({1:"target is undefined"}[1]);if(b&&b.addListener)return b;var c=p[b.constructor.name];return c?c(b,a):new d(b,a)};var n=d.prototype={constructor:d,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)}},p={HTMLIFrameElement:function(b){var a=g(b);return{addListener:function(a,b){window.addEventListener("message",a)},removeListener:function(a,b){window.removeEventListener("message",a)},postMessage:function(c,d){a.then(function(){return m(b.contentWindow,c,d)})}}},BroadcastChannel:function(b,a){function c(){var a=f();b.postMessage("ready?");k(b,"message",function t(c){"ready"==
|
|
c.data&&(b.removeEventListener("message",t),a.resolve())});return a.promise}var d=a&&a.receiver,e=a&&a.ready,e=e||d?Promise.resolve():c();d&&(b.postMessage("ready"),k(b,"message",function(a){"ready?"==a.data&&b.postMessage("ready")}));return{target:b,addListener:n.addListener,removeListener:n.removeListener,postMessage:function(a,c){e.then(function(){return b.postMessage(a,c)})}}},Window:function(b,a){var c=a&&a.ready||b===parent||b===self,c=c?Promise.resolve():g(b);return{addListener:function(a,
|
|
c){window.addEventListener("message",a)},removeListener:function(a,c){window.removeEventListener("message",a)},postMessage:function(a,d){c.then(function(){return m(b,a,d)})}}},SharedWorker:function(b){b.port.start();return new d(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,c){self.removeEventListener("connect",
|
|
this.onconnect);b.forEach(function(a){a.close();c(a)})}}}},g=function(){if("undefined"!=typeof window){var b=window.opener||window.parent,a=new WeakSet;b!=self&&k(window,"DOMContentLoaded",function r(){window.removeEventListener("DOMContentLoaded",r);m(b,"load")});k(self,"message",function(b){return"load"==b.data&&a.add(b.source)});return function(b){var d=b.contentWindow||b;if(a.has(d)||d==window.parent)return Promise.resolve();var e=f();k(window,"message",function t(a){"load"==a.data&&a.source==
|
|
d&&(window.removeEventListener("message",t),e.resolve())});return e.promise}}}()},{"../utils":6}],6:[function(e,l,h){h.uuid=function(){return"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,function(d){var e=16*Math.random()|0;return("x"==d?e:e&3|8).toString(16)})};h.deferred=function(){var d={};d.promise=new Promise(function(e,h){d.resolve=e;d.reject=h});return d}},{}]},{},[1])(1)});
|