Cleanup of EEPROM init code.

This commit is contained in:
flash 2024-04-18 19:37:59 +00:00
parent a08793d992
commit 1dcddffc03
4 changed files with 83 additions and 64 deletions

View file

@ -1,35 +1,37 @@
#include utility.js
#include eeprom/script.jsx
const MamiEEPROM = function() {
//
};
MamiEEPROM.init = (function() {
let initialised = false;
return () => {
return new Promise((resolve, reject) => {
if(initialised)
return new Promise(resolve => resolve());
const MamiEEPROM = function(baseUrl, getAuthLine) {
if(typeof baseUrl !== 'string')
throw 'baseUrl must be a string';
if(typeof getAuthLine !== 'function')
throw 'getAuthLine must be a function';
const script = $e({
tag: 'script',
attrs: {
charset: 'utf-8',
type: 'text/javascript',
src: `${futami.get('eeprom2')}/scripts/eepromv1a.js`,
onload: () => {
initialised = true;
resolve();
},
onerror: () => {
$r(script);
console.error('Failed to load EEPROM script!');
reject();
},
},
});
// when the pools rewrite happen, retrieve this from futami common
const appId = '1';
let client;
document.body.appendChild(script);
});
return {
get client() {
return client;
},
init: async () => {
await MamiEEPROMLoadScript(baseUrl);
client = new EEPROM(appId, baseUrl, getAuthLine);
},
create: fileInput => {
if(client === undefined)
throw 'eeprom client is uninitialised';
return client.create(fileInput);
},
delete: async fileInfo => {
if(client === undefined)
throw 'eeprom client is uninitialised';
await client.delete(fileInfo);
},
};
})();
};

View file

@ -0,0 +1,19 @@
#include utility.js
const MamiEEPROMLoadScript = baseUrl => {
return new Promise((resolve, reject) => {
if(typeof baseUrl !== 'string')
throw 'baseUrl must be a string';
const src = `${baseUrl}/scripts/eepromv1a.js`;
let script = $q(`script[src="${src}"]`);
if(script instanceof Element) {
resolve();
return;
}
script = <script src={src} onload={() => { resolve(); }} onerror={() => { $r(script); reject(); }} />;
document.body.appendChild(script);
});
};

View file

@ -1,4 +1,4 @@
window.Umi = { UI: {}, Protocol: { SockChat: { Protocol: {} } } };
window.Umi = { UI: {} };
#include animate.js
#include common.js
@ -422,14 +422,12 @@ window.Umi = { UI: {}, Protocol: { SockChat: { Protocol: {} } } };
Umi.UI.InputMenus.Add('emotes', 'Emoticons');
let doUpload;
MamiEEPROM.init()
ctx.eeprom = new MamiEEPROM(futami.get('eeprom2'), MamiMisuzuAuth.getLine);
ctx.eeprom.init()
.catch(ex => {
console.log('Failed to initialise EEPROM.', ex);
ctx.eeprom = undefined;
})
.then(() => {
ctx.eeprom = new EEPROM('1', futami.get('eeprom2'), MamiMisuzuAuth.getLine);
Umi.UI.Menus.Add('uploads', 'Upload History', !FUTAMI_DEBUG);
doUpload = async file => {

View file

@ -43,38 +43,38 @@ Umi.UI.Menus = (function() {
return {
Add: function(baseId, title, initiallyHidden) {
if(ids.indexOf(baseId) < 0) {
ids.push(baseId);
if(ids.includes(baseId))
return;
ids.push(baseId);
const menuClass = [sidebarMenu, sidebarMenu + '--' + baseId];
const iconClass = [sidebarSelectorMode, sidebarSelectorMode + '--' + baseId];
const menuClass = [sidebarMenu, `${sidebarMenu}--${baseId}`];
const iconClass = [sidebarSelectorMode, `${sidebarSelectorMode}--${baseId}`];
const menus = $i('umi-menus');
const icons = $i('umi-menu-icons');
const menus = $i('umi-menus');
const icons = $i('umi-menu-icons');
if(menus.children.length < 1) {
menuClass.push(sidebarMenuActive);
iconClass.push(sidebarSelectorModeActive);
}
if(initiallyHidden) {
menuClass.push(sidebarMenuHidden);
iconClass.push(sidebarSelectorModeHidden);
}
icons.appendChild($e({
attrs: {
id: 'umi-menu-icons-' + baseId,
classList: iconClass,
title: title,
onclick: function() {
activate(baseId);
},
},
}));
menus.appendChild($e({ attrs: { 'class': menuClass, id: 'umi-menus-' + baseId } }));
if(menus.children.length < 1) {
menuClass.push(sidebarMenuActive);
iconClass.push(sidebarSelectorModeActive);
}
if(initiallyHidden) {
menuClass.push(sidebarMenuHidden);
iconClass.push(sidebarSelectorModeHidden);
}
icons.appendChild($e({
attrs: {
id: `umi-menu-icons-${baseId}`,
classList: iconClass,
title: title,
onclick: function() {
activate(baseId);
},
},
}));
menus.appendChild($e({ attrs: { 'class': menuClass, id: `umi-menus-${baseId}` } }));
},
Get: function(baseId, icon) {
const id = (icon ? 'umi-menu-icons' : 'umi-menus') + '-' + baseId;