From 187b4beb70a460d94c155b0812d3aacc89b75f48 Mon Sep 17 00:00:00 2001 From: velvettear Date: Tue, 8 Feb 2022 14:07:59 +0100 Subject: [PATCH] moved cache to own .js file --- libs/cache.js | 95 +++++++++++++++++++++++++++++++++++++++++++++++++++ libs/modep.js | 42 ++++++++++++----------- 2 files changed, 118 insertions(+), 19 deletions(-) create mode 100644 libs/cache.js diff --git a/libs/cache.js b/libs/cache.js new file mode 100644 index 0000000..47b02f5 --- /dev/null +++ b/libs/cache.js @@ -0,0 +1,95 @@ +const logger = require('./logger.js'); + +var banks = undefined; +var pedalboards = undefined; +var defaultPedalboard = undefined; +var currentPedalboard = undefined; +var currentPedals = undefined; + +function getBanks() { + return banks; +} + +function clearBanks() { + logger.debug('clearing bank(s)'); + setBanks(undefined); +} + +function setBanks(value) { + logger.debug('caching ' + value.length + ' bank(s)'); + banks = value; +} + +function getPedalboards() { + return pedalboards; +} + +function clearPedalboards() { + logger.debug('clearing pedalboard(s)'); + setPedalboards(undefined); +} + +function setPedalboards(value) { + logger.debug('caching ' + value.length + ' pedalboard(s)'); + pedalboards = value; +} + +function getDefaultPedalboard() { + return defaultPedalboard; +} + +function clearDefaultPedalboard() { + logger.debug('clearing default pedalboard'); + setDefaultPedalboard(undefined); +} + +function setDefaultPedalboard(value) { + logger.debug('caching default pedalboard'); + defaultPedalboard = value; +} + +function getCurrentPedalboard() { + return currentPedalboard; +} + +function clearCurrentPedalboard() { + logger.debug('clearing current pedalboard'); + setCurrentPedalboard(undefined); +} + +function setCurrentPedalboard(value) { + logger.debug('caching current pedalboard'); + currentPedalboard = value; +} + +function getCurrentPedals() { + return currentPedals; +} + +function clearCurrenPedals() { + logger.debug('clearing current pedal(s))'); + setCurrentPedals(undefined); +} + +function setCurrentPedals(value) { + logger.debug('caching ' + value.length + ' current pedal(s)'); + currentPedals = value; +} + +module.exports = { + getBanks, + clearBanks, + setBanks, + getPedalboards, + clearPedalboards, + setPedalboards, + getDefaultPedalboard, + clearDefaultPedalboard, + setDefaultPedalboard, + getCurrentPedalboard, + clearCurrentPedalboard, + setCurrentPedalboard, + getCurrentPedals, + clearCurrenPedals, + setCurrentPedals +} \ No newline at end of file diff --git a/libs/modep.js b/libs/modep.js index cb0c5e7..0644df4 100644 --- a/libs/modep.js +++ b/libs/modep.js @@ -2,17 +2,14 @@ const config = require('../config.json'); const util = require('./util.js'); const constants = require('./constants.js'); const logger = require('./logger.js'); +const cache = require('./cache.js'); const path = require('path'); const fs = require('fs'); const { spawn } = require('child_process') const ttl2jsonld = require('@frogcat/ttl2jsonld').parse; // TODO: MOVE TO CACHE.JS -var banks = undefined; -var pedalboards = undefined; -var defaultPedalboard = undefined; -var currentPedalboard = undefined; -var currentPedals = undefined; + function reset() { return new Promise(function (resolve, reject) { @@ -24,6 +21,7 @@ function reset() { function getBanks() { return new Promise(function (resolve, reject) { + var banks = cache.getBanks(); if (banks != undefined) { return resolve(banks); } @@ -42,6 +40,7 @@ function getBanks() { bank.id = index; } banks = util.sortById(banks); + cache.setBanks(banks); return resolve(banks); }) .catch(reject); @@ -66,6 +65,7 @@ function getBankById(bankId) { function getPedalboards() { return new Promise(function (resolve, reject) { + var pedalboards = cache.getPedalboards(); if (pedalboards != undefined) { return resolve(pedalboards); } @@ -86,10 +86,10 @@ function getPedalboards() { // return resolve(fake); util.httpGET(config.modep.host, config.modep.port, '/pedalboard/list') - .then(function (data) { + .then(function (pedalboards) { var id = 1; - for (var index = 0; index < data.length; index++) { - var pedalboard = data[index]; + for (var index = 0; index < pedalboards.length; index++) { + var pedalboard = pedalboards[index]; if (pedalboard.bundle == constants.PEDALBOARD_DEFAULT) { pedalboard.id = 0; defaultPedalboard = pedalboard; @@ -98,7 +98,8 @@ function getPedalboards() { pedalboard.id = id; id++; } - pedalboards = util.sortById(data); + pedalboards = util.sortById(pedalboards); + cache.setPedalboards(pedalboards); return resolve(pedalboards); }) .catch(reject); @@ -107,17 +108,22 @@ function getPedalboards() { function getDefaultPedalboard() { return new Promise(function (resolve, reject) { + var defaultPedalboard = cache.getDefaultPedalboard(); if (defaultPedalboard != undefined) { return resolve(defaultPedalboard); } getPedalboardByBundle(constants.PEDALBOARD_DEFAULT) - .then(resolve) + .then(function (defaultPedalboard) { + cache.setDefaultPedalboard(defaultPedalboard); + return resolve(defaultPedalboard); + }) .catch(reject); }); } function getCurrentPedalboard() { return new Promise(function (resolve, reject) { + var currentPedalboard = cache.getCurrentPedalboard(); if (currentPedalboard != undefined && currentPedalboard.id != undefined) { return resolve(currentPedalboard); } @@ -133,8 +139,8 @@ function getCurrentPedalboard() { // PRODUCTION util.httpGET(config.modep.host, config.modep.port, '/pedalboard/current') .then(getPedalboardByBundle) - .then(function (pedalboard) { - currentPedalboard = pedalboard; + .then(function (currentPedalboard) { + cache.setCurrentPedalboard(currentPedalboard); return resolve(currentPedalboard) }) .catch(reject); @@ -213,12 +219,16 @@ function getCurrentPedalById(id) { function getCurrentPedals() { return new Promise(function (resolve, reject) { + var currentPedals = cache.getCurrentPedals(); if (currentPedals) { return resolve(currentPedals); } getCurrentPedalboard() .then(parseCurrentPedalboard) - .then(resolve) + .then(function (pedals) { + cache.setCurrentPedals(pedals); + return resolve(pedals); + }) .catch(reject); }); } @@ -341,12 +351,6 @@ function setControlValue(pedalId, requestParams) { }); } -function sendValueToControl(pedalId, controlId, value) { - return new Promise(function (resolve, reject) { - - }); -} - function setPedalboardById(pedalboardId) { return new Promise(function (resolve, reject) { if (pedalboardId == undefined) {