-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
set up an event listener for outputs within useCell
- Loading branch information
Showing
4 changed files
with
92 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,82 @@ | ||
import { useState, useCallback } from "react"; | ||
import { useState, useEffect, useCallback, useReducer } from "react"; | ||
import { invoke } from "@tauri-apps/api/tauri"; | ||
import { listen } from '@tauri-apps/api/event' | ||
|
||
type CellStates = "idle" | "queued" | "busy" | "errored" | "submitted"; | ||
|
||
export function useCell(cellId: string) { | ||
const [content, setContent] = useState<string>(""); | ||
|
||
// We also find out the execution count, whether the cell is queued, busy, or | ||
// errored, and the output of the cell. We can use this to display the | ||
// execution count, a spinner, or an error message. | ||
|
||
// Execution count is only set by the backend. | ||
|
||
const executionCount = null; // TODO: get from backend | ||
enum ExecutionState { | ||
Idle = "idle", | ||
Queued = "queued", | ||
Busy = "busy", | ||
Errored = "errored", | ||
Submitted = "submitted" | ||
} | ||
|
||
// Queued, busy, and errored are set by the backend. However, we have to | ||
// have our own state for queued for before the backend has acknowledged the | ||
// execution request. | ||
type Action = { type: 'setSubmitted' } | { type: 'reset' } | {type: "setOutputs", outputs: []} | ||
|
||
const [executionSubmitted, setExecutionSubmitted] = useState<boolean>(false); | ||
type CellState = { | ||
executionState: ExecutionState; | ||
outputs: []; | ||
} | ||
|
||
let cellState: CellStates = "idle"; | ||
const initialState: CellState = { | ||
executionState: ExecutionState.Idle, | ||
outputs: [] | ||
}; | ||
|
||
if (executionSubmitted) { | ||
cellState = "submitted"; | ||
function cellReducer(state: CellState, action: Action) { | ||
switch (action.type) { | ||
case 'setSubmitted': | ||
return { ...state, executionState: ExecutionState.Submitted }; | ||
case 'reset': | ||
return { ...state, executionState: ExecutionState.Idle }; | ||
case 'setOutputs': | ||
return {...state, outputs: action.outputs } | ||
default: | ||
return state; | ||
} | ||
} | ||
|
||
export function useCell(cellId: string) { | ||
const [content, setContent] = useState<string>(""); | ||
const [state, dispatch] = useReducer(cellReducer, initialState); | ||
|
||
const executeCell = useCallback(async () => { | ||
setExecutionSubmitted(true); | ||
await invoke("execute_cell", { cellId }); | ||
setExecutionSubmitted(false); | ||
dispatch({ type: 'setSubmitted' }); | ||
try { | ||
await invoke("execute_cell", { cellId }); | ||
} catch (error) { | ||
console.error(error); | ||
// Handle error | ||
} | ||
dispatch({ type: 'reset' }); | ||
}, [cellId]); | ||
|
||
const updateCell = useCallback( | ||
async (newContent: string) => { | ||
const updateCell = useCallback(async (newContent: string) => { | ||
try { | ||
await invoke("update_cell", { cellId, newContent }); | ||
setContent(newContent); | ||
}, | ||
[cellId] | ||
); | ||
} catch (error) { | ||
console.error(error); | ||
// Handle error | ||
} | ||
}, [cellId]); | ||
|
||
useEffect(() => { | ||
const setupListener = async () => { | ||
const unlisten = await listen(`cell-outputs-${cellId}`, (event) => { | ||
// Q(Kyle): Do we need to check if the event.windowLabel matches ours | ||
const outputs = event.payload as []; | ||
|
||
// TODO(kyle): Listen for events from the backend to update the cell content | ||
dispatch({type: "setOutputs", outputs}) | ||
|
||
}); | ||
|
||
return () => { | ||
unlisten(); | ||
}; | ||
}; | ||
|
||
setupListener(); | ||
}, [cellId]); | ||
|
||
return { content, executeCell, updateCell, cellState: cellState as CellStates, executionCount }; | ||
return { ...state, content, executeCell, updateCell, executionCount: null}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters