pbc/libs/cache.js

155 lines
3.5 KiB
JavaScript
Raw Normal View History

2022-02-08 14:07:59 +01:00
const logger = require('./logger.js');
2022-02-10 23:20:55 +01:00
const constants = require('./constants.js');
2022-03-10 00:32:38 +01:00
const timeDiff = require('./util.js').timeDiff;
2022-02-08 14:07:59 +01:00
2022-03-10 00:32:38 +01:00
let modep;
2022-02-10 23:20:55 +01:00
const cache = new Map();
2022-03-10 00:32:38 +01:00
async function fill() {
const timestamp = new Date();
clear();
2022-03-30 12:49:00 +02:00
logger.debug('filling cache...');
2022-03-10 00:36:59 +01:00
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);
2022-03-10 00:32:38 +01:00
}
}
2022-02-10 23:20:55 +01:00
function clear() {
2022-03-30 12:49:00 +02:00
logger.debug('clearing cache...');
2022-03-10 00:32:38 +01:00
cache.clear();
2022-02-10 23:20:55 +01:00
}
function getValue(key) {
return cache.get(key);
2022-02-10 23:20:55 +01:00
}
function setValue(key, value) {
logger.debug('caching \'' + key + '\'...');
cache.set(key, value);
2022-02-10 23:20:55 +01:00
}
function resetValue(key) {
cache.delete(key);
}
2022-02-08 14:07:59 +01:00
function getBanks() {
2022-02-10 23:20:55 +01:00
return getValue(constants.CACHE_BANKS);
2022-02-08 14:07:59 +01:00
}
function clearBanks() {
2022-02-10 23:20:55 +01:00
resetValue(constants.CACHE_BANKS);
2022-02-08 14:07:59 +01:00
}
function setBanks(value) {
2022-02-10 23:20:55 +01:00
setValue(constants.CACHE_BANKS, value);
2022-02-08 14:07:59 +01:00
}
function getPedalboards() {
2022-02-10 23:20:55 +01:00
return getValue(constants.CACHE_PEDALBOARDS);
2022-02-08 14:07:59 +01:00
}
function clearPedalboards() {
2022-02-10 23:20:55 +01:00
resetValue(constants.CACHE_PEDALBOARDS);
2022-02-08 14:07:59 +01:00
}
function setPedalboards(value) {
2022-02-10 23:20:55 +01:00
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);
2022-02-08 14:07:59 +01:00
}
function getDefaultPedalboard() {
2022-02-10 23:20:55 +01:00
return getValue(constants.CACHE_PEDALBOARD_DEFAULT);
2022-02-08 14:07:59 +01:00
}
function clearDefaultPedalboard() {
2022-02-10 23:20:55 +01:00
resetValue(constants.CACHE_PEDALBOARD_DEFAULT);
2022-02-08 14:07:59 +01:00
}
function setDefaultPedalboard(value) {
2022-02-10 23:20:55 +01:00
setValue(constants.CACHE_PEDALBOARD_DEFAULT, value);
2022-02-08 14:07:59 +01:00
}
2022-02-10 23:20:55 +01:00
function getCurrentPedals() {
return getValue(constants.CACHE_PEDALS);
2022-02-08 14:07:59 +01:00
}
2022-02-10 23:20:55 +01:00
function clearCurrentPedals() {
resetValue(constants.CACHE_PEDALS);
2022-02-08 14:07:59 +01:00
}
2022-02-10 23:20:55 +01:00
function setCurrentPedals(value) {
setValue(constants.CACHE_PEDALS, value);
2022-02-08 14:07:59 +01:00
}
2022-03-25 12:30:17 +01:00
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;
}
}
2022-02-08 14:07:59 +01:00
module.exports = {
2022-03-10 00:32:38 +01:00
fill,
2022-02-10 23:20:55 +01:00
clear,
2022-02-08 14:07:59 +01:00
getBanks,
clearBanks,
setBanks,
getPedalboards,
clearPedalboards,
setPedalboards,
getDefaultPedalboard,
clearDefaultPedalboard,
setDefaultPedalboard,
getCurrentPedalboard,
clearCurrentPedalboard,
setCurrentPedalboard,
getCurrentPedals,
2022-02-10 23:20:55 +01:00
clearCurrentPedals,
setCurrentPedals,
2022-03-25 12:30:17 +01:00
updateControl
2022-02-08 14:07:59 +01:00
}