Skip to content

Commit

Permalink
Stop observing when disabling the extension
Browse files Browse the repository at this point in the history
When pausing the timer, locking the screen and unlocking again,
I got two new wallpaper in a short amount of time the next time
the timer runs. The observer were still active and unpausing
activated all still observing timer.
  • Loading branch information
Lucki committed Jul 14, 2023
1 parent feff7b1 commit d604323
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
3 changes: 3 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ class Extension {
if (Timer)
Timer.AFTimer.destroy();

if (this._wallpaperController)
this._wallpaperController.cleanup();

this._timer = null;
this._logger = null;
this._panelMenu = null;
Expand Down
35 changes: 25 additions & 10 deletions src/wallpaperController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ class WallpaperController {
private _startLoadingHooks: (() => void)[] = [];
/** functions will be called when loading a new wallpaper stopped. */
private _stopLoadingHooks: (() => void)[] = [];
private _observedValues: number[] = [];
private _observedBackgroundValues: number[] = [];

/**
* Create a new controller.
Expand Down Expand Up @@ -85,24 +87,24 @@ class WallpaperController {
this._backendConnection.setBoolean('request-new-wallpaper', false);

// Track value changes
this._backendConnection.observe('clear-history', () => this._clearHistory());
this._backendConnection.observe('open-folder', () => this._openFolder());
this._backendConnection.observe('pause-timer', () => this._pauseTimer());
this._backendConnection.observe('request-new-wallpaper', () => this._requestNewWallpaper().catch(error => {
this._observedBackgroundValues.push(this._backendConnection.observe('clear-history', () => this._clearHistory()));
this._observedBackgroundValues.push(this._backendConnection.observe('open-folder', () => this._openFolder()));
this._observedBackgroundValues.push(this._backendConnection.observe('pause-timer', () => this._pauseTimer()));
this._observedBackgroundValues.push(this._backendConnection.observe('request-new-wallpaper', () => this._requestNewWallpaper().catch(error => {
this._logger.error(error);
}));
})));

this._settings.observe('history-length', () => this._updateHistory());
this._settings.observe('auto-fetch', () => this._updateAutoFetching());
this._settings.observe('minutes', () => this._updateAutoFetching());
this._settings.observe('hours', () => this._updateAutoFetching());
this._observedValues.push(this._settings.observe('history-length', () => this._updateHistory()));
this._observedValues.push(this._settings.observe('auto-fetch', () => this._updateAutoFetching()));
this._observedValues.push(this._settings.observe('minutes', () => this._updateAutoFetching()));
this._observedValues.push(this._settings.observe('hours', () => this._updateAutoFetching()));

/**
* When the user installs a manager we won't notice that it's available.
* The preference window however checks on startup for availability and will allow this setting
* to change. Let's listen for that change and update our manager accordingly.
*/
this._settings.observe('multiple-displays', () => this._updateWallpaperManager());
this._observedValues.push(this._settings.observe('multiple-displays', () => this._updateWallpaperManager()));

this._updateHistory();

Expand Down Expand Up @@ -150,6 +152,19 @@ class WallpaperController {
}
}

/**
* Clean up extension remnants.
*/
cleanup(): void {
for (const observedValue of this._observedValues)
this._settings.disconnect(observedValue);
this._observedValues = [];

for (const observedValue of this._observedBackgroundValues)
this._backendConnection.disconnect(observedValue);
this._observedBackgroundValues = [];
}

/**
* Empty the history. (Background settings observer edition)
*/
Expand Down

0 comments on commit d604323

Please sign in to comment.