From 1dcddffc03358365c0059c170dbc71e043f557c8 Mon Sep 17 00:00:00 2001 From: flashwave Date: Thu, 18 Apr 2024 19:37:59 +0000 Subject: [PATCH] Cleanup of EEPROM init code. --- src/mami.js/eeprom/eeprom.js | 64 ++++++++++++++++++----------------- src/mami.js/eeprom/script.jsx | 19 +++++++++++ src/mami.js/main.js | 8 ++--- src/mami.js/ui/menus.js | 56 +++++++++++++++--------------- 4 files changed, 83 insertions(+), 64 deletions(-) create mode 100644 src/mami.js/eeprom/script.jsx diff --git a/src/mami.js/eeprom/eeprom.js b/src/mami.js/eeprom/eeprom.js index 72d5356..7092ffd 100644 --- a/src/mami.js/eeprom/eeprom.js +++ b/src/mami.js/eeprom/eeprom.js @@ -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); + }, }; -})(); +}; diff --git a/src/mami.js/eeprom/script.jsx b/src/mami.js/eeprom/script.jsx new file mode 100644 index 0000000..cead304 --- /dev/null +++ b/src/mami.js/eeprom/script.jsx @@ -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 =