Skip to content

Commit 480f96e

Browse files
committed
fix: sync_file_query_exists
1 parent a6e11ee commit 480f96e

1 file changed

Lines changed: 24 additions & 10 deletions

File tree

  • game-mode@karldreher/files/game-mode@karldreher

game-mode@karldreher/files/game-mode@karldreher/desklet.js

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,19 @@ const ICON_SIZE = 48;
2222
/** @constant {number} Circular container diameter in pixels */
2323
const CONTAINER_SIZE = 80;
2424

25+
/**
26+
* Test whether an exception is a GLib "file not found" error.
27+
* Guards against non-GError exceptions (such as the SyntaxError from
28+
* JSON.parse), which have no matches() method.
29+
*
30+
* @param {*} e - Caught exception
31+
* @returns {boolean} True if e is a Gio NOT_FOUND error
32+
*/
33+
function isNotFound(e) {
34+
return e instanceof GLib.Error
35+
&& e.matches(Gio.IOErrorEnum, Gio.IOErrorEnum.NOT_FOUND);
36+
}
37+
2538
/**
2639
* A circular toggle desklet that enables/disables "game mode".
2740
* When active the desklet shows a green background and applies:
@@ -46,7 +59,7 @@ GameModeDesklet.prototype = {
4659

4760
/**
4861
* Initialise the desklet, create settings interfaces for session,
49-
* notifications, and power, detect prior active state from the
62+
* notifications, and power, detect prior active state by reading the
5063
* persisted state file, and build the UI.
5164
*
5265
* @param {Object} metadata - Desklet metadata from metadata.json
@@ -64,8 +77,8 @@ GameModeDesklet.prototype = {
6477
/** @type {Gio.Settings} Power management (suspend, dimming, display sleep) */
6578
this._powerSettings = new Gio.Settings({ schema_id: "org.cinnamon.settings-daemon.plugins.power" });
6679

67-
/** @type {boolean} */
68-
this._gameModeActive = GLib.file_test(STATE_FILE, GLib.FileTest.EXISTS);
80+
/** @type {boolean} A readable state file means game mode is on */
81+
this._gameModeActive = this._loadState() !== null;
6982

7083
this.setupUI();
7184
},
@@ -173,7 +186,9 @@ GameModeDesklet.prototype = {
173186
}
174187
}
175188
} catch (e) {
176-
global.logWarning(UUID + ": failed to read state file: " + e.message);
189+
// A missing state file is the normal "game mode off" case.
190+
if (!isNotFound(e))
191+
global.logWarning(UUID + ": failed to read state file: " + e.message);
177192
}
178193
return null;
179194
},
@@ -201,16 +216,15 @@ GameModeDesklet.prototype = {
201216
},
202217

203218
/**
204-
* Delete the state file if it exists.
219+
* Delete the state file. An already-absent file is the expected
220+
* outcome and is not reported.
205221
*/
206222
_clearState: function() {
207223
try {
208-
let file = Gio.file_new_for_path(STATE_FILE);
209-
if (file.query_exists(null)) {
210-
file.delete(null);
211-
}
224+
Gio.file_new_for_path(STATE_FILE).delete(null);
212225
} catch (e) {
213-
global.logWarning(UUID + ": failed to clear state file: " + e.message);
226+
if (!isNotFound(e))
227+
global.logWarning(UUID + ": failed to clear state file: " + e.message);
214228
}
215229
},
216230

0 commit comments

Comments
 (0)