Skip to content

Commit

Permalink
working copy - allow to override backup delay (#172345) (#185958)
Browse files Browse the repository at this point in the history
* working copy - allow to override backup delay (#172345)

* working copy - also use `backupDelay` for untitled
  • Loading branch information
bpasero authored Jun 23, 2023
1 parent fd4a49e commit 03d7160
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 5 deletions.
13 changes: 13 additions & 0 deletions src/vs/workbench/contrib/notebook/common/notebookEditorModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { NotebookTextModel } from 'vs/workbench/contrib/notebook/common/model/no
import { ICellDto2, INotebookEditorModel, INotebookLoadOptions, IResolvedNotebookEditorModel, NotebookCellsChangeType, NotebookData } from 'vs/workbench/contrib/notebook/common/notebookCommon';
import { INotebookSerializer, INotebookService, SimpleNotebookProviderInfo } from 'vs/workbench/contrib/notebook/common/notebookService';
import { IFilesConfigurationService } from 'vs/workbench/services/filesConfiguration/common/filesConfigurationService';
import { IFileWorkingCopyModelConfiguration } from 'vs/workbench/services/workingCopy/common/fileWorkingCopy';
import { IFileWorkingCopyManager } from 'vs/workbench/services/workingCopy/common/fileWorkingCopyManager';
import { IStoredFileWorkingCopy, IStoredFileWorkingCopyModel, IStoredFileWorkingCopyModelContentChangedEvent, IStoredFileWorkingCopyModelFactory, IStoredFileWorkingCopySaveEvent, StoredFileWorkingCopyState } from 'vs/workbench/services/workingCopy/common/storedFileWorkingCopy';
import { IUntitledFileWorkingCopy, IUntitledFileWorkingCopyModel, IUntitledFileWorkingCopyModelContentChangedEvent, IUntitledFileWorkingCopyModelFactory } from 'vs/workbench/services/workingCopy/common/untitledFileWorkingCopy';
Expand Down Expand Up @@ -173,6 +174,8 @@ export class NotebookFileWorkingCopyModel extends Disposable implements IStoredF

readonly onWillDispose: Event<void>;

readonly configuration: IFileWorkingCopyModelConfiguration | undefined = undefined;

constructor(
private readonly _notebookModel: NotebookTextModel,
private readonly _notebookService: INotebookService
Expand All @@ -197,6 +200,16 @@ export class NotebookFileWorkingCopyModel extends Disposable implements IStoredF
break;
}
}));

if (_notebookModel.uri.scheme === Schemas.vscodeRemote) {
this.configuration = {
// Intentionally pick a larger delay for triggering backups when
// we are connected to a remote. This saves us repeated roundtrips
// to the remote server when the content changes because the
// remote hosts the extension of the notebook with the contents truth
backupDelay: 10000
};
}
}

override dispose(): void {
Expand Down
18 changes: 18 additions & 0 deletions src/vs/workbench/services/workingCopy/common/fileWorkingCopy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,18 @@ export interface IFileWorkingCopyModelFactory<M extends IFileWorkingCopyModel> {
createModel(resource: URI, contents: VSBufferReadableStream, token: CancellationToken): Promise<M>;
}

export interface IFileWorkingCopyModelConfiguration {

/**
* The delay in milliseconds to wait before triggering
* a backup after the content of the model has changed.
*
* If not configured, a sensible default will be taken
* based on user settings.
*/
readonly backupDelay?: number;
}

/**
* A generic file working copy model to be reused by untitled
* and stored file working copies.
Expand Down Expand Up @@ -50,6 +62,12 @@ export interface IFileWorkingCopyModel extends IDisposable {
*/
readonly onWillDispose: Event<void>;

/**
* Optional additional configuration for the model that drives
* some of the working copy behaviour.
*/
readonly configuration?: IFileWorkingCopyModelConfiguration;

/**
* Snapshots the model's current content for writing. This must include
* any changes that were made to the model that are in memory.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -757,6 +757,10 @@ export class StoredFileWorkingCopy<M extends IStoredFileWorkingCopyModel> extend

//#region Backup

get backupDelay(): number | undefined {
return this.model?.configuration?.backupDelay;
}

async backup(token: CancellationToken): Promise<IWorkingCopyBackup> {

// Fill in metadata if we are resolved
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,10 @@ export class UntitledFileWorkingCopy<M extends IUntitledFileWorkingCopyModel> ex

//#region Backup

get backupDelay(): number | undefined {
return this.model?.configuration?.backupDelay;
}

async backup(token: CancellationToken): Promise<IWorkingCopyBackup> {
let content: VSBufferReadableStream | undefined = undefined;

Expand Down
9 changes: 9 additions & 0 deletions src/vs/workbench/services/workingCopy/common/workingCopy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,15 @@ export interface IWorkingCopy extends IWorkingCopyIdentifier {

//#region Save / Backup

/**
* The delay in milliseconds to wait before triggering
* a backup after the content of the model has changed.
*
* If not configured, a sensible default will be taken
* based on user settings.
*/
readonly backupDelay?: number;

/**
* The workbench may call this method often after it receives
* the `onDidChangeContent` event for the working copy. The motivation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export abstract class WorkingCopyBackupTracker extends Disposable {
// have different scheduling delays based on auto save. This helps to
// avoid a (not critical but also not really wanted) race between saving
// (after 1s per default) and making a backup of the working copy.
private static readonly BACKUP_SCHEDULE_DELAYS = {
private static readonly DEFAULT_BACKUP_SCHEDULE_DELAYS = {
[AutoSaveMode.OFF]: 1000,
[AutoSaveMode.ON_FOCUS_CHANGE]: 1000,
[AutoSaveMode.ON_WINDOW_CHANGE]: 1000,
Expand Down Expand Up @@ -220,12 +220,16 @@ export abstract class WorkingCopyBackupTracker extends Disposable {
}

protected getBackupScheduleDelay(workingCopy: IWorkingCopy): number {
if (typeof workingCopy.backupDelay === 'number') {
return workingCopy.backupDelay; // respect working copy override
}

let autoSaveMode = this.filesConfigurationService.getAutoSaveMode();
if (workingCopy.capabilities & WorkingCopyCapabilities.Untitled) {
autoSaveMode = AutoSaveMode.OFF; // auto-save is never on for untitled working copies
}

return WorkingCopyBackupTracker.BACKUP_SCHEDULE_DELAYS[autoSaveMode];
return WorkingCopyBackupTracker.DEFAULT_BACKUP_SCHEDULE_DELAYS[autoSaveMode];
}

protected getContentVersion(workingCopy: IWorkingCopy): number {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,16 +146,16 @@ suite('WorkingCopyBackupTracker (browser)', function () {

class TestBackupWorkingCopy extends TestWorkingCopy {

backupDelay = 0;

constructor(resource: URI) {
super(resource);

accessor.workingCopyService.registerWorkingCopy(this);
}

readonly backupDelay = 10;

override async backup(token: CancellationToken): Promise<IWorkingCopyBackup> {
await timeout(this.backupDelay);
await timeout(0);

return {};
}
Expand Down

0 comments on commit 03d7160

Please sign in to comment.