backupModelService.ts 2.9 KB
Newer Older
1 2 3 4 5 6 7
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

'use strict';

8
import Uri from 'vs/base/common/uri';
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
import { IBackupService, IBackupModelService, IBackupFileService } from 'vs/workbench/services/backup/common/backup';
import { IDisposable, dispose } from 'vs/base/common/lifecycle';
import { ITextFileService, TextFileModelChangeEvent } from 'vs/workbench/services/textfile/common/textfiles';
import { IFileService } from 'vs/platform/files/common/files';
import { IWorkspaceContextService } from 'vs/platform/workspace/common/workspace';
import { IUntitledEditorService } from 'vs/workbench/services/untitled/common/untitledEditorService';

export class BackupModelService implements IBackupModelService {

	public _serviceBrand: any;

	private toDispose: IDisposable[];

	constructor(
		@IBackupFileService private backupFileService: IBackupFileService,
		@IBackupService private backupService: IBackupService,
		@IFileService private fileService: IFileService,
		@ITextFileService private textFileService: ITextFileService,
		@IUntitledEditorService private untitledEditorService: IUntitledEditorService,
		@IWorkspaceContextService private contextService: IWorkspaceContextService
	) {
		this.toDispose = [];

		this.registerListeners();
	}

	private registerListeners() {
		this.toDispose.push(this.textFileService.models.onModelContentChanged((e) => this.onTextFileModelChanged(e)));
37 38 39
		this.toDispose.push(this.textFileService.models.onModelSaved((e) => this.onTextFileModelClean(e.resource)));
		this.toDispose.push(this.textFileService.models.onModelReverted((e) => this.onTextFileModelClean(e.resource)));
		this.toDispose.push(this.textFileService.models.onModelDisposed((e) => this.onTextFileModelClean(e)));
40
		this.toDispose.push(this.untitledEditorService.onDidChangeContent((e) => this.onUntitledModelChanged(e)));
41 42 43 44 45 46 47 48 49
	}

	private onTextFileModelChanged(event: TextFileModelChangeEvent): void {
		if (this.backupService.isHotExitEnabled) {
			const model = this.textFileService.models.get(event.resource);
			this.backupService.doBackup(model.getResource(), model.getValue());
		}
	}

50 51 52 53 54 55 56 57 58 59 60
	private onUntitledModelChanged(resource: Uri): void {
		if (this.backupService.isHotExitEnabled) {
			const input = this.untitledEditorService.get(resource);
			if (input.isDirty()) {
				this.backupService.doBackup(resource, input.getValue());
			} else {
				this.backupFileService.discardAndDeregisterResource(resource);
			}
		}
	}

61 62
	private onTextFileModelClean(resource: Uri): void {
		this.backupFileService.discardAndDeregisterResource(resource);
63 64 65 66 67 68 69 70
	}

	public dispose(): void {
		this.toDispose = dispose(this.toDispose);
	}

	// TODO: Move untitled file model watching here
}