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