1
2
3
4
5
6
7
8
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
|
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { Emitter } from 'vs/base/common/event';
import { combinedDisposable, Disposable, IDisposable } from 'vs/base/common/lifecycle';
import { ResourceMap } from 'vs/base/common/map';
import { isEqual } from 'vs/base/common/resources';
import { URI } from 'vs/base/common/uri';
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
import { ILogService } from 'vs/platform/log/common/log';
import { NotebookTextModel } from 'vs/workbench/contrib/notebook/common/model/notebookTextModel';
import { CellEditType, CellUri, ICellEditOperation, NotebookCellExecutionState, NotebookCellInternalMetadata, NotebookTextModelWillAddRemoveEvent } from 'vs/workbench/contrib/notebook/common/notebookCommon';
import { CellExecutionUpdateType, INotebookExecutionService } from 'vs/workbench/contrib/notebook/common/notebookExecutionService';
import { ICellExecuteUpdate, ICellExecutionComplete, ICellExecutionStateChangedEvent, ICellExecutionStateUpdate, INotebookCellExecution, INotebookExecutionStateService } from 'vs/workbench/contrib/notebook/common/notebookExecutionStateService';
import { INotebookService } from 'vs/workbench/contrib/notebook/common/notebookService';
export class NotebookExecutionStateService extends Disposable implements INotebookExecutionStateService {
declare _serviceBrand: undefined;
private readonly _executions = new ResourceMap<Map<number, CellExecution>>();
private readonly _notebookListeners = new ResourceMap<NotebookExecutionListeners>();
private readonly _cellListeners = new ResourceMap<IDisposable>();
private readonly _onDidChangeCellExecution = this._register(new Emitter<ICellExecutionStateChangedEvent>());
onDidChangeCellExecution = this._onDidChangeCellExecution.event;
constructor(
@IInstantiationService private readonly _instantiationService: IInstantiationService,
@ILogService private readonly _logService: ILogService,
@INotebookService private readonly _notebookService: INotebookService,
) {
super();
}
forceCancelNotebookExecutions(notebookUri: URI): void {
const notebookExecutions = this._executions.get(notebookUri);
if (!notebookExecutions) {
return;
}
for (const exe of notebookExecutions.values()) {
this._onCellExecutionDidComplete(notebookUri, exe.cellHandle, exe);
}
}
getCellExecution(cellUri: URI): INotebookCellExecution | undefined {
const parsed = CellUri.parse(cellUri);
if (!parsed) {
throw new Error(`Not a cell URI: ${cellUri}`);
}
const exeMap = this._executions.get(parsed.notebook);
if (exeMap) {
return exeMap.get(parsed.handle);
}
return undefined;
}
getCellExecutionStatesForNotebook(notebook: URI): INotebookCellExecution[] {
const exeMap = this._executions.get(notebook);
return exeMap ? Array.from(exeMap.values()) : [];
}
private _onCellExecutionDidChange(notebookUri: URI, cellHandle: number, exe: CellExecution): void {
this._onDidChangeCellExecution.fire(new NotebookExecutionEvent(notebookUri, cellHandle, exe));
}
private _onCellExecutionDidComplete(notebookUri: URI, cellHandle: number, exe: CellExecution): void {
const notebookExecutions = this._executions.get(notebookUri);
if (!notebookExecutions) {
this._logService.debug(`NotebookExecutionStateService#_onCellExecutionDidComplete - unknown notebook ${notebookUri.toString()}`);
return;
}
exe.dispose();
const cellUri = CellUri.generate(notebookUri, cellHandle);
this._cellListeners.get(cellUri)?.dispose();
this._cellListeners.delete(cellUri);
notebookExecutions.delete(cellHandle);
if (notebookExecutions.size === 0) {
this._executions.delete(notebookUri);
this._notebookListeners.get(notebookUri)?.dispose();
this._notebookListeners.delete(notebookUri);
}
this._onDidChangeCellExecution.fire(new NotebookExecutionEvent(notebookUri, cellHandle));
}
createCellExecution(notebookUri: URI, cellHandle: number): INotebookCellExecution {
const notebook = this._notebookService.getNotebookTextModel(notebookUri);
if (!notebook) {
throw new Error(`Notebook not found: ${notebookUri.toString()}`);
}
let notebookExecutionMap = this._executions.get(notebookUri);
if (!notebookExecutionMap) {
const listeners = this._instantiationService.createInstance(NotebookExecutionListeners, notebookUri);
this._notebookListeners.set(notebookUri, listeners);
notebookExecutionMap = new Map<number, CellExecution>();
this._executions.set(notebookUri, notebookExecutionMap);
}
let exe = notebookExecutionMap.get(cellHandle);
if (!exe) {
exe = this._createNotebookCellExecution(notebook, cellHandle);
notebookExecutionMap.set(cellHandle, exe);
this._onDidChangeCellExecution.fire(new NotebookExecutionEvent(notebookUri, cellHandle, exe));
}
return exe;
}
private _createNotebookCellExecution(notebook: NotebookTextModel, cellHandle: number): CellExecution {
const notebookUri = notebook.uri;
const exe: CellExecution = this._instantiationService.createInstance(CellExecution, cellHandle, notebook);
const disposable = combinedDisposable(
exe.onDidUpdate(() => this._onCellExecutionDidChange(notebookUri, cellHandle, exe)),
exe.onDidComplete(() => this._onCellExecutionDidComplete(notebookUri, cellHandle, exe)));
this._cellListeners.set(CellUri.generate(notebookUri, cellHandle), disposable);
return exe;
}
override dispose(): void {
super.dispose();
this._executions.forEach(executionMap => {
executionMap.forEach(execution => execution.dispose());
executionMap.clear();
});
this._executions.clear();
this._cellListeners.forEach(disposable => disposable.dispose());
this._notebookListeners.forEach(disposable => disposable.dispose());
}
}
class NotebookExecutionEvent implements ICellExecutionStateChangedEvent {
constructor(
readonly notebook: URI,
readonly cellHandle: number,
readonly changed?: CellExecution
) { }
affectsCell(cell: URI): boolean {
const parsedUri = CellUri.parse(cell);
return !!parsedUri && isEqual(this.notebook, parsedUri.notebook) && this.cellHandle === parsedUri.handle;
}
affectsNotebook(notebook: URI): boolean {
return isEqual(this.notebook, notebook);
}
}
class NotebookExecutionListeners extends Disposable {
private readonly _notebookModel: NotebookTextModel;
constructor(
notebook: URI,
@INotebookService private readonly _notebookService: INotebookService,
@INotebookExecutionService private readonly _notebookExecutionService: INotebookExecutionService,
@INotebookExecutionStateService private readonly _notebookExecutionStateService: INotebookExecutionStateService,
@ILogService private readonly _logService: ILogService,
) {
super();
this._logService.debug(`NotebookExecution#ctor ${notebook.toString()}`);
const notebookModel = this._notebookService.getNotebookTextModel(notebook);
if (!notebookModel) {
throw new Error('Notebook not found: ' + notebook);
}
this._notebookModel = notebookModel;
this._register(this._notebookModel.onWillAddRemoveCells(e => this.onWillAddRemoveCells(e)));
this._register(this._notebookModel.onWillDispose(() => this.onWillDisposeDocument()));
}
private cancelAll(): void {
this._logService.debug(`NotebookExecutionListeners#cancelAll`);
const exes = this._notebookExecutionStateService.getCellExecutionStatesForNotebook(this._notebookModel.uri);
this._notebookExecutionService.cancelNotebookCellHandles(this._notebookModel, exes.map(exe => exe.cellHandle));
}
private onWillDisposeDocument(): void {
this._logService.debug(`NotebookExecution#onWillDisposeDocument`);
this.cancelAll();
}
private onWillAddRemoveCells(e: NotebookTextModelWillAddRemoveEvent): void {
const notebookExes = this._notebookExecutionStateService.getCellExecutionStatesForNotebook(this._notebookModel.uri);
const handles = new Set(notebookExes.map(exe => exe.cellHandle));
const myDeletedHandles = new Set<number>();
e.rawEvent.changes.forEach(([start, deleteCount]) => {
if (deleteCount) {
const deletedHandles = this._notebookModel.cells.slice(start, start + deleteCount).map(c => c.handle);
deletedHandles.forEach(h => {
if (handles.has(h)) {
myDeletedHandles.add(h);
}
});
}
return false;
});
if (myDeletedHandles.size) {
this._logService.debug(`NotebookExecution#onWillAddRemoveCells, ${JSON.stringify([...myDeletedHandles])}`);
this._notebookExecutionService.cancelNotebookCellHandles(this._notebookModel, myDeletedHandles);
}
}
}
function updateToEdit(update: ICellExecuteUpdate, cellHandle: number): ICellEditOperation {
if (update.editType === CellExecutionUpdateType.Output) {
return {
editType: CellEditType.Output,
handle: update.cellHandle,
append: update.append,
outputs: update.outputs,
};
} else if (update.editType === CellExecutionUpdateType.OutputItems) {
return {
editType: CellEditType.OutputItems,
items: update.items,
append: update.append,
outputId: update.outputId
};
} else if (update.editType === CellExecutionUpdateType.ExecutionState) {
const newInternalMetadata: Partial<NotebookCellInternalMetadata> = {};
if (typeof update.executionOrder !== 'undefined') {
newInternalMetadata.executionOrder = update.executionOrder;
}
if (typeof update.runStartTime !== 'undefined') {
newInternalMetadata.runStartTime = update.runStartTime;
}
return {
editType: CellEditType.PartialInternalMetadata,
handle: cellHandle,
internalMetadata: newInternalMetadata
};
}
throw new Error('Unknown cell update type');
}
class CellExecution extends Disposable implements INotebookCellExecution {
private readonly _onDidUpdate = this._register(new Emitter<void>());
readonly onDidUpdate = this._onDidUpdate.event;
private readonly _onDidComplete = this._register(new Emitter<void>());
readonly onDidComplete = this._onDidComplete.event;
private _state: NotebookCellExecutionState = NotebookCellExecutionState.Unconfirmed;
get state() {
return this._state;
}
get notebook(): URI {
return this._notebookModel.uri;
}
private _didPause = false;
get didPause() {
return this._didPause;
}
private _isPaused = false;
get isPaused() {
return this._isPaused;
}
constructor(
readonly cellHandle: number,
private readonly _notebookModel: NotebookTextModel,
@ILogService private readonly _logService: ILogService,
) {
super();
this._logService.debug(`CellExecution#ctor ${this.getCellLog()}`);
const startExecuteEdit: ICellEditOperation = {
editType: CellEditType.PartialInternalMetadata,
handle: this.cellHandle,
internalMetadata: {
runStartTime: null,
runEndTime: null,
lastRunSuccess: null,
executionOrder: null,
}
};
this._applyExecutionEdits([startExecuteEdit]);
}
private getCellLog(): string {
return `${this._notebookModel.uri.toString()}, ${this.cellHandle}`;
}
private logUpdates(updates: ICellExecuteUpdate[]): void {
const updateTypes = updates.map(u => CellExecutionUpdateType[u.editType]).join(', ');
this._logService.debug(`CellExecution#updateExecution ${this.getCellLog()}, [${updateTypes}]`);
}
confirm() {
this._logService.debug(`CellExecution#confirm ${this.getCellLog()}`);
this._state = NotebookCellExecutionState.Pending;
this._onDidUpdate.fire();
}
update(updates: ICellExecuteUpdate[]): void {
this.logUpdates(updates);
if (updates.some(u => u.editType === CellExecutionUpdateType.ExecutionState)) {
this._state = NotebookCellExecutionState.Executing;
}
if (!this._didPause && updates.some(u => u.editType === CellExecutionUpdateType.ExecutionState && u.didPause)) {
this._didPause = true;
}
const lastIsPausedUpdate = [...updates].reverse().find(u => u.editType === CellExecutionUpdateType.ExecutionState && typeof u.isPaused === 'boolean');
if (lastIsPausedUpdate) {
this._isPaused = (lastIsPausedUpdate as ICellExecutionStateUpdate).isPaused!;
}
const cellModel = this._notebookModel.cells.find(c => c.handle === this.cellHandle);
if (!cellModel) {
this._logService.debug(`CellExecution#update, updating cell not in notebook: ${this._notebookModel.uri.toString()}, ${this.cellHandle}`);
} else {
const edits = updates.map(update => updateToEdit(update, this.cellHandle));
this._applyExecutionEdits(edits);
}
this._onDidUpdate.fire();
}
complete(completionData: ICellExecutionComplete): void {
const cellModel = this._notebookModel.cells.find(c => c.handle === this.cellHandle);
if (!cellModel) {
this._logService.debug(`CellExecution#complete, completing cell not in notebook: ${this._notebookModel.uri.toString()}, ${this.cellHandle}`);
} else {
const edit: ICellEditOperation = {
editType: CellEditType.PartialInternalMetadata,
handle: this.cellHandle,
internalMetadata: {
lastRunSuccess: completionData.lastRunSuccess,
runStartTime: this._didPause ? null : cellModel.internalMetadata.runStartTime,
runEndTime: this._didPause ? null : completionData.runEndTime,
}
};
this._applyExecutionEdits([edit]);
}
this._onDidComplete.fire();
}
private _applyExecutionEdits(edits: ICellEditOperation[]): void {
this._notebookModel.applyEdits(edits, true, undefined, () => undefined, undefined, false);
}
}
|