Skip to content

Commit e4fff91

Browse files
committed
Refactor to prevent drift between plugin settings and the extension model
1 parent ee5e219 commit e4fff91

File tree

2 files changed

+49
-37
lines changed

2 files changed

+49
-37
lines changed

src/index.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -115,15 +115,8 @@ function activate(
115115
{ rank: 60 }
116116
);
117117

118-
// Sync the refresh interval to the current settings:
119-
gitExtension.refreshInterval = settings.composite
120-
.refreshInterval as number;
121-
122-
// Listen for changes to extension settings:
123-
settings.changed.connect((settings: ISettingRegistry.ISettings) => {
124-
gitExtension.refreshInterval = settings.composite
125-
.refreshInterval as number;
126-
});
118+
// Pass along the plugin settings to the Git model:
119+
gitExtension.settings = settings;
127120
})
128121
.catch(reason => {
129122
console.error(

src/model.ts

Lines changed: 47 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import { JupyterFrontEnd } from '@jupyterlab/application';
2-
import { IChangedArgs, PathExt, Poll } from '@jupyterlab/coreutils';
2+
import {
3+
IChangedArgs,
4+
PathExt,
5+
Poll,
6+
ISettingRegistry
7+
} from '@jupyterlab/coreutils';
38
import { ServerConnection } from '@jupyterlab/services';
49
import { CommandRegistry } from '@phosphor/commands';
510
import { JSONObject } from '@phosphor/coreutils';
@@ -8,9 +13,6 @@ import { ISignal, Signal } from '@phosphor/signaling';
813
import { httpGitRequest } from './git';
914
import { IGitExtension, Git } from './tokens';
1015

11-
// Refresh interval (note: this should be the same as in the plugin settings schema):
12-
let REFRESH_INTERVAL = 3000; // ms
13-
1416
/** Main extension class */
1517
export class GitExtension implements IGitExtension, IDisposable {
1618
constructor(app: JupyterFrontEnd = null) {
@@ -24,34 +26,35 @@ export class GitExtension implements IGitExtension, IDisposable {
2426
.catch(reason => {
2527
console.error(`Fail to get the server root path.\n${reason}`);
2628
});
27-
28-
// Start watching the repository
29-
this._poll = new Poll({
30-
factory: () => this._refreshStatus(),
31-
frequency: {
32-
interval: REFRESH_INTERVAL,
33-
backoff: true,
34-
max: 300 * 1000
35-
},
36-
standby: 'when-hidden'
37-
});
3829
}
3930

4031
/**
41-
* Number of milliseconds between polling the file system for changes.
42-
*
43-
* ## Notes
44-
*
45-
* - WARNING: this should only be set upon changes to plugin settings. If the refresh interval is set independently of plugin settings (e.g., programmatically set by internal extension logic irrespective of whether plugin settings have changed), we run the risk of being out-of-sync with user expectation.
32+
* Plugin settings.
4633
*/
47-
set refreshInterval(v: number) {
48-
const freq = this._poll.frequency;
49-
this._poll.frequency = {
50-
interval: v,
51-
backoff: freq.backoff,
52-
max: freq.max
53-
};
54-
REFRESH_INTERVAL = v;
34+
set settings(settings: ISettingRegistry.ISettings) {
35+
if (this._settings) {
36+
this._settings.changed.disconnect(this._onSettingsChange, this);
37+
}
38+
settings.changed.connect(this._onSettingsChange, this);
39+
if (this._poll) {
40+
const freq = this._poll.frequency;
41+
this._poll.frequency = {
42+
interval: settings.composite.refreshInterval as number,
43+
backoff: freq.backoff,
44+
max: freq.max
45+
};
46+
} else {
47+
this._poll = new Poll({
48+
factory: () => this._refreshStatus(),
49+
frequency: {
50+
interval: settings.composite.refreshInterval as number,
51+
backoff: true,
52+
max: 300 * 1000
53+
},
54+
standby: 'when-hidden'
55+
});
56+
}
57+
this._settings = settings;
5558
}
5659

5760
/**
@@ -914,6 +917,21 @@ export class GitExtension implements IGitExtension, IDisposable {
914917
return this._currentMarker;
915918
}
916919

920+
/**
921+
* Callback invoked upon a change to plugin settings.
922+
*
923+
* @private
924+
* @param settings - settings registry
925+
*/
926+
private _onSettingsChange(settings: ISettingRegistry.ISettings) {
927+
const freq = this._poll.frequency;
928+
this._poll.frequency = {
929+
interval: settings.composite.refreshInterval as number,
930+
backoff: freq.backoff,
931+
max: freq.max
932+
};
933+
}
934+
917935
private _status: Git.IStatusFileResult[] = [];
918936
private _pathRepository: string | null = null;
919937
private _branches: Git.IBranch[];
@@ -927,6 +945,7 @@ export class GitExtension implements IGitExtension, IDisposable {
927945
private _readyPromise: Promise<void> = Promise.resolve();
928946
private _pendingReadyPromise = 0;
929947
private _poll: Poll;
948+
private _settings: ISettingRegistry.ISettings | null = null;
930949
private _headChanged = new Signal<IGitExtension, void>(this);
931950
private _markChanged = new Signal<IGitExtension, void>(this);
932951
private _repositoryChanged = new Signal<

0 commit comments

Comments
 (0)