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(); logger.debug('filling cache...'); clear(); 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() { 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 getInfo() { return getValue(constants.CACHE_INFO); } function clearInfo() { resetValue(constants.CACHE_INFO); } function setInfo(value) { setValue(constants.CACHE_INFO, value); } module.exports = { fill, clear, getBanks, clearBanks, setBanks, getPedalboards, clearPedalboards, setPedalboards, getDefaultPedalboard, clearDefaultPedalboard, setDefaultPedalboard, getCurrentPedalboard, clearCurrentPedalboard, setCurrentPedalboard, getCurrentPedals, clearCurrentPedals, setCurrentPedals, getInfo, clearInfo, setInfo }