@codingame/monaco-vscode-working-copy-service-override 4.1.0 → 4.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,255 @@
1
+ import { Disposable, toDisposable } from 'vscode/vscode/vs/base/common/lifecycle';
2
+ import { CancellationTokenSource } from 'vscode/vscode/vs/base/common/cancellation';
3
+ import { Promises } from 'vscode/vscode/vs/base/common/async';
4
+
5
+ class WorkingCopyBackupTracker extends Disposable {
6
+ constructor(workingCopyBackupService, workingCopyService, logService, lifecycleService, filesConfigurationService, workingCopyEditorService, editorService, editorGroupService) {
7
+ super();
8
+ this.workingCopyBackupService = workingCopyBackupService;
9
+ this.workingCopyService = workingCopyService;
10
+ this.logService = logService;
11
+ this.lifecycleService = lifecycleService;
12
+ this.filesConfigurationService = filesConfigurationService;
13
+ this.workingCopyEditorService = workingCopyEditorService;
14
+ this.editorService = editorService;
15
+ this.editorGroupService = editorGroupService;
16
+ this.mapWorkingCopyToContentVersion = ( new Map());
17
+ this.pendingBackupOperations = ( new Map());
18
+ this.suspended = false;
19
+ this.unrestoredBackups = ( new Set());
20
+ this.whenReady = this.resolveBackupsToRestore();
21
+ this._isReady = false;
22
+ for (const workingCopy of this.workingCopyService.modifiedWorkingCopies) {
23
+ this.onDidRegister(workingCopy);
24
+ }
25
+ this.registerListeners();
26
+ }
27
+ registerListeners() {
28
+ this._register(this.workingCopyService.onDidRegister(workingCopy => this.onDidRegister(workingCopy)));
29
+ this._register(this.workingCopyService.onDidUnregister(workingCopy => this.onDidUnregister(workingCopy)));
30
+ this._register(this.workingCopyService.onDidChangeDirty(workingCopy => this.onDidChangeDirty(workingCopy)));
31
+ this._register(this.workingCopyService.onDidChangeContent(workingCopy => this.onDidChangeContent(workingCopy)));
32
+ this._register(this.lifecycleService.onBeforeShutdown(event => event.finalVeto(() => this.onFinalBeforeShutdown(event.reason), 'veto.backups')));
33
+ this._register(this.lifecycleService.onWillShutdown(() => this.onWillShutdown()));
34
+ this._register(this.workingCopyEditorService.onDidRegisterHandler(handler => this.restoreBackups(handler)));
35
+ }
36
+ onWillShutdown() {
37
+ this.cancelBackupOperations();
38
+ this.suspendBackupOperations();
39
+ }
40
+ static { this.DEFAULT_BACKUP_SCHEDULE_DELAYS = {
41
+ ['default']: 1000,
42
+ ['delayed']: 2000
43
+ }; }
44
+ onDidRegister(workingCopy) {
45
+ if (this.suspended) {
46
+ this.logService.warn(`[backup tracker] suspended, ignoring register event`, ( workingCopy.resource.toString()), workingCopy.typeId);
47
+ return;
48
+ }
49
+ if (workingCopy.isModified()) {
50
+ this.scheduleBackup(workingCopy);
51
+ }
52
+ }
53
+ onDidUnregister(workingCopy) {
54
+ this.mapWorkingCopyToContentVersion.delete(workingCopy);
55
+ if (this.suspended) {
56
+ this.logService.warn(`[backup tracker] suspended, ignoring unregister event`, ( workingCopy.resource.toString()), workingCopy.typeId);
57
+ return;
58
+ }
59
+ this.discardBackup(workingCopy);
60
+ }
61
+ onDidChangeDirty(workingCopy) {
62
+ if (this.suspended) {
63
+ this.logService.warn(`[backup tracker] suspended, ignoring dirty change event`, ( workingCopy.resource.toString()), workingCopy.typeId);
64
+ return;
65
+ }
66
+ if (workingCopy.isDirty()) {
67
+ this.scheduleBackup(workingCopy);
68
+ }
69
+ else {
70
+ this.discardBackup(workingCopy);
71
+ }
72
+ }
73
+ onDidChangeContent(workingCopy) {
74
+ const contentVersionId = this.getContentVersion(workingCopy);
75
+ this.mapWorkingCopyToContentVersion.set(workingCopy, contentVersionId + 1);
76
+ if (this.suspended) {
77
+ this.logService.warn(`[backup tracker] suspended, ignoring content change event`, ( workingCopy.resource.toString()), workingCopy.typeId);
78
+ return;
79
+ }
80
+ if (workingCopy.isModified()) {
81
+ this.scheduleBackup(workingCopy);
82
+ }
83
+ }
84
+ scheduleBackup(workingCopy) {
85
+ this.cancelBackupOperation(workingCopy);
86
+ this.logService.trace(`[backup tracker] scheduling backup`, ( workingCopy.resource.toString()), workingCopy.typeId);
87
+ const workingCopyIdentifier = { resource: workingCopy.resource, typeId: workingCopy.typeId };
88
+ const cts = ( new CancellationTokenSource());
89
+ const handle = setTimeout(async () => {
90
+ if (cts.token.isCancellationRequested) {
91
+ return;
92
+ }
93
+ if (workingCopy.isModified()) {
94
+ this.logService.trace(`[backup tracker] creating backup`, ( workingCopy.resource.toString()), workingCopy.typeId);
95
+ try {
96
+ const backup = await workingCopy.backup(cts.token);
97
+ if (cts.token.isCancellationRequested) {
98
+ return;
99
+ }
100
+ if (workingCopy.isModified()) {
101
+ this.logService.trace(`[backup tracker] storing backup`, ( workingCopy.resource.toString()), workingCopy.typeId);
102
+ await this.workingCopyBackupService.backup(workingCopy, backup.content, this.getContentVersion(workingCopy), backup.meta, cts.token);
103
+ }
104
+ }
105
+ catch (error) {
106
+ this.logService.error(error);
107
+ }
108
+ }
109
+ if (!cts.token.isCancellationRequested) {
110
+ this.doClearPendingBackupOperation(workingCopyIdentifier);
111
+ }
112
+ }, this.getBackupScheduleDelay(workingCopy));
113
+ this.pendingBackupOperations.set(workingCopyIdentifier, {
114
+ cancel: () => {
115
+ this.logService.trace(`[backup tracker] clearing pending backup creation`, ( workingCopy.resource.toString()), workingCopy.typeId);
116
+ cts.cancel();
117
+ },
118
+ disposable: toDisposable(() => {
119
+ cts.dispose();
120
+ clearTimeout(handle);
121
+ })
122
+ });
123
+ }
124
+ getBackupScheduleDelay(workingCopy) {
125
+ if (typeof workingCopy.backupDelay === 'number') {
126
+ return workingCopy.backupDelay;
127
+ }
128
+ let backupScheduleDelay;
129
+ if (workingCopy.capabilities & 2 ) {
130
+ backupScheduleDelay = 'default';
131
+ }
132
+ else {
133
+ backupScheduleDelay = this.filesConfigurationService.hasShortAutoSaveDelay(workingCopy.resource) ? 'delayed' : 'default';
134
+ }
135
+ return WorkingCopyBackupTracker.DEFAULT_BACKUP_SCHEDULE_DELAYS[backupScheduleDelay];
136
+ }
137
+ getContentVersion(workingCopy) {
138
+ return this.mapWorkingCopyToContentVersion.get(workingCopy) || 0;
139
+ }
140
+ discardBackup(workingCopy) {
141
+ this.cancelBackupOperation(workingCopy);
142
+ const workingCopyIdentifier = { resource: workingCopy.resource, typeId: workingCopy.typeId };
143
+ const cts = ( new CancellationTokenSource());
144
+ this.doDiscardBackup(workingCopyIdentifier, cts);
145
+ this.pendingBackupOperations.set(workingCopyIdentifier, {
146
+ cancel: () => {
147
+ this.logService.trace(`[backup tracker] clearing pending backup discard`, ( workingCopy.resource.toString()), workingCopy.typeId);
148
+ cts.cancel();
149
+ },
150
+ disposable: cts
151
+ });
152
+ }
153
+ async doDiscardBackup(workingCopyIdentifier, cts) {
154
+ this.logService.trace(`[backup tracker] discarding backup`, ( workingCopyIdentifier.resource.toString()), workingCopyIdentifier.typeId);
155
+ try {
156
+ await this.workingCopyBackupService.discardBackup(workingCopyIdentifier, cts.token);
157
+ }
158
+ catch (error) {
159
+ this.logService.error(error);
160
+ }
161
+ if (!cts.token.isCancellationRequested) {
162
+ this.doClearPendingBackupOperation(workingCopyIdentifier);
163
+ }
164
+ }
165
+ cancelBackupOperation(workingCopy) {
166
+ let workingCopyIdentifier = undefined;
167
+ for (const [identifier] of this.pendingBackupOperations) {
168
+ if (( identifier.resource.toString()) === ( workingCopy.resource.toString()) && identifier.typeId === workingCopy.typeId) {
169
+ workingCopyIdentifier = identifier;
170
+ break;
171
+ }
172
+ }
173
+ if (workingCopyIdentifier) {
174
+ this.doClearPendingBackupOperation(workingCopyIdentifier, { cancel: true });
175
+ }
176
+ }
177
+ doClearPendingBackupOperation(workingCopyIdentifier, options) {
178
+ const pendingBackupOperation = this.pendingBackupOperations.get(workingCopyIdentifier);
179
+ if (!pendingBackupOperation) {
180
+ return;
181
+ }
182
+ if (options?.cancel) {
183
+ pendingBackupOperation.cancel();
184
+ }
185
+ pendingBackupOperation.disposable.dispose();
186
+ this.pendingBackupOperations.delete(workingCopyIdentifier);
187
+ }
188
+ cancelBackupOperations() {
189
+ for (const [, operation] of this.pendingBackupOperations) {
190
+ operation.cancel();
191
+ operation.disposable.dispose();
192
+ }
193
+ this.pendingBackupOperations.clear();
194
+ }
195
+ suspendBackupOperations() {
196
+ this.suspended = true;
197
+ return { resume: () => this.suspended = false };
198
+ }
199
+ get isReady() { return this._isReady; }
200
+ async resolveBackupsToRestore() {
201
+ await this.lifecycleService.when(3 );
202
+ for (const backup of await this.workingCopyBackupService.getBackups()) {
203
+ this.unrestoredBackups.add(backup);
204
+ }
205
+ this._isReady = true;
206
+ }
207
+ async restoreBackups(handler) {
208
+ await this.whenReady;
209
+ const openedEditorsForBackups = ( new Set());
210
+ const nonOpenedEditorsForBackups = ( new Set());
211
+ const restoredBackups = ( new Set());
212
+ for (const unrestoredBackup of this.unrestoredBackups) {
213
+ const canHandleUnrestoredBackup = await handler.handles(unrestoredBackup);
214
+ if (!canHandleUnrestoredBackup) {
215
+ continue;
216
+ }
217
+ let hasOpenedEditorForBackup = false;
218
+ for (const { editor } of this.editorService.getEditors(0 )) {
219
+ const isUnrestoredBackupOpened = handler.isOpen(unrestoredBackup, editor);
220
+ if (isUnrestoredBackupOpened) {
221
+ openedEditorsForBackups.add(editor);
222
+ hasOpenedEditorForBackup = true;
223
+ }
224
+ }
225
+ if (!hasOpenedEditorForBackup) {
226
+ nonOpenedEditorsForBackups.add(await handler.createEditor(unrestoredBackup));
227
+ }
228
+ restoredBackups.add(unrestoredBackup);
229
+ }
230
+ if (nonOpenedEditorsForBackups.size > 0) {
231
+ await this.editorGroupService.activeGroup.openEditors(( [...nonOpenedEditorsForBackups].map(nonOpenedEditorForBackup => ({
232
+ editor: nonOpenedEditorForBackup,
233
+ options: {
234
+ pinned: true,
235
+ preserveFocus: true,
236
+ inactive: true
237
+ }
238
+ }))));
239
+ for (const nonOpenedEditorForBackup of nonOpenedEditorsForBackups) {
240
+ openedEditorsForBackups.add(nonOpenedEditorForBackup);
241
+ }
242
+ }
243
+ await Promises.settled(( [...openedEditorsForBackups].map(async (openedEditorForBackup) => {
244
+ if (this.editorService.isVisible(openedEditorForBackup)) {
245
+ return;
246
+ }
247
+ return openedEditorForBackup.resolve();
248
+ })));
249
+ for (const restoredBackup of restoredBackups) {
250
+ this.unrestoredBackups.delete(restoredBackup);
251
+ }
252
+ }
253
+ }
254
+
255
+ export { WorkingCopyBackupTracker };