Skip to content
Snippets Groups Projects
Commit 02cd4983 authored by Loïc Correnson's avatar Loïc Correnson
Browse files

[ivette/states] hold synchronized value

parent 0c2299e1
No related branches found
No related tags found
No related merge requests found
...@@ -284,10 +284,11 @@ class SyncState<A> { ...@@ -284,10 +284,11 @@ class SyncState<A> {
} }
getValue(): A | undefined { getValue(): A | undefined {
if (!this.upToDate && Server.isRunning()) { const running = Server.isRunning();
if (!this.upToDate && running) {
this.update(); this.update();
} }
return this.value; return running ? this.value : undefined;
} }
async setValue(v: A): Promise<void> { async setValue(v: A): Promise<void> {
...@@ -309,18 +310,20 @@ class SyncState<A> { ...@@ -309,18 +310,20 @@ class SyncState<A> {
async update(): Promise<void> { async update(): Promise<void> {
try { try {
this.upToDate = true; this.upToDate = true;
this.value = undefined;
this.UPDATE.emit();
if (Server.isRunning()) { if (Server.isRunning()) {
const v = await Server.send(this.handler.getter, null); const v = await Server.send(this.handler.getter, null);
this.value = v; this.value = v;
this.UPDATE.emit(); this.UPDATE.emit();
} else if (this.value !== undefined) {
this.value = undefined;
this.UPDATE.emit();
} }
} catch (error) { } catch (error) {
D.error( D.error(
`Fail to update SyncState '${this.handler.name}'.`, `Fail to update SyncState '${this.handler.name}'.`,
`${error}`, `${error}`,
); );
this.value = undefined;
this.UPDATE.emit(); this.UPDATE.emit();
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment