Share Gaia assets and adapters, keep private profiles and development documents local, and update both public READMEs.
5 lines
No EOL
2 KiB
JavaScript
5 lines
No EOL
2 KiB
JavaScript
|
|
define(['require'],function(require){'use strict';var mozApps=window.navigator.mozApps;var mozAppsMgmt=mozApps&&mozApps.mgmt;var AppsCache=function(){this._initPromise=null;this._apps=[];this._eventHandlers={};this._eventHandlers.oninstall=[];this._eventHandlers.onuninstall=[];};AppsCache.prototype={apps:function am_apps(){var self=this;return this._init().then(function(){return self._apps;});},_init:function am__init(){var self=this;if(!this._initPromise){this._initPromise=this._initApps().then(function(){self._initEvents();});}
|
|
return this._initPromise;},_initEvents:function am__initEvents(){var self=this;mozAppsMgmt.oninstall=function(evt){var newApp=evt.application;var existing=false;self._apps.some((app,i)=>{if(app.manifestURL===newApp.manifestURL){self._apps[i]=newApp;existing=true;return true;}});if(!existing){self._apps.push(newApp);}
|
|
self._eventHandlers.oninstall.forEach(function(eventHandler){eventHandler(evt);});};mozAppsMgmt.onuninstall=function(evt){var manifestURL=evt.application.manifestURL;var appRemoved=false;self._apps=self._apps.filter(app=>{if(app.manifestURL===manifestURL){appRemoved=true;return false;}
|
|
return true;});if(appRemoved){self._eventHandlers.onuninstall.forEach(function(eventHandler){eventHandler(evt);});}};},_initApps:function am__initApps(){var self=this;var promise=new Promise(function(resolve,reject){var req=mozAppsMgmt.getAll();req.onsuccess=function success(evt){self._apps=evt.target.result;resolve(self._apps);};req.onerror=function error(evt){console.log('failed to get installed apps');reject(req.error);};});return promise;},addEventListener:function am_addEventListener(eventName,callback){if(this._eventHandlers[eventName]){this._eventHandlers[eventName].push(callback);}},removeEventListener:function am_removeEventListener(eventName,callback){if(this._eventHandlers[eventName]){var index=this._eventHandlers[eventName].indexOf(callback);if(index>=0){this._eventHandlers[eventName].splice(index,1);}}},};var appsCache=new AppsCache();return appsCache;}); |