VulpesOS/assets/webapps/system.gaiamobile.org/js/service.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

12 lines
No EOL
4.1 KiB
JavaScript

;'use strict';(function(exports){var DEBUG=false;exports.Service={_providers:new Map(),_services:new Map(),_requestsByService:new Map(),_requestsByProvider:new Map(),request:function(service){var requestItems=service.split(':');var args=Array.prototype.slice.call(arguments,1);var self=this;this.debug(requestItems);if(requestItems.length>1){var serverName=requestItems[0];var serviceName=requestItems[1];if(this._providers.get(serverName)){this.debug('service: '+serviceName+' is online, perform the request with '+args.concat());return Promise.resolve(this._providers.get(serverName)[serviceName].apply(self._providers.get(serverName),args));}else{return new Promise(function(resolve,reject){self.debug('service: '+service+' is offline, queue the task.');if(!self._requestsByProvider.has(serverName)){self._requestsByProvider.set(serverName,[]);}
self._requestsByProvider.get(serverName).push({service:serviceName,resolve:resolve,args:args});});}
return;}
if(this._services.has(service)){var server=this._services.get(service);this.debug('service ['+service+'] provider ['+server.name+'] is online, perform the task.');return Promise.resolve(server[service].apply(server,args));}else{this.debug('service: '+service+' is offline, queue the task.');var promise=new Promise(function(resolve,reject){self.debug('storing the requests...');if(!self._requestsByService.has(service)){self._requestsByService.set(service,[]);}
self._requestsByService.get(service).push({service:service,resolve:resolve,args:args});});return promise;}},register:function(service,server){var self=this;if(!this._providers.has(server.name)){this._providers.set(server.name,server);}
this.debug((server.name||'(Anonymous)')+' is registering service: ['+service+']');this.debug('checking awaiting requests by server..');if(this._requestsByProvider.has(server.name)){this._requestsByProvider.get(server.name).forEach(function(request){self.debug('resolving..',server,server.name,request.service,request.args);var returnValue=(typeof(server[request.service])==='function')?server[request.service].apply(server,request.args):server[request.service];request.resolve(returnValue);});this._requestsByProvider.delete(server.name);}
if(!this._services.has(service)){this._services.set(service,server);}else{this.debug('the service ['+service+'] has already been '+'registered by other server:',this._services.get(service).name);return;}
this.debug('checking awaiting requests by service..');if(this._requestsByService.has(service)){this._requestsByService.get(service).forEach(function(request){self.debug('resolving..',server,request.service);var returnValue=server[request.service].apply(server,request.args);request.resolve(returnValue);});this._requestsByService.delete(service);}},unregister:function(service,server){this._providers.delete(server.name);var se=this._services.get(service);if(se&&server===se){this._services.delete(service);}},_states:new Map(),_statesByState:new Map(),registerState:function(state,provider){this._states.set(provider.name,provider);if(!provider.name){console.warn('Service.registerState: Provider should have a name.',provider);}
this._statesByState.set(state,provider);},unregisterState:function(state,provider){this._states.delete(provider.name);var machine=this._statesByState.get(state);if(machine===provider){this._statesByState.delete(state);}},query:function(stateString){this.debug(stateString);var args=stateString.split('.');var state,provider;if(args.length>1){provider=this._states.get(args[0]);state=args[1];}else{state=args[0];provider=this._statesByState.get(state);}
if(!provider){this.debug('Provider not ready, return undefined state.');return undefined;}
if(typeof(provider[state])==='function'){var functionArgs=Array.prototype.slice.call(arguments,1);return provider[state].apply(provider,functionArgs);}else{return provider[state];}},_start:new Date().getTime()/1000,currentTime:function(){return(new Date().getTime()/1000-this._start).toFixed(3);},debug:function sys_debug(){if(DEBUG){console.log('[System]'+'['+window.Service.currentTime()+']'+
Array.slice(arguments).concat());}},get manifestURL(){return window.location.href.replace('index.html','manifest.webapp');}};})(window);