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 =