Skip to content

Commit 03d7160

Browse files
authored
working copy - allow to override backup delay (microsoft#172345) (microsoft#185958)
* working copy - allow to override backup delay (microsoft#172345) * working copy - also use `backupDelay` for untitled
1 parent fd4a49e commit 03d7160

File tree

7 files changed

+57
-5
lines changed

7 files changed

+57
-5
lines changed

src/vs/workbench/contrib/notebook/common/notebookEditorModel.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { NotebookTextModel } from 'vs/workbench/contrib/notebook/common/model/no
1919
import { ICellDto2, INotebookEditorModel, INotebookLoadOptions, IResolvedNotebookEditorModel, NotebookCellsChangeType, NotebookData } from 'vs/workbench/contrib/notebook/common/notebookCommon';
2020
import { INotebookSerializer, INotebookService, SimpleNotebookProviderInfo } from 'vs/workbench/contrib/notebook/common/notebookService';
2121
import { IFilesConfigurationService } from 'vs/workbench/services/filesConfiguration/common/filesConfigurationService';
22+
import { IFileWorkingCopyModelConfiguration } from 'vs/workbench/services/workingCopy/common/fileWorkingCopy';
2223
import { IFileWorkingCopyManager } from 'vs/workbench/services/workingCopy/common/fileWorkingCopyManager';
2324
import { IStoredFileWorkingCopy, IStoredFileWorkingCopyModel, IStoredFileWorkingCopyModelContentChangedEvent, IStoredFileWorkingCopyModelFactory, IStoredFileWorkingCopySaveEvent, StoredFileWorkingCopyState } from 'vs/workbench/services/workingCopy/common/storedFileWorkingCopy';
2425
import { IUntitledFileWorkingCopy, IUntitledFileWorkingCopyModel, IUntitledFileWorkingCopyModelContentChangedEvent, IUntitledFileWorkingCopyModelFactory } from 'vs/workbench/services/workingCopy/common/untitledFileWorkingCopy';
@@ -173,6 +174,8 @@ export class NotebookFileWorkingCopyModel extends Disposable implements IStoredF
173174

174175
readonly onWillDispose: Event<void>;
175176

177+
readonly configuration: IFileWorkingCopyModelConfiguration | undefined = undefined;
178+
176179
constructor(
177180
private readonly _notebookModel: NotebookTextModel,
178181
private readonly _notebookService: INotebookService
@@ -197,6 +200,16 @@ export class NotebookFileWorkingCopyModel extends Disposable implements IStoredF
197200
break;
198201
}
199202
}));
203+
204+
if (_notebookModel.uri.scheme === Schemas.vscodeRemote) {
205+
this.configuration = {
206+
// Intentionally pick a larger delay for triggering backups when
207+
// we are connected to a remote. This saves us repeated roundtrips
208+
// to the remote server when the content changes because the
209+
// remote hosts the extension of the notebook with the contents truth
210+
backupDelay: 10000
211+
};
212+
}
200213
}
201214

