VulpesOS/gaia/apps/email/js/app_self.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

50 lines
1.3 KiB
JavaScript
Executable file

/**
* Provides a wrapper over the mozApps.getSelf() API. Structured as an
* evt emitter, with "latest" support, and "latest" is overridden so
* that the call to getSelf() is delayed until the very first need
* for it.
*
* This allows code to have a handle on this module, instead of making
* the getSelf() call, and then only trigger the fetch via a call to
* latest, delaying the work until it is actually needed. Once getSelf()
* is fetched once, the result is reused.
*/
'use strict';
define(function(require, exports, module) {
var evt = require('evt');
var appSelf = evt.mix({}),
mozApps = navigator.mozApps,
oldLatest = appSelf.latest,
loaded = false;
if (!mozApps) {
appSelf.self = {};
loaded = true;
}
function loadSelf() {
mozApps.getSelf().onsuccess = function(event) {
loaded = true;
var app = event.target.result;
appSelf.self = app;
appSelf.emit('self', appSelf.self);
};
}
// Override latest to only do the work when something actually wants to
// listen.
appSelf.latest = function(id) {
if (!loaded) {
loadSelf();
}
if (id !== 'self') {
throw new Error(module.id + ' only supports "self" property');
}
return oldLatest.apply(this, arguments);
};
return appSelf;
});