-
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 within useCell
- Loading branch information
Showing
3 changed files
with
71 additions
and
38 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,85 @@ | ||
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' }; | ||
|
||
const [executionSubmitted, setExecutionSubmitted] = useState<boolean>(false); | ||
type CellState = { | ||
executionState: ExecutionState; | ||
} | ||
|
||
let cellState: CellStates = "idle"; | ||
const initialState: CellState = { | ||
executionState: ExecutionState.Idle | ||
}; | ||
|
||
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 }; | ||
// Handle other actions here | ||
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(() => { | ||
let isMounted = true; | ||
|
||
// TODO(kyle): Listen for events from the backend to update the cell content | ||
const setupListener = async () => { | ||
const unlisten = await listen(`cell-${cellId}`, (event) => { | ||
if (isMounted) { | ||
console.log(event); | ||
// Update state based on event here | ||
} | ||
}); | ||
|
||
return () => { | ||
isMounted = false; | ||
unlisten(); | ||
}; | ||
}; | ||
|
||
setupListener(); | ||
|
||
return () => { | ||
isMounted = false; | ||
}; | ||
}, [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