diff --git a/back/src/Services/Repository/RedisVariablesRepository.ts b/back/src/Services/Repository/RedisVariablesRepository.ts index f59e37ab..70ff447a 100644 --- a/back/src/Services/Repository/RedisVariablesRepository.ts +++ b/back/src/Services/Repository/RedisVariablesRepository.ts @@ -8,12 +8,16 @@ import { VariablesRepositoryInterface } from "./VariablesRepositoryInterface"; export class RedisVariablesRepository implements VariablesRepositoryInterface { private readonly hgetall: OmitThisParameter<(arg1: string) => Promise<{ [p: string]: string }>>; private readonly hset: OmitThisParameter<(arg1: [string, ...string[]]) => Promise>; + private readonly hdel: OmitThisParameter<(arg1: string, arg2: string) => Promise>; - constructor(redisClient: RedisClient) { + + constructor(private redisClient: RedisClient) { // @eslint-disable-next-line @typescript-eslint/unbound-method this.hgetall = promisify(redisClient.hgetall).bind(redisClient); // @eslint-disable-next-line @typescript-eslint/unbound-method this.hset = promisify(redisClient.hset).bind(redisClient); + // @eslint-disable-next-line @typescript-eslint/unbound-method + this.hdel = promisify(redisClient.hdel).bind(redisClient); } /** @@ -26,11 +30,13 @@ export class RedisVariablesRepository implements VariablesRepositoryInterface { } async saveVariable(roomUrl: string, key: string, value: string): Promise { - // TODO: handle the case for "undefined" - // TODO: handle the case for "undefined" - // TODO: handle the case for "undefined" - // TODO: handle the case for "undefined" - // TODO: handle the case for "undefined" + + // The value is passed to JSON.stringify client side. If value is "undefined", JSON.stringify returns "undefined" + // which is translated to empty string when fetching the value in the pusher. + // Therefore, empty string server side == undefined client side. + if (value === '') { + return this.hdel(roomUrl, key); + } // TODO: SLOW WRITING EVERY 2 SECONDS WITH A TIMEOUT diff --git a/front/src/Connexion/RoomConnection.ts b/front/src/Connexion/RoomConnection.ts index b23f9549..521a8473 100644 --- a/front/src/Connexion/RoomConnection.ts +++ b/front/src/Connexion/RoomConnection.ts @@ -189,7 +189,11 @@ export class RoomConnection implements RoomConnection { const variables = new Map(); for (const variable of roomJoinedMessage.getVariableList()) { - variables.set(variable.getName(), JSON.parse(variable.getValue())); + try { + variables.set(variable.getName(), JSON.parse(variable.getValue())); + } catch (e) { + console.error('Unable to unserialize value received from server for variable "'+variable.getName()+'". Value received: "'+variable.getValue()+'". Error: ', e); + } } this.userId = roomJoinedMessage.getCurrentuserid(); @@ -652,7 +656,11 @@ export class RoomConnection implements RoomConnection { const serializedValue = message.getValue(); let value: unknown = undefined; if (serializedValue) { - value = JSON.parse(serializedValue); + try { + value = JSON.parse(serializedValue); + } catch (e) { + console.error('Unable to unserialize value received from server for variable "'+name+'". Value received: "'+serializedValue+'". Error: ', e); + } } callback(name, value); }); diff --git a/maps/tests/Variables/shared_variables.html b/maps/tests/Variables/shared_variables.html index c0a586d8..21e0b998 100644 --- a/maps/tests/Variables/shared_variables.html +++ b/maps/tests/Variables/shared_variables.html @@ -28,6 +28,11 @@ console.log(WA.state.loadVariable('textField')); document.getElementById('placeholder').innerText = WA.state.loadVariable('textField'); }); + + document.getElementById('setUndefined').addEventListener('click', () => { + WA.state.textField = undefined; + document.getElementById('textField').value = ''; + }); }); }) @@ -35,6 +40,8 @@ + +