Skip to content

Commit

Permalink
Expose execution_state in the JS package (#259)
Browse files Browse the repository at this point in the history
* Expose `execution_state` in the JS package

* Use camelCase to satisfy the linter

* Update API, add change event attribute
  • Loading branch information
krassowski authored Aug 7, 2024
1 parent 16162f9 commit 8fbbd69
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
14 changes: 14 additions & 0 deletions javascript/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,8 @@ export interface ISharedBaseCell<
toJSON(): nbformat.IBaseCell;
}

export type IExecutionState = 'running' | 'idle';

/**
* Implements an API for nbformat.ICodeCell.
*/
Expand All @@ -509,6 +511,11 @@ export interface ISharedCodeCell
*/
execution_count: nbformat.ExecutionCount;

/**
* The code cell's execution state.
*/
executionState: IExecutionState;

/**
* Cell outputs
*/
Expand Down Expand Up @@ -746,6 +753,13 @@ export type CellChange = SourceChange & {
oldValue?: number;
newValue?: number;
};
/**
* Cell execution state change
*/
executionStateChange?: {
oldValue?: IExecutionState;
newValue?: IExecutionState;
};
/**
* Cell metadata change
*/
Expand Down
23 changes: 23 additions & 0 deletions javascript/src/ycell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { Awareness } from 'y-protocols/awareness';
import * as Y from 'yjs';
import type {
CellChange,
IExecutionState,
IMapChange,
ISharedAttachmentsCell,
ISharedBaseCell,
Expand Down Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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;
}

Expand Down

0 comments on commit 8fbbd69

Please sign in to comment.