const logger = require('./logger.js'); const constants = require('./constants.js'); const timeDiff = require('./util.js').timeDiff; let modep; const cache = new Map(); async function fill() { const timestamp = new Date(); clear(); logger.debug('filling cache...'); try { if (modep === undefined) { modep = require('./modep.js'); } await modep.getBanks(); await modep.getPedalboards(); await modep.getDefaultPedalboard(); await modep.getCurrentPedalboard(); await modep.getCurrentPedals(); logger.debug('cache filled after ' + timeDiff(timestamp) + 'ms'); } catch (err) { logger.error('encountered an error while filling the cache after ' + timeDiff(timestamp) + 'ms'); logger.error(err); } } function clear() { logger.debug('clearing cache...'); cache.clear(); } function getValue(key) { return cache.get(key); } function setValue(key, value) { logger.debug('caching \'' + key + '\'...'); cache.set(key, value); } function resetValue(key) { cache.delete(key); } function getBanks() { return getValue(constants.CACHE_BANKS); } function clearBanks() { resetValue(constants.CACHE_BANKS); } function setBanks(value) { setValue(constants.CACHE_BANKS, value); } function getPedalboards() { return getValue(constants.CACHE_PEDALBOARDS); } function clearPedalboards() { resetValue(constants.CACHE_PEDALBOARDS); } function setPedalboards(value) { setValue(constants.CACHE_PEDALBOARDS, value); } function getCurrentPedalboard() { return getValue(constants.CACHE_PEDALBOARD_CURRENT); } function clearCurrentPedalboard() { resetValue(constants.CACHE_PEDALBOARD_CURRENT); } function setCurrentPedalboard(value) { setValue(constants.CACHE_PEDALBOARD_CURRENT, value); } function getDefaultPedalboard() { return getValue(constants.CACHE_PEDALBOARD_DEFAULT); } function clearDefaultPedalboard() { resetValue(constants.CACHE_PEDALBOARD_DEFAULT); } function setDefaultPedalboard(value) { setValue(constants.CACHE_PEDALBOARD_DEFAULT, value); } function getCurrentPedals() { return getValue(constants.CACHE_PEDALS); } function clearCurrentPedals() { resetValue(constants.CACHE_PEDALS); } function setCurrentPedals(value) { setValue(constants.CACHE_PEDALS, value); } function updateControl(pedalId, control) { if (pedalId === undefined || control === undefined) { return; } const pedals = getValue(constants.CACHE_PEDALS); if (pedals === undefined) { return; } let pedal; for (let pedalIndex = 0; pedalIndex < pedals.length; pedalIndex++) { if (pedals[pedalIndex].id !== pedalId) { continue; } pedal = pedals[index]; break; } if (pedal === undefined) { return; } for (let controlIndex = 0; controlIndex < pedal.controls.length; controlIndex++) { if (pedal.controls[controlIndex].id !== control.index) { continue; } pedal.controls[controlIndex] = control; setValue(constants.CACHE_PEDALS, pedals); return; } } module.exports = { fill, clear, getBanks, clearBanks, setBanks, getPedalboards, clearPedalboards, setPedalboards, getDefaultPedalboard, clearDefaultPedalboard, setDefaultPedalboard, getCurrentPedalboard, clearCurrentPedalboard, setCurrentPedalboard, getCurrentPedals, clearCurrentPedals, setCurrentPedals, updateControl }