Fix: Don't use globalThis, since it won't get g.c. on window close

Don't use `globalThis`, since it won't get garbage collected on window
close.

See here for the relevant review:
https://extensions.gnome.org/review/38364
And for the relevant documentation:
https://gjs.guide/extensions/review-guidelines/review-guidelines.html#only-use-init-for-initialization
This commit is contained in:
Julian 2023-01-26 03:42:56 +01:00
parent 2ef16f310c
commit 7b1c030325
Signed by: julian
GPG Key ID: 094C2AC34192FA11
2 changed files with 7 additions and 4 deletions

View File

@ -11,6 +11,4 @@ function buildPrefsWidget() {
} }
function init() { function init() {
// Load the settings.
globalThis.settings = ExtensionUtils.getSettings();
} }

View File

@ -1,6 +1,5 @@
"use strict"; "use strict";
/* exported PrefsBoxOrderListBox */ /* exported PrefsBoxOrderListBox */
/* global settings */
const Gtk = imports.gi.Gtk; const Gtk = imports.gi.Gtk;
const GObject = imports.gi.GObject; const GObject = imports.gi.GObject;
@ -30,6 +29,9 @@ var PrefsBoxOrderListBox = GObject.registerClass({
constructor(params = {}) { constructor(params = {}) {
super(params); super(params);
// Load the settings.
this._settings = ExtensionUtils.getSettings();
// Add a placeholder widget for the case, where no GtkListBoxRows are // Add a placeholder widget for the case, where no GtkListBoxRows are
// present. // present.
this.set_placeholder(new PrefsBoxOrderListEmptyPlaceholder.PrefsBoxOrderListEmptyPlaceholder()); this.set_placeholder(new PrefsBoxOrderListEmptyPlaceholder.PrefsBoxOrderListEmptyPlaceholder());
@ -42,6 +44,9 @@ var PrefsBoxOrderListBox = GObject.registerClass({
set boxOrder(value) { set boxOrder(value) {
this._boxOrder = value; this._boxOrder = value;
// Load the settings here as well, since a `CONSTRUCT_ONLY` property
// apparently can't access `this._settings`.
const settings = ExtensionUtils.getSettings();
// Get the actual box order for the given box order name from settings. // Get the actual box order for the given box order name from settings.
const boxOrder = settings.get_strv(this._boxOrder); const boxOrder = settings.get_strv(this._boxOrder);
// Populate this GtkListBox with GtkListBoxRows for the items of the // Populate this GtkListBox with GtkListBoxRows for the items of the
@ -67,6 +72,6 @@ var PrefsBoxOrderListBox = GObject.registerClass({
const item = potentialPrefsBoxOrderItemRow.item; const item = potentialPrefsBoxOrderItemRow.item;
currentBoxOrder.push(item); currentBoxOrder.push(item);
} }
settings.set_strv(this.boxOrder, currentBoxOrder); this._settings.set_strv(this.boxOrder, currentBoxOrder);
} }
}); });