Night Light Timer: Create D-Bus proxy asynchronously

This commit is contained in:
Romain Vigier
2022-08-16 01:26:00 +02:00
parent 73c1cf8fcc
commit 0a18075787
+30 -18
View File
@@ -30,6 +30,7 @@ const COLOR_INTERFACE = `
var TimerNightlight = class { var TimerNightlight = class {
#settings; #settings;
#cancellable = null;
#colorDbusProxy = null; #colorDbusProxy = null;
#settingsConnections = []; #settingsConnections = [];
#nightlightStateConnection = null; #nightlightStateConnection = null;
@@ -39,9 +40,10 @@ var TimerNightlight = class {
this.#settings = extensionUtils.getSettings(utils.getSettingsSchema('time')); this.#settings = extensionUtils.getSettings(utils.getSettingsSchema('time'));
} }
enable() { async enable() {
console.debug('Enabling Night Light Timer...'); console.debug('Enabling Night Light Timer...');
this.#connectToColorDbusProxy(); this.#cancellable = new Gio.Cancellable();
this.#colorDbusProxy = await this.#createColorDbusProxy();
this.#connectSettings(); this.#connectSettings();
this.#listenToNightlightState(); this.#listenToNightlightState();
this.emit('time-changed', this.time); this.emit('time-changed', this.time);
@@ -52,7 +54,9 @@ var TimerNightlight = class {
console.debug('Disabling Night Light Timer...'); console.debug('Disabling Night Light Timer...');
this.#stopListeningToNightlightState(); this.#stopListeningToNightlightState();
this.#disconnectSettings(); this.#disconnectSettings();
this.#disconnectFromColorDbusProxy(); this.#colorDbusProxy = null;
this.#cancellable.cancel();
this.#cancellable = null;
console.debug('Night Light Timer disabled.'); console.debug('Night Light Timer disabled.');
} }
@@ -62,21 +66,26 @@ var TimerNightlight = class {
} }
#connectToColorDbusProxy() { #createColorDbusProxy() {
console.debug('Connecting to Color DBus proxy...'); console.debug('Creating the Color DBus proxy...');
const ColorProxy = Gio.DBusProxy.makeProxyWrapper(COLOR_INTERFACE); const ColorProxy = Gio.DBusProxy.makeProxyWrapper(COLOR_INTERFACE);
this.#colorDbusProxy = new ColorProxy( return new Promise((resolve, reject) => {
Gio.DBus.session, ColorProxy(
'org.gnome.SettingsDaemon.Color', Gio.DBus.session,
'/org/gnome/SettingsDaemon/Color' 'org.gnome.SettingsDaemon.Color',
); '/org/gnome/SettingsDaemon/Color',
console.debug('Connected to Color DBus proxy.'); (proxy, error) => {
} if (error === null) {
console.debug('Created the Color DBus proxy.');
#disconnectFromColorDbusProxy() { resolve(proxy);
console.debug('Disconnecting from Color DBus proxy...'); } else {
this.#colorDbusProxy = null; reject(error);
console.debug('Disconnected from Color DBus proxy.'); }
},
this.#cancellable,
Gio.DBusProxyFlags.NONE
);
});
} }
#connectSettings() { #connectSettings() {
@@ -102,7 +111,10 @@ var TimerNightlight = class {
} }
#stopListeningToNightlightState() { #stopListeningToNightlightState() {
this.#colorDbusProxy.disconnect(this.#nightlightStateConnection); if (this.#colorDbusProxy && this.#nightlightStateConnection) {
this.#colorDbusProxy.disconnect(this.#nightlightStateConnection);
this.#nightlightStateConnection = null;
}
console.debug('Stopped listening to Night Light state.'); console.debug('Stopped listening to Night Light state.');
} }