VulpesOS/assets/webapps/music.gaiamobile.org/components/bridge/service.min.js
OverSu b38dea0597 Publish Vulpes Preview 15 sources for desktop, Android and Tundra
Share Gaia assets and adapters, keep private profiles and development documents local, and update both public READMEs.
2026-09-30 23:13:03 +02:00

24 lines
12 KiB
JavaScript

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