Skip to content

Commit ae72a0c

Browse files
authored
Merge pull request #16 from nlpsuge/dev-fix-log
Fix log.js: Use Log class to hold this._prefsUtils and remove it in the destroy(). See: https://gjs.guide/extensions/review-guidelines/review-guidelines.html#rules
2 parents 3202589 + d1e9310 commit ae72a0c

10 files changed

Lines changed: 86 additions & 39 deletions

File tree

closeSession.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ const Log = Me.imports.utils.log;
88

99
var CloseSession = class {
1010
constructor() {
11+
this._log = new Log.Log();
12+
1113
this._skip_app_with_multiple_windows = true;
1214
this._defaultAppSystem = Shell.AppSystem.get_default();
1315
// TODO Put into Settings
@@ -16,16 +18,15 @@ var CloseSession = class {
1618
}
1719

1820
closeWindows() {
19-
Log.debug('Closing open windows');
21+
this._log.debug('Closing open windows');
2022
let running_apps = this._defaultAppSystem.get_running();
2123
for (const app of running_apps) {
2224
const app_name = app.get_name();
2325
if (this._skip_multiple_windows(app)) {
24-
Log.debug(`Skipping ${app.get_name()} because it has more than one windows`);
26+
this._log.debug(`Skipping ${app.get_name()} because it has more than one windows`);
2527
continue;
2628
}
27-
28-
Log.debug(`Closing ${app_name}`);
29+
this._log.debug(`Closing ${app_name}`);
2930
app.request_quit();
3031
}
3132
}
@@ -34,7 +35,7 @@ var CloseSession = class {
3435
if (shellApp.get_n_windows() > 1 && this._skip_app_with_multiple_windows) {
3536
const app_id = shellApp.get_id();
3637
if (this.whitelist.includes(app_id)) {
37-
Log.debug(`${shellApp.get_name()} / ${app_id} in the whitelist.`);
38+
this._log.debug(`${shellApp.get_name()} / ${app_id} in the whitelist.`);
3839
return false;
3940
}
4041
return true;
@@ -46,6 +47,11 @@ var CloseSession = class {
4647
if (this._defaultAppSystem) {
4748
this._defaultAppSystem = null;
4849
}
50+
51+
if (this._log) {
52+
this._log.destroy();
53+
this._log = null;
54+
}
4955
}
5056

5157
}

indicator.js

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const SessionItem = Me.imports.ui.sessionItem;
1515
const SearchSessionItem = Me.imports.ui.searchSessionItem;
1616
const PopupMenuButtonItems = Me.imports.ui.popupMenuButtonItems;
1717
const IconFinder = Me.imports.utils.iconFinder;
18+
const PrefsUtils = Me.imports.utils.prefsUtils;
1819
const Log = Me.imports.utils.log;
1920

2021

@@ -23,6 +24,9 @@ class AwsIndicator extends PanelMenu.Button {
2324

2425
_init() {
2526
super._init(0.0, "Another Window Session Manager");
27+
28+
this._prefsUtils = new PrefsUtils.PrefsUtils();
29+
this._log = new Log.Log();
2630

2731
this._itemIndex = 0;
2832

@@ -111,20 +115,20 @@ class AwsIndicator extends PanelMenu.Button {
111115
return;
112116
}
113117

114-
Log.debug('List all sessions to add session items');
118+
this._log.debug('List all sessions to add session items');
115119

116120
let sessionFileInfos = [];
117-
FileUtils.listAllSessions(null, false, (file, info) => {
121+
FileUtils.listAllSessions(null, false, this._prefsUtils.isDebug(),(file, info) => {
118122
// We have an interest in regular and text files
119123

120124
const file_type = info.get_file_type();
121125
if (file_type !== Gio.FileType.REGULAR) {
122-
Log.debug(`${file.get_path()} (file type is ${file_type}) is not a regular file, skipping`);
126+
this._log.debug(`${file.get_path()} (file type is ${file_type}) is not a regular file, skipping`);
123127
return;
124128
}
125129
const content_type = info.get_content_type();
126130
if (content_type !== 'text/plain') {
127-
Log.debug(`${file.get_path()} (content type is ${content_type}) is not a text file, skipping`);
131+
this._log.debug(`${file.get_path()} (content type is ${content_type}) is not a text file, skipping`);
128132
return;
129133
}
130134

@@ -139,7 +143,7 @@ class AwsIndicator extends PanelMenu.Button {
139143
} else {
140144
parentPath = parent.get_path();
141145
}
142-
Log.debug(`Processing ${file.get_path()} under ${parentPath}`);
146+
this._log.debug(`Processing ${file.get_path()} under ${parentPath}`);
143147
sessionFileInfos.push({
144148
info: info,
145149
file: file
@@ -196,8 +200,7 @@ class AwsIndicator extends PanelMenu.Button {
196200
// https://gjs-docs.gnome.org/gio20~2.66p/gio.filemonitor#signal-changed
197201
// Looks like the document is wrong ...
198202
_sessionPathChanged(monitor, srcFile, descFile) {
199-
Log.debug(`Session path changed, readd all session items from ${this._sessions_path}. ${srcFile.get_path()} was changed.`);
200-
203+
this._log.debug(`Session path changed, readd all session items from ${this._sessions_path}. ${srcFile.get_path()} was changed.`);
201204
this._sessionsMenuSection.removeAll();
202205
// It probably is a problem when there is large amount session files,
203206
// say thousands of them, but who creates that much?
@@ -241,6 +244,16 @@ class AwsIndicator extends PanelMenu.Button {
241244
this._sessions_path = null;
242245
}
243246

247+
if (this._prefsUtils) {
248+
this._prefsUtils.destroy();
249+
this._prefsUtils = null;
250+
}
251+
252+
if (this._log) {
253+
this._log.destroy();
254+
this._log = null;
255+
}
256+
244257
this.destroy();
245258

246259
}

metadata.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@
88
],
99
"url": "https://github.com/nlpsuge/gnome-shell-extension-another-window-session-manager",
1010
"uuid": "another-window-session-manager@gmail.com",
11-
"version": 3
11+
"version": 4
1212
}

moveSession.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ const Log = Me.imports.utils.log;
1212
var MoveSession = class {
1313

1414
constructor() {
15+
this._log = new Log.Log();
16+
1517
this.sessionName = FileUtils.default_sessionName;
1618
this._defaultAppSystem = Shell.AppSystem.get_default();
1719

@@ -29,8 +31,7 @@ var MoveSession = class {
2931
return;
3032
}
3133

32-
Log.debug(`Moving windows by saved session located in ${session_file_path}`);
33-
34+
this._log.debug(`Moving windows by saved session located in ${session_file_path}`);
3435
const session_file = Gio.File.new_for_path(session_file_path);
3536
let [success, contents] = session_file.load_contents(null);
3637
if (success) {
@@ -63,8 +64,7 @@ var MoveSession = class {
6364
const title = open_window.get_title();
6465
const desktop_number = saved_window_session.desktop_number;
6566

66-
Log.debug(`Auto move the window '${title}' to workspace ${desktop_number} for ${shellApp.get_name()}`);
67-
67+
this._log.debug(`Auto move the window '${title}' to workspace ${desktop_number} for ${shellApp.get_name()}`);
6868
this._createEnoughWorkspace(desktop_number);
6969
open_window.change_workspace_by_index(desktop_number, false);
7070

@@ -118,7 +118,7 @@ var MoveSession = class {
118118

119119
if (windows_count === 1 || title === saved_window_session.window_title) {
120120
if (open_window_workspace_index === desktop_number) {
121-
Log.debug(`The window '${title}' is already on workspace ${desktop_number} for ${shellApp.get_name()}`);
121+
this._log.debug(`The window '${title}' is already on workspace ${desktop_number} for ${shellApp.get_name()}`);
122122
saved_window_session.moved = true;
123123
return;
124124
}
@@ -147,6 +147,11 @@ var MoveSession = class {
147147
if (this._defaultAppSystem) {
148148
this._defaultAppSystem = null;
149149
}
150+
151+
if (this._log) {
152+
this._log.destroy();
153+
this._log = null;
154+
}
150155

151156
}
152157

restoreSession.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ const Log = Me.imports.utils.log;
1717
var RestoreSession = class {
1818

1919
constructor() {
20+
this._log = new Log.Log();
21+
2022
this.sessionName = FileUtils.default_sessionName;
2123
this._defaultAppSystem = Shell.AppSystem.get_default();
2224
this._restoredApps = new Map();
@@ -35,8 +37,7 @@ var RestoreSession = class {
3537
return;
3638
}
3739

38-
Log.debug(`Restoring saved session from ${session_file_path}`);
39-
40+
this._log.debug(`Restoring saved session from ${session_file_path}`);
4041
try {
4142
this.restoreSessionFromPath(session_file_path);
4243
} catch (e) {
@@ -68,7 +69,7 @@ var RestoreSession = class {
6869
[launched, running] = this.launch(shell_app);
6970
if (launched) {
7071
if (!running) {
71-
Log.debug(`${app_name} launched!`);
72+
this._log.debug(`${app_name} launched!`);
7273
}
7374
const existingShellAppData = this._restoredApps.get(shell_app);
7475
if (existingShellAppData) {
@@ -125,7 +126,7 @@ var RestoreSession = class {
125126
}
126127

127128
if (this._appIsRunning(shellApp)) {
128-
Log.debug(`${shellApp.get_name()} is running, skipping`)
129+
this._log.debug(`${shellApp.get_name()} is running, skipping`)
129130
return [true, true];
130131
}
131132

@@ -188,6 +189,11 @@ var RestoreSession = class {
188189
this._moveSession = null;
189190
}
190191

192+
if (this._log) {
193+
this._log.destroy();
194+
this._log = null;
195+
}
196+
191197
}
192198

193199
}

saveSession.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ const Log = Me.imports.utils.log;
1212
var SaveSession = class {
1313

1414
constructor() {
15+
this._log = new Log.Log();
16+
1517
this._windowTracker = Shell.WindowTracker.get_default();
1618
this._subprocessLauncher = new Gio.SubprocessLauncher({
1719
flags: (Gio.SubprocessFlags.STDOUT_PIPE |
@@ -154,7 +156,7 @@ var SaveSession = class {
154156
);
155157

156158
if (success) {
157-
Log.debug(`Saved open windows as a session in ${session_file_path}!`);
159+
this._log.debug(`Saved open windows as a session in ${session_file_path}!`);
158160
}
159161
return success;
160162
}

ui/button.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
'use strict';
2+
13
const { GObject, St, Clutter } = imports.gi;
24

35
const ExtensionUtils = imports.misc.extensionUtils;

utils/fileUtils.js

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,6 @@
22

33
const { Gio, GLib } = imports.gi
44

5-
const ExtensionUtils = imports.misc.extensionUtils;
6-
const Me = ExtensionUtils.getCurrentExtension();
7-
8-
const Log = Me.imports.utils.log;
9-
105
var default_sessionName = 'defaultSession';
116
const home_dir = GLib.get_home_dir();
127
// This extension can restore `xsm`'s session file,
@@ -38,7 +33,7 @@ function getJsonObj(contents) {
3833
return session_config;
3934
}
4035

41-
function listAllSessions(sessionPath, recursion, callback) {
36+
function listAllSessions(sessionPath, recursion, debug, callback) {
4237
if (!sessionPath) {
4338
sessionPath = get_sessions_path();
4439
}
@@ -47,8 +42,9 @@ function listAllSessions(sessionPath, recursion, callback) {
4742
return;
4843
}
4944

50-
Log.debug(`Looking up path: ${sessionPath}`);
51-
45+
if (debug) {
46+
log(`Looking up path: ${sessionPath}`);
47+
}
5248
const sessionPathFile = Gio.File.new_for_path(sessionPath);
5349
let fileEnumerator;
5450
try {
@@ -69,8 +65,10 @@ function listAllSessions(sessionPath, recursion, callback) {
6965
while ((info = fileEnumerator.next_file(null))) {
7066
const file = fileEnumerator.get_child(info);
7167
if (recursion && info.get_file_type() === Gio.FileType.DIRECTORY) {
72-
Log.debug(`${info.get_name()} is a folder, checking`);
73-
listAllSessions(file.get_path(), recursion, callback);
68+
if (debug) {
69+
log(`${info.get_name()} is a folder, checking`);
70+
}
71+
listAllSessions(file.get_path(), recursion, debug, callback);
7472
}
7573

7674
if (callback) {
@@ -82,8 +80,6 @@ function listAllSessions(sessionPath, recursion, callback) {
8280
}
8381

8482
function trashSession(sessionName) {
85-
Log.debug(`Moving ${sessionName} to trash scan.`);
86-
8783
const sessionFilePath = GLib.build_filenamev([sessions_path, sessionName]);
8884
if (!GLib.file_test(sessionFilePath, GLib.FileTest.EXISTS)) {
8985
return true;

utils/log.js

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,25 @@ const Me = ExtensionUtils.getCurrentExtension();
66
const PrefsUtils = Me.imports.utils.prefsUtils;
77

88

9-
const _prefsUtils = new PrefsUtils.PrefsUtils();
9+
var Log = class {
1010

11-
function debug(logContent) {
12-
if (_prefsUtils.isDebug()) {
13-
log(`[Another window session manager] ${logContent}`);
11+
constructor() {
12+
this._prefsUtils = new PrefsUtils.PrefsUtils();
13+
1414
}
15+
16+
debug(logContent) {
17+
if (this._prefsUtils.isDebug()) {
18+
log(`[DEBUG][Another window session manager] ${logContent}`);
19+
}
20+
}
21+
22+
destroy() {
23+
if (this._prefsUtils) {
24+
this._prefsUtils.destroy();
25+
this._prefsUtils = null;
26+
}
27+
28+
}
29+
1530
}

utils/prefsUtils.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
'use strict';
2+
13
const ExtensionUtils = imports.misc.extensionUtils;
24

35
var PrefsUtils = class {

0 commit comments

Comments
 (0)