diff --git a/src/vs/workbench/api/browser/mainThreadNotebookKernels.ts b/src/vs/workbench/api/browser/mainThreadNotebookKernels.ts index f8a63895d67..86394fdf58d 100644 --- a/src/vs/workbench/api/browser/mainThreadNotebookKernels.ts +++ b/src/vs/workbench/api/browser/mainThreadNotebookKernels.ts @@ -14,9 +14,9 @@ import { NotebookDto } from 'vs/workbench/api/browser/mainThreadNotebookDto'; import { extHostNamedCustomer } from 'vs/workbench/api/common/extHostCustomers'; import { INotebookEditor } from 'vs/workbench/contrib/notebook/browser/notebookBrowser'; import { INotebookEditorService } from 'vs/workbench/contrib/notebook/browser/notebookEditorService'; -import { ICellExecuteUpdate, INotebookCellExecution, INotebookExecutionService } from 'vs/workbench/contrib/notebook/common/notebookExecutionService'; +import { INotebookCellExecution, INotebookExecutionService } from 'vs/workbench/contrib/notebook/common/notebookExecutionService'; import { INotebookKernel, INotebookKernelChangeEvent, INotebookKernelService } from 'vs/workbench/contrib/notebook/common/notebookKernelService'; -import { ICellExecuteUpdateDto, ExtHostContext, ExtHostNotebookKernelsShape, IExtHostContext, INotebookKernelDto2, MainContext, MainThreadNotebookKernelsShape } from '../common/extHost.protocol'; +import { ExtHostContext, ExtHostNotebookKernelsShape, ICellExecuteUpdateDto, IExtHostContext, INotebookKernelDto2, MainContext, MainThreadNotebookKernelsShape } from '../common/extHost.protocol'; abstract class MainThreadKernel implements INotebookKernel { @@ -99,7 +99,7 @@ export class MainThreadNotebookKernels implements MainThreadNotebookKernelsShape private readonly _kernels = new Map(); private readonly _proxy: ExtHostNotebookKernelsShape; - private readonly _executions = new Map(); + private readonly _executions = new Map(); constructor( extHostContext: IExtHostContext, @@ -229,9 +229,8 @@ export class MainThreadNotebookKernels implements MainThreadNotebookKernelsShape // --- execution $addExecution(handle: number, uri: UriComponents, cellHandle: number): void { - const execution = new MainThreadExecution(handle, URI.revive(uri), cellHandle); + const execution = this._notebookExecutionService.createNotebookCellExecution(URI.revive(uri), cellHandle); this._executions.set(handle, execution); - this._notebookExecutionService.registerNotebookCellExecution(execution); } $updateExecutions(updates: ICellExecuteUpdateDto[]): void { @@ -251,18 +250,3 @@ export class MainThreadNotebookKernels implements MainThreadNotebookKernelsShape }); } } - -class MainThreadExecution implements INotebookCellExecution { - private readonly _onDidChange = new Emitter(); - readonly onDidChange: Event> = this._onDidChange.event; - - constructor( - readonly id: number, - readonly notebook: URI, - readonly cellHandle: number, - ) { } - - update(updates: ICellExecuteUpdate[]): void { - this._onDidChange.fire(updates); - } -} diff --git a/src/vs/workbench/contrib/notebook/browser/notebookExecutionServiceImpl.ts b/src/vs/workbench/contrib/notebook/browser/notebookExecutionServiceImpl.ts index ff04e182af7..8213e7c9826 100644 --- a/src/vs/workbench/contrib/notebook/browser/notebookExecutionServiceImpl.ts +++ b/src/vs/workbench/contrib/notebook/browser/notebookExecutionServiceImpl.ts @@ -3,6 +3,7 @@ * Licensed under the MIT License. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------*/ +import { URI } from 'vs/base/common/uri'; import { NotebookTextModel } from 'vs/workbench/contrib/notebook/common/model/notebookTextModel'; import { CellEditType, ICellEditOperation, NotebookCellExecutionState, NotebookCellInternalMetadata } from 'vs/workbench/contrib/notebook/common/notebookCommon'; import { CellExecutionUpdateType, ICellExecuteUpdate, INotebookCellExecution, INotebookExecutionService } from 'vs/workbench/contrib/notebook/common/notebookExecutionService'; @@ -16,33 +17,8 @@ export class NotebookExecutionService implements INotebookExecutionService { ) { } - registerNotebookCellExecution(execution: INotebookCellExecution): void { - const notebook = this._notebookService.getNotebookTextModel(execution.notebook); - if (!notebook) { - return; - } - - const startExecuteEdit: ICellEditOperation = { - editType: CellEditType.PartialInternalMetadata, - handle: execution.cellHandle, - internalMetadata: { - runState: NotebookCellExecutionState.Pending - } - }; - this._applyExecutionEdits(notebook, [startExecuteEdit]); - - const listener = execution.onDidChange(updates => { - const edits = updates.map(update => updateToEdit(update, execution.cellHandle)); - if (updates.some(update => update.editType === CellExecutionUpdateType.Complete)) { - listener.dispose(); - } - - this._applyExecutionEdits(notebook, edits); - }); - } - - private _applyExecutionEdits(notebook: NotebookTextModel, edits: ICellEditOperation[]): void { - notebook.applyEdits(edits, true, undefined, () => undefined, undefined, false); + createNotebookCellExecution(notebook: URI, cellHandle: number): INotebookCellExecution { + return new CellExecution(notebook, cellHandle, this._notebookService); } } @@ -90,3 +66,38 @@ function updateToEdit(update: ICellExecuteUpdate, cellHandle: number): ICellEdit throw new Error('Unknown cell update type'); } + +class CellExecution implements INotebookCellExecution { + private readonly _notebookModel: NotebookTextModel; + + constructor( + readonly notebook: URI, + readonly cellHandle: number, + private readonly _notebookService: INotebookService, + ) { + const notebookModel = this._notebookService.getNotebookTextModel(notebook); + if (!notebookModel) { + throw new Error('Notebook not found: ' + notebook); + } + + this._notebookModel = notebookModel; + + const startExecuteEdit: ICellEditOperation = { + editType: CellEditType.PartialInternalMetadata, + handle: cellHandle, + internalMetadata: { + runState: NotebookCellExecutionState.Pending + } + }; + this._applyExecutionEdits([startExecuteEdit]); + } + + update(updates: ICellExecuteUpdate[]): void { + const edits = updates.map(update => updateToEdit(update, this.cellHandle)); + this._applyExecutionEdits(edits); + } + + private _applyExecutionEdits(edits: ICellEditOperation[]): void { + this._notebookModel.applyEdits(edits, true, undefined, () => undefined, undefined, false); + } +} diff --git a/src/vs/workbench/contrib/notebook/common/notebookExecutionService.ts b/src/vs/workbench/contrib/notebook/common/notebookExecutionService.ts index a95e6eda8b5..7ba8fd9b77a 100644 --- a/src/vs/workbench/contrib/notebook/common/notebookExecutionService.ts +++ b/src/vs/workbench/contrib/notebook/common/notebookExecutionService.ts @@ -3,7 +3,6 @@ * Licensed under the MIT License. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------*/ -import { Event } from 'vs/base/common/event'; import { URI } from 'vs/base/common/uri'; import { createDecorator } from 'vs/platform/instantiation/common/instantiation'; import { IOutputDto, IOutputItemDto } from 'vs/workbench/contrib/notebook/common/notebookCommon'; @@ -48,10 +47,9 @@ export interface ICellExecutionComplete { } export interface INotebookCellExecution { - readonly id: number; readonly notebook: URI; readonly cellHandle: number; - readonly onDidChange: Event>; + update(updates: ICellExecuteUpdate[]): void; } export const INotebookExecutionService = createDecorator('INotebookExecutionService'); @@ -59,5 +57,6 @@ export const INotebookExecutionService = createDecorator