Fixing "has/in" on variables proxy object

When using WA.state, using `"myVariable" in WA.state` would always return false.
This is now fixed by adding a "has" method on the Proxy class.

Also, added a `WA.state.hasVariable` method.
This commit is contained in:
David Négrier 2021-08-27 11:26:08 +02:00
parent 29c1ea25c7
commit a0d3685227
2 changed files with 11 additions and 0 deletions

View File

@ -9,6 +9,7 @@ Moreover, `WA.state` functions can be used to persist this state across reloads.
``` ```
WA.state.saveVariable(key : string, data : unknown): void WA.state.saveVariable(key : string, data : unknown): void
WA.state.loadVariable(key : string) : unknown WA.state.loadVariable(key : string) : unknown
WA.state.hasVariable(key : string) : boolean
WA.state.onVariableChange(key : string).subscribe((data: unknown) => {}) : Subscription WA.state.onVariableChange(key : string).subscribe((data: unknown) => {}) : Subscription
WA.state.[any property]: unknown WA.state.[any property]: unknown
``` ```

View File

@ -62,6 +62,10 @@ export class WorkadventureStateCommands extends IframeApiContribution<Workadvent
return variables.get(key); return variables.get(key);
} }
hasVariable(key: string): boolean {
return variables.has(key);
}
onVariableChange(key: string): Observable<unknown> { onVariableChange(key: string): Observable<unknown> {
let subject = variableSubscribers.get(key); let subject = variableSubscribers.get(key);
if (subject === undefined) { if (subject === undefined) {
@ -85,6 +89,12 @@ const proxyCommand = new Proxy(new WorkadventureStateCommands(), {
target.saveVariable(p.toString(), value); target.saveVariable(p.toString(), value);
return true; return true;
}, },
has(target: WorkadventureStateCommands, p: PropertyKey): boolean {
if (p in target) {
return true;
}
return target.hasVariable(p.toString());
},
}) as WorkadventureStateCommands & { [key: string]: unknown }; }) as WorkadventureStateCommands & { [key: string]: unknown };
export default proxyCommand; export default proxyCommand;