提交 8429b398 编写于 作者: D Daniel Imms

Move backup unload logic to an event

Fixes #15260
上级 23d3de42
......@@ -5,19 +5,18 @@
'use strict';
import Uri from 'vs/base/common/uri';
import { EventEmitter } from 'events';
import { ipcMain as ipc, app } from 'electron';
import { TPromise, TValueCallback } from 'vs/base/common/winjs.base';
import { ReadyState, VSCodeWindow } from 'vs/code/electron-main/window';
import { IEnvironmentService } from 'vs/platform/environment/common/environment';
import { createDecorator } from 'vs/platform/instantiation/common/instantiation';
import { IBackupMainService } from 'vs/platform/backup/common/backup';
import { ILogService } from 'vs/code/electron-main/log';
import { IStorageService } from 'vs/code/electron-main/storage';
const EventTypes = {
BEFORE_QUIT: 'before-quit'
BEFORE_QUIT: 'before-quit',
AFTER_UNLOAD: 'after-unload'
};
export const ILifecycleService = createDecorator<ILifecycleService>('lifecycleService');
......@@ -31,6 +30,7 @@ export interface ILifecycleService {
wasUpdated: boolean;
onBeforeQuit(clb: () => void): () => void;
onAfterUnload(clb: (VSCodeWindow) => void): () => void;
ready(): void;
registerWindow(vscodeWindow: VSCodeWindow): void;
unload(vscodeWindow: VSCodeWindow): TPromise<boolean /* veto */>;
......@@ -54,8 +54,7 @@ export class LifecycleService implements ILifecycleService {
constructor(
@IEnvironmentService private environmentService: IEnvironmentService,
@ILogService private logService: ILogService,
@IStorageService private storageService: IStorageService,
@IBackupMainService private backupService: IBackupMainService
@IStorageService private storageService: IStorageService
) {
this.windowToCloseRequest = Object.create(null);
this.quitRequested = false;
......@@ -88,6 +87,12 @@ export class LifecycleService implements ILifecycleService {
return () => this.eventEmitter.removeListener(EventTypes.BEFORE_QUIT, clb);
}
onAfterUnload(clb: (VSCodeWindow) => void): () => void {
this.eventEmitter.addListener(EventTypes.AFTER_UNLOAD, clb);
return () => this.eventEmitter.removeListener(EventTypes.AFTER_UNLOAD, clb);
}
public ready(): void {
this.registerListeners();
}
......@@ -163,13 +168,7 @@ export class LifecycleService implements ILifecycleService {
const oneTimeCancelEvent = 'vscode:cancel' + oneTimeEventToken;
ipc.once(oneTimeOkEvent, () => {
// Clear out any workspace backups from workspaces.json that don't have any backups
if (vscodeWindow.openedWorkspacePath) {
const workspaceResource = Uri.file(vscodeWindow.openedWorkspacePath);
if (!this.backupService.hasWorkspaceBackup(workspaceResource)) {
this.backupService.removeWorkspaceBackupPathSync(workspaceResource);
}
}
this.eventEmitter.emit(EventTypes.AFTER_UNLOAD, vscodeWindow);
c(false); // no veto
});
......
......@@ -35,7 +35,7 @@ import { SyncDescriptor } from 'vs/platform/instantiation/common/descriptors';
import { ILogService, MainLogService } from 'vs/code/electron-main/log';
import { IStorageService, StorageService } from 'vs/code/electron-main/storage';
import { IBackupMainService } from 'vs/platform/backup/common/backup';
import { BackupMainService } from 'vs/platform/backup/node/backupMainService';
import { BackupMainService } from 'vs/platform/backup/electron-main/backupMainService';
import { IEnvironmentService, ParsedArgs } from 'vs/platform/environment/common/environment';
import { EnvironmentService } from 'vs/platform/environment/node/environmentService';
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
......
......@@ -10,6 +10,8 @@ import Uri from 'vs/base/common/uri';
import { readdirSync } from 'vs/base/node/extfs';
import { IBackupWorkspacesFormat, IBackupMainService } from 'vs/platform/backup/common/backup';
import { IEnvironmentService } from 'vs/platform/environment/common/environment';
import { ILifecycleService } from 'vs/code/electron-main/lifecycle';
import { VSCodeWindow } from 'vs/code/electron-main/window';
export class BackupMainService implements IBackupMainService {
......@@ -21,13 +23,29 @@ export class BackupMainService implements IBackupMainService {
private workspacesJsonContent: IBackupWorkspacesFormat;
constructor(
@IEnvironmentService environmentService: IEnvironmentService
@IEnvironmentService environmentService: IEnvironmentService,
@ILifecycleService lifecycleService: ILifecycleService
) {
this.backupHome = environmentService.backupHome;
this.workspacesJsonPath = environmentService.backupWorkspacesPath;
if (lifecycleService) {
lifecycleService.onAfterUnload(this.onAfterUnloadWindow.bind(this));
}
this.loadSync();
}
private onAfterUnloadWindow(vscodeWindow: VSCodeWindow) {
if (vscodeWindow.openedWorkspacePath) {
// Clear out workspace from workspaces.json if it doesn't have any backups
const workspaceResource = Uri.file(vscodeWindow.openedWorkspacePath);
if (!this.hasWorkspaceBackup(workspaceResource)) {
this.removeWorkspaceBackupPathSync(workspaceResource);
}
}
}
public getWorkspaceBackupPaths(): string[] {
return this.workspacesJsonContent.folderWorkspaces;
}
......
......@@ -15,12 +15,12 @@ import extfs = require('vs/base/node/extfs');
import pfs = require('vs/base/node/pfs');
import Uri from 'vs/base/common/uri';
import { TestEnvironmentService } from 'vs/test/utils/servicesTestUtils';
import { BackupMainService } from 'vs/platform/backup/node/backupMainService';
import { BackupMainService } from 'vs/platform/backup/electron-main/backupMainService';
import { IBackupWorkspacesFormat } from 'vs/platform/backup/common/backup';
class TestBackupMainService extends BackupMainService {
constructor(backupHome: string, backupWorkspacesPath: string) {
super(TestEnvironmentService);
super(TestEnvironmentService, null);
this.backupHome = backupHome;
this.workspacesJsonPath = backupWorkspacesPath;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册