3
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
4
*--------------------------------------------------------------------------------------------*/
5
5
6
+
import { CancelablePromise } from 'vs/base/common/async';
6
7
import { Emitter, Event } from 'vs/base/common/event';
7
8
import { Disposable } from 'vs/base/common/lifecycle';
8
9
import { URI } from 'vs/base/common/uri';
9
-
import { ICustomEditorModel, CustomEditorEdit, CustomEditorSaveAsEvent, CustomEditorSaveEvent } from 'vs/workbench/contrib/customEditor/common/customEditor';
10
-
import { WorkingCopyCapabilities } from 'vs/workbench/services/workingCopy/common/workingCopyService';
11
-
import { ISaveOptions, IRevertOptions } from 'vs/workbench/common/editor';
10
+
import { IRevertOptions, ISaveOptions } from 'vs/workbench/common/editor';
11
+
import { CustomEditorEdit, CustomEditorSaveAsEvent, CustomEditorSaveEvent, ICustomEditorModel } from 'vs/workbench/contrib/customEditor/common/customEditor';
12
+
import { IWorkingCopyBackup, WorkingCopyCapabilities } from 'vs/workbench/services/workingCopy/common/workingCopyService';
12
13
import { ILabelService } from 'vs/platform/label/common/label';
13
14
import { basename } from 'vs/base/common/path';
14
15
16
+
namespace HotExitState {
17
+
export const enum Type {
18
+
NotSupported,
19
+
Allowed,
20
+
NotAllowed,
21
+
Pending,
22
+
}
23
+
24
+
export const NotSupported = Object.freeze({ type: Type.NotSupported } as const);
25
+
export const Allowed = Object.freeze({ type: Type.Allowed } as const);
26
+
export const NotAllowed = Object.freeze({ type: Type.NotAllowed } as const);
27
+
28
+
export class Pending {
29
+
readonly type = Type.Pending;
30
+
31
+
constructor(
32
+
public readonly operation: CancelablePromise<boolean>,
33
+
) { }
34
+
}
35
+
36
+
export type State = typeof NotSupported | typeof Allowed | typeof NotAllowed | Pending;
37
+
}
38
+
15
39
export class CustomEditorModel extends Disposable implements ICustomEditorModel {
16
40
17
41
private _currentEditIndex: number = -1;
18
42
private _savePoint: number = -1;
19
43
private readonly _edits: Array<CustomEditorEdit> = [];
44
+
private _hotExitState: HotExitState.State = HotExitState.NotSupported;
20
45
21
46
constructor(
22
47
public readonly viewType: string,
23
48
private readonly _resource: URI,
24
-
private readonly labelService: ILabelService
49
+
private readonly labelService: ILabelService,
25
50
) {
26
51
super();
27
52
}
@@ -72,7 +97,20 @@ export class CustomEditorModel extends Disposable implements ICustomEditorModel
72
97
protected readonly _onWillSaveAs = this._register(new Emitter<CustomEditorSaveAsEvent>());
73
98
readonly onWillSaveAs = this._onWillSaveAs.event;
74
99
75
-
public pushEdit(edit: CustomEditorEdit, trigger: any): void {
100
+
private _onBackup: undefined | (() => CancelablePromise<boolean>);
101
+
102
+
public onBackup(f: () => CancelablePromise<boolean>) {
103
+
if (this._onBackup) {
104
+
throw new Error('Backup already implemented');
105
+
}
106
+
this._onBackup = f;
107
+
108
+
if (this._hotExitState === HotExitState.NotSupported) {
109
+
this._hotExitState = this.isDirty() ? HotExitState.NotAllowed : HotExitState.Allowed;
110
+
}
111
+
}
112
+
113
+
public pushEdit(edit: CustomEditorEdit, trigger: any) {
76
114
this.spliceEdits(edit);
77
115
78
116
this._currentEditIndex = this._edits.length - 1;
@@ -196,4 +234,36 @@ export class CustomEditorModel extends Disposable implements ICustomEditorModel
196
234
this.updateDirty();
197
235
this.updateContentChanged();
198
236
}
237
+
238
+
public async backup(): Promise<IWorkingCopyBackup> {
239
+
if (this._hotExitState === HotExitState.NotSupported) {
240
+
throw new Error('Not supported');
241
+
}
242
+
243
+
if (this._hotExitState.type === HotExitState.Type.Pending) {
244
+
this._hotExitState.operation.cancel();
245
+
}
246
+
this._hotExitState = HotExitState.NotAllowed;
247
+
248
+
const pendingState = new HotExitState.Pending(this._onBackup!());
249
+
this._hotExitState = pendingState;
250
+
251
+
try {
252
+
this._hotExitState = await pendingState.operation ? HotExitState.Allowed : HotExitState.NotAllowed;
253
+
} catch (e) {
254
+
// Make sure state has not changed in the meantime
255
+
if (this._hotExitState === pendingState) {
256
+
this._hotExitState = HotExitState.NotAllowed;
257
+
}
258
+
}
259
+
260
+
if (this._hotExitState === HotExitState.Allowed) {
261
+
return {
262
+
meta: {
263
+
viewType: this.viewType,
264
+
}
265
+
};
266
+
}
267
+
throw new Error('Cannot back up in this state');
268
+
}
199
269
}
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4