Fixing loop when setting variables

Setting a variable would makes the application enter in an infinite loop of events (between all the scripts and the back)
This fix makes sure a variable does not emit any event if it is changed to a value it already has.
This commit is contained in:
David Négrier 2021-07-23 11:50:03 +02:00
parent 84df25f863
commit 2aba6b1c27
6 changed files with 61 additions and 23 deletions

View file

@ -134,7 +134,17 @@ export class VariablesManager {
return variable;
}
setVariable(name: string, value: string, user: User): string | undefined {
/**
* Sets the variable.
*
* Returns who is allowed to read the variable (the readableby property) or "undefined" if anyone can read it.
* Also, returns "false" if the variable was not modified (because we set it to the value it already has)
*
* @param name
* @param value
* @param user
*/
setVariable(name: string, value: string, user: User): string | undefined | false {
let readableBy: string | undefined;
if (this.variableObjects) {
const variableObject = this.variableObjects.get(name);
@ -159,6 +169,11 @@ export class VariablesManager {
readableBy = variableObject.readableBy;
}
// If the value is not modified, return false
if (this._variables.get(name) === value) {
return false;
}
this._variables.set(name, value);
variablesRepository
.saveVariable(this.roomUrl, name, value)