Skip to content

Commit 617dccc

Browse files
author
Marcos Alves
committed
feat: improve event and add flag to allCellsReady (#273)
1 parent 9e887d3 commit 617dccc

File tree

3 files changed

+35
-17
lines changed

3 files changed

+35
-17
lines changed

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

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -59,26 +59,29 @@ export const Cell = (props: ICellProps) => {
5959
}
6060

6161
adapter.sessionContext.initialize().then(() => {
62-
if (!adapter.cell.model) {
62+
if (!autoStart || !adapter.cell.model) {
6363
return;
6464
}
65-
// Set that session/kernel is ready for that cell
66-
cellStore.setKernelAvailable(id, true);
6765

6866
// Perform auto-start for code or markdown cells
69-
if (autoStart) {
70-
if (adapter.cell instanceof CodeCell) {
71-
CodeCell.execute(
72-
adapter.cell,
73-
adapter.sessionContext
74-
);
75-
}
67+
if (adapter.cell instanceof CodeCell) {
68+
CodeCell.execute(
69+
adapter.cell,
70+
adapter.sessionContext
71+
);
72+
}
7673

77-
if (adapter.cell instanceof MarkdownCell) {
78-
adapter.cell.rendered = true;
79-
}
74+
if (adapter.cell instanceof MarkdownCell) {
75+
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.setKernelAvailable(id, true);
83+
})
84+
});
8285
}
8386

8487
useEffect(() => {

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

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export interface ICellState {
1717

1818
export interface ICellsState {
1919
cells: Map<string, ICellState>;
20+
areAllCellsReady: boolean;
2021
}
2122

2223
export type CellState = ICellsState & {
@@ -28,15 +29,28 @@ export type CellState = ICellsState & {
2829
getAdapter: (id: string) => CellAdapter | undefined;
2930
getSource: (id: string) => string | undefined;
3031
getOutputsCount: (id: string) => number | undefined;
31-
getIsKernelAvaiable: (id: string) => boolean | undefined;
32+
getIsKernelAvailable: (id: string) => boolean | undefined;
3233
execute: (id?: string) => void;
3334
};
3435

36+
/**
37+
* Iterate over all cells map and check if all cells/sessions are ready
38+
*/
39+
const areAllSessionsAvailable = (cells: Map<string, ICellState>): boolean => {
40+
for (const cell of cells.values()) {
41+
if (!cell.kernelAvailable) {
42+
return false;
43+
}
44+
}
45+
return true;
46+
};
47+
3548
export const cellStore = createStore<CellState>((set, get) => ({
3649
cells: new Map<string, ICellState>(),
3750
source: '',
3851
outputsCount: 0,
3952
kernelAvailable: false,
53+
areAllCellsReady: false,
4054
adapter: undefined,
4155
setCells: (cells: Map<string, ICellState>) => set((cell: CellState) => ({ cells })),
4256

@@ -68,7 +82,8 @@ export const cellStore = createStore<CellState>((set, get) => ({
6882
} else {
6983
cells.set(id, {kernelAvailable});
7084
}
71-
set((cell: CellState) => ({ cells }))
85+
const areAllCellsReady = areAllSessionsAvailable(cells);
86+
set((cell: CellState) => ({ cells, areAllCellsReady }));
7287
},
7388
setAdapter: (id: string, adapter?: CellAdapter) => {
7489
const cells = get().cells;
@@ -89,7 +104,7 @@ export const cellStore = createStore<CellState>((set, get) => ({
89104
getOutputsCount: (id: string): number | undefined => {
90105
return get().cells.get(id)?.outputsCount;
91106
},
92-
getIsKernelAvaiable: (id: string): boolean | undefined => {
107+
getIsKernelAvailable: (id: string): boolean | undefined => {
93108
return get().cells.get(id)?.kernelAvailable;
94109
},
95110
execute: (id: string) => {

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.getIsKernelAvaiable(props.id))}</>
61+
<>kernel available: {String(cellStore.getIsKernelAvailable(props.id))}</>
6262
</>
6363
);
6464
};

0 commit comments

Comments
 (0)