202215
override dispose(): void {

src/vs/workbench/services/workingCopy/common/fileWorkingCopy.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,18 @@ export interface IFileWorkingCopyModelFactory<M extends IFileWorkingCopyModel> {
2323
createModel(resource: URI, contents: VSBufferReadableStream, token: CancellationToken): Promise<M>;
2424
}
2525

26+
export interface IFileWorkingCopyModelConfiguration {
27+
28+
/**
29+
* The delay in milliseconds to wait before triggering
30+
* a backup after the content of the model has changed.
31+
*
32+
* If not configured, a sensible default will be taken
33+
* based on user settings.
34+
*/
35+
readonly backupDelay?: number;
36+
}
37+
2638
/**
2739
* A generic file working copy model to be reused by untitled
2840
* and stored file working copies.
@@ -50,6 +62,12 @@ export interface IFileWorkingCopyModel extends IDisposable {
5062
*/
5163
readonly onWillDispose: Event<void>;
5264

65+
/**
66+
* Optional additional configuration for the model that drives
67+
* some of the working copy behaviour.
68+
*/
69+
readonly configuration?: IFileWorkingCopyModelConfiguration;
70+
5371
/**
5472
* Snapshots the model's current content for writing. This must include
5573
* any changes that were made to the model that are in memory.

src/vs/workbench/services/workingCopy/common/storedFileWorkingCopy.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -757,6 +757,10 @@ export class StoredFileWorkingCopy<M extends IStoredFileWorkingCopyModel> extend
757757

758758
//#region Backup
759759

760+
get backupDelay(): number | undefined {
761+
return this.model?.configuration?.backupDelay;
762+
}
763+
760764
async backup(token: CancellationToken): Promise<IWorkingCopyBackup> {
761765

762766
// Fill in metadata if we are resolved

src/vs/workbench/services/workingCopy/common/untitledFileWorkingCopy.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,10 @@ export class UntitledFileWorkingCopy<M extends IUntitledFileWorkingCopyModel> ex
252252

253253
//#region Backup
254254

255+
get backupDelay(): number | undefined {
256+
return this.model?.configuration?.backupDelay;
257+
}
258+
255259
async backup(token: CancellationToken): Promise<IWorkingCopyBackup> {
256260
let content: VSBufferReadableStream | undefined = undefined;
257261

src/vs/workbench/services/workingCopy/common/workingCopy.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,15 @@ export interface IWorkingCopy extends IWorkingCopyIdentifier {
182182

183183
//#region Save / Backup
184184

185+
/**
186+
* The delay in milliseconds to wait before triggering
187+
* a backup after the content of the model has changed.
188+
*
189+
* If not configured, a sensible default will be taken
190+
* based on user settings.
191+
*/
192+
readonly backupDelay?: number;
193+
185194
/**
186195
* The workbench may call this method often after it receives
187196
* the `onDidChangeContent` event for the working copy. The motivation

src/vs/workbench/services/workingCopy/common/workingCopyBackupTracker.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ export abstract class WorkingCopyBackupTracker extends Disposable {
8787
// have different scheduling delays based on auto save. This helps to
8888
// avoid a (not critical but also not really wanted) race between saving
8989
// (after 1s per default) and making a backup of the working copy.
90-
private static readonly BACKUP_SCHEDULE_DELAYS = {
90+
private static readonly DEFAULT_BACKUP_SCHEDULE_DELAYS = {
9191
[AutoSaveMode.OFF]: 1000,
9292
[AutoSaveMode.ON_FOCUS_CHANGE]: 1000,
9393
[AutoSaveMode.ON_WINDOW_CHANGE]: 1000,
@@ -220,12 +220,16 @@ export abstract class WorkingCopyBackupTracker extends Disposable {
220220
}
221221

222222
protected getBackupScheduleDelay(workingCopy: IWorkingCopy): number {
223+
if (typeof workingCopy.backupDelay === 'number') {
224+
return workingCopy.backupDelay; // respect working copy override
225+
}
226+
223227
let autoSaveMode = this.filesConfigurationService.getAutoSaveMode();
224228
if (workingCopy.capabilities & WorkingCopyCapabilities.Untitled) {
225229
autoSaveMode = AutoSaveMode.OFF; // auto-save is never on for untitled working copies
226230
}
227231

228-
return WorkingCopyBackupTracker.BACKUP_SCHEDULE_DELAYS[autoSaveMode];
232+
return WorkingCopyBackupTracker.DEFAULT_BACKUP_SCHEDULE_DELAYS[autoSaveMode];
229233
}
230234

231235
protected getContentVersion(workingCopy: IWorkingCopy): number {

src/vs/workbench/services/workingCopy/test/browser/workingCopyBackupTracker.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,16 +146,16 @@ suite('WorkingCopyBackupTracker (browser)', function () {
146146

147147
class TestBackupWorkingCopy extends TestWorkingCopy {
148148

149-
backupDelay = 0;
150-
151149
constructor(resource: URI) {
152150
super(resource);
153151

154152
accessor.workingCopyService.registerWorkingCopy(this);
155153
}
156154

155+
readonly backupDelay = 10;
156+
157157
override async backup(token: CancellationToken): Promise<IWorkingCopyBackup> {
158-
await timeout(this.backupDelay);
158+
await timeout(0);
159159

160160
return {};
161161
}

0 commit comments

Comments
 (0)