Skip to content

Commit bbc2378

Browse files
MarcosVnMarcos Alves
andauthored
Control session and kernel initialization individually per Cell (#276)
* feat: control isKernelAvaiable for each cell (#273) * feat: improve event and add flag to allCellsReady (#273) * feat: control isKernelAvaiable for each cell (#273) --------- Co-authored-by: Marcos Alves <marcos@aevotech.com.br>
1 parent 0af4aae commit bbc2378

File tree

4 files changed

+46
-13
lines changed

4 files changed

+46
-13
lines changed

packages/react/src/components/cell/Cell.tsx

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
import { useState, useEffect } from 'react';
88
import { CodeCell, MarkdownCell } from '@jupyterlab/cells';
9-
import { KernelMessage } from '@jupyterlab/services';
109
import { Box } from '@primer/react';
1110
import CellAdapter from './CellAdapter';
1211
import Lumino from '../lumino/Lumino';
@@ -61,24 +60,28 @@ export const Cell = (props: ICellProps) => {
6160

6261
adapter.sessionContext.initialize().then(() => {
6362
if (!autoStart || !adapter.cell.model) {
64-
return
63+
return;
6564
}
66-
65+
6766
// Perform auto-start for code or markdown cells
6867
if (adapter.cell instanceof CodeCell) {
69-
const execute = CodeCell.execute(
68+
CodeCell.execute(
7069
adapter.cell,
7170
adapter.sessionContext
7271
);
73-
execute.then((msg: void | KernelMessage.IExecuteReplyMsg) => {
74-
cellStore.setKernelAvailable(true);
75-
});
7672
}
7773

7874
if (adapter.cell instanceof MarkdownCell) {
7975
adapter.cell.rendered = true;
8076
}
8177
});
78+
79+
adapter.sessionContext.kernelChanged.connect(() => {
80+
void adapter.sessionContext.session?.kernel?.info.then(info => {
81+
// Set that session/kernel is ready for this cell when the kernel is guaranteed to be connected
82+
cellStore.setIsKernelSessionAvailable(id, true);
83+
})
84+
});
8285
}
8386

8487
useEffect(() => {

packages/react/src/components/cell/CellAdapter.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,9 @@ export class CellAdapter {
268268
if (this._type === 'code') {
269269
const lang = info.language_info;
270270
const mimeType = mimeService.getMimeTypeByLanguage(lang);
271-
this._cell.model.mimeType = mimeType;
271+
if (this._cell.model) {
272+
this._cell.model.mimeType = mimeType;
273+
}
272274
}
273275
});
274276
});

packages/react/src/components/cell/CellState.ts

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,30 +12,45 @@ export interface ICellState {
1212
source?: string;
1313
outputsCount?: number;
1414
adapter?: CellAdapter;
15+
isKernelSessionAvailable?: boolean; // Individual, for cell
1516
}
1617

1718
export interface ICellsState {
1819
cells: Map<string, ICellState>;
19-
kernelAvailable: boolean; // Currently shared between all cells
20+
areAllKernelSessionsReady: boolean; // Control the state for all cells
2021
}
2122

2223
export type CellState = ICellsState & {
2324
setCells: (cells: Map<string, ICellState>) => void;
2425
setSource: (id: string, source: string) => void;
2526
setOutputsCount: (id: string, outputsCount: number) => void;
26-
setKernelAvailable: (kernelAvailable: boolean) => void;
27+
setIsKernelSessionAvailable: (id: string, kernelAvailable: boolean) => void;
2728
setAdapter: (id: string, adapter?: CellAdapter) => void;
2829
getAdapter: (id: string) => CellAdapter | undefined;
2930
getSource: (id: string) => string | undefined;
3031
getOutputsCount: (id: string) => number | undefined;
32+
getIsKernelSessionAvailable: (id: string) => boolean | undefined;
3133
execute: (id?: string) => void;
3234
};
3335

36+
/**
37+
* Iterate over all cells map and check if all cells/sessions are ready
38+
*/
39+
const areAllKernelSessionsAvailable = (cells: Map<string, ICellState>): boolean => {
40+
for (const cell of cells.values()) {
41+
if (!cell.isKernelSessionAvailable) {
42+
return false;
43+
}
44+
}
45+
return true;
46+
};
47+
3448
export const cellStore = createStore<CellState>((set, get) => ({
3549
cells: new Map<string, ICellState>(),
3650
source: '',
3751
outputsCount: 0,
38-
kernelAvailable: false,
52+
isKernelSessionAvailable: false,
53+
areAllKernelSessionsReady: false,
3954
adapter: undefined,
4055
setCells: (cells: Map<string, ICellState>) => set((cell: CellState) => ({ cells })),
4156

@@ -59,7 +74,17 @@ export const cellStore = createStore<CellState>((set, get) => ({
5974
}
6075
set((state: CellState) => ({ cells }))
6176
},
62-
setKernelAvailable: (kernelAvailable: boolean) => set((state: CellState) => ({ kernelAvailable })),
77+
setIsKernelSessionAvailable: (id: string, isKernelSessionAvailable: boolean) => {
78+
const cells = get().cells;
79+
const cell = cells.get(id);
80+
if (cell) {
81+
cell.isKernelSessionAvailable = isKernelSessionAvailable;
82+
} else {
83+
cells.set(id, {isKernelSessionAvailable});
84+
}
85+
const areAllKernelSessionsReady = areAllKernelSessionsAvailable(cells);
86+
set((cell: CellState) => ({ cells, areAllKernelSessionsReady }));
87+
},
6388
setAdapter: (id: string, adapter?: CellAdapter) => {
6489
const cells = get().cells;
6590
const cell = cells.get(id);
@@ -79,6 +104,9 @@ export const cellStore = createStore<CellState>((set, get) => ({
79104
getOutputsCount: (id: string): number | undefined => {
80105
return get().cells.get(id)?.outputsCount;
81106
},
107+
getIsKernelSessionAvailable: (id: string): boolean | undefined => {
108+
return get().cells.get(id)?.isKernelSessionAvailable;
109+
},
82110
execute: (id: string) => {
83111
const cells = get().cells;
84112
const cell = cells.get(id);

packages/react/src/examples/All.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ const CellPreview = (props: ICellToolProps) => {
5858
return (
5959
<>
6060
<>source: {cellStore.getSource(props.id)}</>
61-
<>kernel available: {String(cellStore.kernelAvailable)}</>
61+
<>kernel available: {String(cellStore.getIsKernelSessionAvailable(props.id))}</>
6262
</>
6363
);
6464
};

0 commit comments

Comments
 (0)