From 39b8bf46b422d434023441c4d736700b0b44f677 Mon Sep 17 00:00:00 2001 From: Axel Boberg Date: Sun, 21 Apr 2024 17:14:26 +0200 Subject: [PATCH] Always fetch the fresh variable data when populating Signed-off-by: Axel Boberg --- api/items.js | 9 ++++++--- api/variables.js | 9 +++++++++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/api/items.js b/api/items.js index 85750a9..7b32a18 100644 --- a/api/items.js +++ b/api/items.js @@ -225,9 +225,10 @@ function deepClone (obj) { * * @param { any } item * @param { any } type + * @param { any } values * @returns { any } The item with modified property values */ -function populateVariablesMutable (item, type) { +function populateVariablesMutable (item, type, values) { if (!item.data) { item.data = {} } @@ -239,7 +240,7 @@ function populateVariablesMutable (item, type) { const currentValue = objectPath.get(item.data, key) if (currentValue != null) { - objectPath.set(item.data, key, JSON.parse(variables.substituteInString(JSON.stringify(currentValue)))) + objectPath.set(item.data, key, JSON.parse(variables.substituteInString(JSON.stringify(currentValue), values))) } } @@ -263,7 +264,9 @@ async function playItem (id) { } const type = await types.getType(item.type) - const clone = populateVariablesMutable(deepClone(item), type) + const vars = await variables.getAllVariables() + const clone = populateVariablesMutable(deepClone(item), type, vars) + const delay = parseInt(clone?.data?.delay) if (delay && !Number.isNaN(delay)) { diff --git a/api/variables.js b/api/variables.js index ad1cd02..d4f755d 100644 --- a/api/variables.js +++ b/api/variables.js @@ -27,6 +27,15 @@ function getVariable (key) { } exports.getVariable = getVariable +/** + * Get all variables' values + * @returns { Promise. } + */ +async function getAllVariables () { + return state.get('variables') +} +exports.getAllVariables = getAllVariables + /** * Substitute variables for their * values in a string