Setting a variable to undefined now removes it from server-side storage.

This commit is contained in:
David Négrier 2021-07-19 18:46:33 +02:00
parent d955ddfe82
commit ac3d1240ae
3 changed files with 29 additions and 8 deletions

View File

@ -8,12 +8,16 @@ import { VariablesRepositoryInterface } from "./VariablesRepositoryInterface";
export class RedisVariablesRepository implements VariablesRepositoryInterface { export class RedisVariablesRepository implements VariablesRepositoryInterface {
private readonly hgetall: OmitThisParameter<(arg1: string) => Promise<{ [p: string]: string }>>; private readonly hgetall: OmitThisParameter<(arg1: string) => Promise<{ [p: string]: string }>>;
private readonly hset: OmitThisParameter<(arg1: [string, ...string[]]) => Promise<number>>; private readonly hset: OmitThisParameter<(arg1: [string, ...string[]]) => Promise<number>>;
private readonly hdel: OmitThisParameter<(arg1: string, arg2: string) => Promise<number>>;
constructor(redisClient: RedisClient) {
constructor(private redisClient: RedisClient) {
// @eslint-disable-next-line @typescript-eslint/unbound-method // @eslint-disable-next-line @typescript-eslint/unbound-method
this.hgetall = promisify(redisClient.hgetall).bind(redisClient); this.hgetall = promisify(redisClient.hgetall).bind(redisClient);
// @eslint-disable-next-line @typescript-eslint/unbound-method // @eslint-disable-next-line @typescript-eslint/unbound-method
this.hset = promisify(redisClient.hset).bind(redisClient); 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<number> { async saveVariable(roomUrl: string, key: string, value: string): Promise<number> {
// 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"
// TODO: handle the case for "undefined" // which is translated to empty string when fetching the value in the pusher.
// TODO: handle the case for "undefined" // Therefore, empty string server side == undefined client side.
// TODO: handle the case for "undefined" if (value === '') {
return this.hdel(roomUrl, key);
}
// TODO: SLOW WRITING EVERY 2 SECONDS WITH A TIMEOUT // TODO: SLOW WRITING EVERY 2 SECONDS WITH A TIMEOUT

View File

@ -189,7 +189,11 @@ export class RoomConnection implements RoomConnection {
const variables = new Map<string, unknown>(); const variables = new Map<string, unknown>();
for (const variable of roomJoinedMessage.getVariableList()) { 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(); this.userId = roomJoinedMessage.getCurrentuserid();
@ -652,7 +656,11 @@ export class RoomConnection implements RoomConnection {
const serializedValue = message.getValue(); const serializedValue = message.getValue();
let value: unknown = undefined; let value: unknown = undefined;
if (serializedValue) { 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); callback(name, value);
}); });

View File

@ -28,6 +28,11 @@
console.log(WA.state.loadVariable('textField')); console.log(WA.state.loadVariable('textField'));
document.getElementById('placeholder').innerText = 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 = '';
});
}); });
}) })
</script> </script>
@ -35,6 +40,8 @@
<body> <body>
<input type="text" id="textField" /> <input type="text" id="textField" />
<button id="setUndefined">Delete variable</button>
<button id="btn">Display textField variable value</button> <button id="btn">Display textField variable value</button>
<div id="placeholder"></div> <div id="placeholder"></div>
</body> </body>