| 23 | * All cells queued using `queueCell` are added to the queue and processed in order they were added/queued. |
| 24 | */ |
| 25 | export class CellExecutionQueue implements Disposable { |
| 26 | private readonly queueOfItemsToExecute: (ICellExecution | ICodeExecution)[] = []; |
| 27 | private get queueOfCellsToExecute(): ICellExecution[] { |
| 28 | return this.queueOfItemsToExecute.filter((c) => c.type === 'cell').map((c) => c as ICellExecution); |
| 29 | } |
| 30 | private cancelledOrCompletedWithErrors = false; |
| 31 | private startedRunningCells = false; |
| 32 | private disposables: Disposable[] = []; |
| 33 | private lastCellExecution?: ICellExecution | ICodeExecution; |
| 34 | /** |
| 35 | * Whether all cells have completed processing or cancelled, or some completed & others cancelled. |
| 36 | */ |
| 37 | public get isEmpty(): boolean { |
| 38 | return this.queueOfItemsToExecute.length === 0; |
| 39 | } |
| 40 | /** |
| 41 | * Whether cells have been cancelled (as a result of interrupt or some have failed). |
| 42 | * Even if this property is true, its possible there is still some async operation pending (updating states of cells). |
| 43 | */ |
| 44 | public get failed(): boolean { |
| 45 | return this.cancelledOrCompletedWithErrors; |
| 46 | } |
| 47 | public get queue(): Readonly<NotebookCell[]> { |
| 48 | return this.queueOfCellsToExecute.map((cell) => cell.cell); |
| 49 | } |
| 50 | constructor( |
| 51 | private readonly session: Promise<IKernelSession>, |
| 52 | private readonly executionFactory: CellExecutionFactory, |
| 53 | readonly metadata: Readonly<KernelConnectionMetadata>, |
| 54 | readonly resourceUri: Resource, |
| 55 | private readonly notebook?: NotebookDocument, |
| 56 | private readonly snapshotService?: ISnapshotMetadataService |
| 57 | ) {} |
| 58 | |
| 59 | public dispose() { |
| 60 | this.disposables.forEach((d) => d.dispose()); |
| 61 | this.lastCellExecution?.dispose(); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Queue the cell for execution & start processing it immediately. |
| 66 | */ |
| 67 | public queueCell(cell: NotebookCell, codeOverride?: string): void { |
| 68 | this.enqueue({ cell, codeOverride }); |
| 69 | } |
| 70 | /** |
| 71 | * Queue the code for execution & start processing it immediately. |
| 72 | */ |
| 73 | public queueCode(code: string, extensionId: string, token: CancellationToken): ICodeExecution { |
| 74 | const item = this.enqueue({ code, extensionId, token }); |
| 75 | return item as ICodeExecution; |
| 76 | } |
| 77 | private enqueue( |
| 78 | options: |
| 79 | | { |
| 80 | cell: NotebookCell; |
| 81 | events?: { |
| 82 | started: EventEmitter<void>; |
nothing calls this directly
no outgoing calls
no test coverage detected