Skip to content

Commit 12582d3

Browse files
committed
fix: settings synchronization when updated from the plugin
1 parent b327c11 commit 12582d3

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

src/stream-deck/settings.ts

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { delay, get, IEventSubscriber, PromiseCompletionSource, set } from '../core';
1+
import { delay, get, IEventSubscriber, set } from '../core';
22
import streamDeckClient from './stream-deck-client';
33

44
export type SettingsEventArgs = {
@@ -11,15 +11,21 @@ export type SettingsEventArgs = {
1111
* Provides a wrapper around loading, managing, and persisting settings within the Stream Deck.
1212
*/
1313
export class Settings<TEventArgs extends SettingsEventArgs> {
14-
private _settings = new PromiseCompletionSource<unknown>();
14+
private readonly _initialization;
15+
private _settings: unknown;
1516

1617
/**
1718
* Initializes a new instance of the settings.
1819
* @param didReceive The event invoked when the settings are received from the Stream Deck.
1920
* @param save The delegate responsible for persisting the settings in the Stream Deck.
2021
*/
2122
constructor(private didReceive: IEventSubscriber<TEventArgs>, private save: (settings: unknown) => Promise<void>) {
22-
didReceive.subscribe(async (data: TEventArgs) => this._settings?.setResult(data.payload.settings));
23+
this._initialization = new Promise<void>((resolve) => {
24+
didReceive.subscribe((data) => {
25+
this._settings = data.payload.settings;
26+
resolve();
27+
});
28+
});
2329
}
2430

2531
/**
@@ -41,7 +47,10 @@ export class Settings<TEventArgs extends SettingsEventArgs> {
4147
}
4248

4349
// Construct getter and setter.
44-
const getter = async (): Promise<T> => get(key, await this._settings.promise);
50+
const getter = async (): Promise<T> => {
51+
await this._initialization;
52+
return get(key, await this._settings);
53+
};
4554
const setter = timeout ? delay((value) => this.set(key, value, save), timeout) : (value?: unknown) => this.set(key, value, save);
4655

4756
return [getter, setter];
@@ -54,16 +63,16 @@ export class Settings<TEventArgs extends SettingsEventArgs> {
5463
* @param save Indicates whether the setting should be persisted to the Stream Deck.
5564
*/
5665
private async set(key: string, value?: unknown, save: boolean | null = true): Promise<void> {
57-
const _settings = await this._settings.promise;
66+
await this._initialization;
5867

59-
const oldValue = get(key, _settings);
68+
const oldValue = get(key, this._settings);
6069
if (oldValue === value) {
6170
return;
6271
}
6372

64-
set(key, _settings, value);
73+
set(key, this._settings, value);
6574
if (save) {
66-
await this.save(_settings);
75+
await this.save(this._settings);
6776
}
6877
}
6978
}

0 commit comments

Comments
 (0)