|
4 | 4 | 'use strict'; |
5 | 5 |
|
6 | 6 | import { inject, injectable } from 'inversify'; |
7 | | -import { CancellationToken, Uri } from 'vscode'; |
| 7 | +import { CancellationToken, EventEmitter, Uri } from 'vscode'; |
8 | 8 | import type { NotebookCell, NotebookDocument, NotebookKernel as VSCNotebookKernel } from 'vscode-proposed'; |
| 9 | +import { noop } from '../../common/utils/misc'; |
9 | 10 | import { INotebookExecutionService } from './types'; |
10 | 11 |
|
| 12 | +/** |
| 13 | + * Cancellation token source that can be cancelled multiple times. |
| 14 | + */ |
| 15 | +class MultiCancellationTokenSource { |
| 16 | + /** |
| 17 | + * The cancellation token of this source. |
| 18 | + */ |
| 19 | + public readonly token: CancellationToken; |
| 20 | + private readonly eventEmitter = new EventEmitter<void>(); |
| 21 | + constructor() { |
| 22 | + this.token = { |
| 23 | + isCancellationRequested: false, |
| 24 | + onCancellationRequested: this.eventEmitter.event.bind(this.eventEmitter) |
| 25 | + }; |
| 26 | + } |
| 27 | + public cancel(): void { |
| 28 | + this.token.isCancellationRequested = true; |
| 29 | + this.eventEmitter.fire(); |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * Dispose object and free resources. |
| 34 | + */ |
| 35 | + public dispose(): void { |
| 36 | + this.eventEmitter.dispose(); |
| 37 | + } |
| 38 | +} |
| 39 | + |
11 | 40 | /** |
12 | 41 | * VSC will use this class to execute cells in a notebook. |
13 | 42 | * This is where we hookup Jupyter with a Notebook in VSCode. |
14 | 43 | */ |
15 | 44 | @injectable() |
16 | 45 | export class NotebookKernel implements VSCNotebookKernel { |
17 | | - private _preloads: Uri[] = []; |
18 | | - |
19 | 46 | get preloads(): Uri[] { |
20 | | - return this._preloads; |
| 47 | + return []; |
21 | 48 | } |
22 | | - constructor(@inject(INotebookExecutionService) private readonly execution: INotebookExecutionService) {} |
23 | 49 | public get label(): string { |
24 | 50 | return 'Jupyter'; |
25 | 51 | } |
26 | | - |
27 | | - public async executeCell(document: NotebookDocument, cell: NotebookCell, token: CancellationToken): Promise<void> { |
28 | | - return this.execution.executeCell(document, cell, token); |
| 52 | + private cellExecutions = new WeakMap<NotebookCell, MultiCancellationTokenSource>(); |
| 53 | + private documentExecutions = new WeakMap<NotebookDocument, MultiCancellationTokenSource>(); |
| 54 | + constructor(@inject(INotebookExecutionService) private readonly execution: INotebookExecutionService) {} |
| 55 | + public executeCell(document: NotebookDocument, cell: NotebookCell) { |
| 56 | + if (this.cellExecutions.has(cell)) { |
| 57 | + return; |
| 58 | + } |
| 59 | + const source = new MultiCancellationTokenSource(); |
| 60 | + this.cellExecutions.set(cell, source); |
| 61 | + this.execution |
| 62 | + .executeCell(document, cell, source.token) |
| 63 | + .finally(() => { |
| 64 | + if (this.cellExecutions.get(cell) === source) { |
| 65 | + this.cellExecutions.delete(cell); |
| 66 | + } |
| 67 | + }) |
| 68 | + .catch(noop); |
| 69 | + } |
| 70 | + public executeAllCells(document: NotebookDocument) { |
| 71 | + if (this.documentExecutions.has(document)) { |
| 72 | + return; |
| 73 | + } |
| 74 | + const source = new MultiCancellationTokenSource(); |
| 75 | + this.documentExecutions.set(document, source); |
| 76 | + this.execution |
| 77 | + .executeAllCells(document, source.token) |
| 78 | + .finally(() => { |
| 79 | + if (this.documentExecutions.get(document) === source) { |
| 80 | + this.documentExecutions.delete(document); |
| 81 | + } |
| 82 | + }) |
| 83 | + .catch(noop); |
| 84 | + } |
| 85 | + public cancelCellExecution(_document: NotebookDocument, cell: NotebookCell) { |
| 86 | + this.cellExecutions.get(cell)?.cancel(); // NOSONAR |
29 | 87 | } |
30 | | - public async executeAllCells(document: NotebookDocument, token: CancellationToken): Promise<void> { |
31 | | - return this.execution.executeAllCells(document, token); |
| 88 | + public cancelAllCellsExecution(document: NotebookDocument) { |
| 89 | + this.documentExecutions.get(document)?.cancel(); // NOSONAR |
32 | 90 | } |
33 | 91 | } |
0 commit comments