src/vs/workbench/contrib/notebook/browser/notebookEditorWidgetContextKeys.ts/NotebookEditorContextKeys/_handleDidChangeModel
class NotebookEditorContextKeys:	private _handleDidChangeModel(): void {

		this._updateKernelContext();

		this._viewModelDisposables.clear();
		dispose(this._cellStateListeners);
		this._cellStateListeners.length = 0;
		dispose(this._cellOutputsListeners);
		this._cellOutputsListeners.length = 0;

		if (!this._editor.hasModel()) {
			return;
		}

		let executionCount = 0;

		const addCellStateListener = (c: ICellViewModel) => {
			return (c as CellViewModel).onDidChangeState(e => {
				if (!e.runStateChanged) {
					return;
				}
				if (c.internalMetadata.runState === NotebookCellExecutionState.Pending) {
					executionCount++;
				} else if (!c.internalMetadata.runState) {
					executionCount--;
				}
				this._someCellRunning.set(executionCount > 0);
			});
		};

		const recomputeOutputsExistence = () => {
			let hasOutputs = false;
			if (this._editor.hasModel()) {
				for (let i = 0; i < this._editor.viewModel.viewCells.length; i++) {
					if (this._editor.viewModel.viewCells[i].outputsViewModels.length > 0) {
						hasOutputs = true;
						break;
					}
				}
			}

			this._hasOutputs.set(hasOutputs);
		};

		const addCellOutputsListener = (c: ICellViewModel) => {
			return c.model.onDidChangeOutputs(() => {
				recomputeOutputsExistence();
			});
		};

		for (const cell of this._editor.viewModel.viewCells) {
			this._cellStateListeners.push(addCellStateListener(cell));
			this._cellOutputsListeners.push(addCellOutputsListener(cell));
		}

		recomputeOutputsExistence();

		this._viewModelDisposables.add(this._editor.viewModel.onDidChangeViewCells(e => {
			e.splices.reverse().forEach(splice => {
				const [start, deleted, newCells] = splice;
				const deletedCellStates = this._cellStateListeners.splice(start, deleted, ...newCells.map(addCellStateListener));
				const deletedCellOutputStates = this._cellOutputsListeners.splice(start, deleted, ...newCells.map(addCellOutputsListener));
				dispose(deletedCellStates);
				dispose(deletedCellOutputStates);
			});
		}));
		this._viewType.set(this._editor.viewModel.viewType);
	}