From 8fbbd6985f686e7876b301ad5637052001caa128 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Krassowski?= <5832902+krassowski@users.noreply.github.com> Date: Wed, 7 Aug 2024 18:11:40 +0100 Subject: [PATCH] Expose `execution_state` in the JS package (#259) * Expose `execution_state` in the JS package * Use camelCase to satisfy the linter * Update API, add change event attribute --- javascript/src/api.ts | 14 ++++++++++++++ javascript/src/ycell.ts | 23 +++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/javascript/src/api.ts b/javascript/src/api.ts index a20b301..3eaf995 100644 --- a/javascript/src/api.ts +++ b/javascript/src/api.ts @@ -494,6 +494,8 @@ export interface ISharedBaseCell< toJSON(): nbformat.IBaseCell; } +export type IExecutionState = 'running' | 'idle'; + /** * Implements an API for nbformat.ICodeCell. */ @@ -509,6 +511,11 @@ export interface ISharedCodeCell */ execution_count: nbformat.ExecutionCount; + /** + * The code cell's execution state. + */ + executionState: IExecutionState; + /** * Cell outputs */ @@ -746,6 +753,13 @@ export type CellChange = SourceChange & { oldValue?: number; newValue?: number; }; + /** + * Cell execution state change + */ + executionStateChange?: { + oldValue?: IExecutionState; + newValue?: IExecutionState; + }; /** * Cell metadata change */ diff --git a/javascript/src/ycell.ts b/javascript/src/ycell.ts index 3a5729a..e60b3a1 100644 --- a/javascript/src/ycell.ts +++ b/javascript/src/ycell.ts @@ -10,6 +10,7 @@ import { Awareness } from 'y-protocols/awareness'; import * as Y from 'yjs'; import type { CellChange, + IExecutionState, IMapChange, ISharedAttachmentsCell, ISharedBaseCell, @@ -749,6 +750,20 @@ export class YCodeCell } } + /** + * The code cell's execution state. + */ + get executionState(): IExecutionState { + return this.ymodel.get('execution_state') ?? 'idle'; + } + set executionState(state: IExecutionState) { + if (this.ymodel.get('execution_state') !== state) { + this.transact(() => { + this.ymodel.set('execution_state', state); + }, false); + } + } + /** * Cell outputs. */ @@ -899,6 +914,14 @@ export class YCodeCell }; } + if (modelEvent && modelEvent.keysChanged.has('execution_state')) { + const change = modelEvent.changes.keys.get('execution_state'); + changes.executionStateChange = { + oldValue: change!.oldValue, + newValue: this.ymodel.get('execution_state') + }; + } + return changes; }