-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #191 from flatironinstitute/compile-model-button
Compile Model button and CompileContextProvider
- Loading branch information
Showing
13 changed files
with
402 additions
and
239 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
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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { createContext } from "react"; | ||
|
||
export type CompileStatus = | ||
| "preparing" | ||
| "compiling" | ||
| "compiled" | ||
| "failed" | ||
| ""; | ||
|
||
type CompileContextType = { | ||
compileStatus: CompileStatus; | ||
compileMessage: string; | ||
compiledMainJsUrl?: string; | ||
validSyntax: boolean; | ||
compile: () => void; | ||
setValidSyntax: (valid: boolean) => void; | ||
stanWasmServerUrl: string; | ||
setStanWasmServerUrl: (url: string) => void; | ||
}; | ||
|
||
export const CompileContext = createContext<CompileContextType>({ | ||
compileStatus: "", | ||
compileMessage: "", | ||
validSyntax: false, | ||
compile: () => {}, | ||
setValidSyntax: () => {}, | ||
stanWasmServerUrl: "", | ||
setStanWasmServerUrl: () => {}, | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,102 @@ | ||
import { ProjectContext } from "@SpCore/ProjectContextProvider"; | ||
import compileStanProgram from "@SpCompileContext/compileStanProgram"; | ||
import { | ||
FunctionComponent, | ||
PropsWithChildren, | ||
useCallback, | ||
useContext, | ||
useEffect, | ||
useState, | ||
} from "react"; | ||
import { CompileContext, CompileStatus } from "./CompileContext"; | ||
|
||
type CompileContextProviderProps = { | ||
// none | ||
}; | ||
|
||
const initialStanWasmServerUrl = | ||
localStorage.getItem("stanWasmServerUrl") || | ||
"https://trom-stan-wasm-server.magland.org"; | ||
|
||
export const CompileContextProvider: FunctionComponent< | ||
PropsWithChildren<CompileContextProviderProps> | ||
> = ({ children }) => { | ||
const { data } = useContext(ProjectContext); | ||
const [compileStatus, setCompileStatus] = useState<CompileStatus>(""); | ||
const [ | ||
theStanFileContentThasHasBeenCompiled, | ||
setTheStanFileContentThasHasBeenCompiled, | ||
] = useState<string>(""); | ||
const [compileMessage, setCompileMessage] = useState<string>(""); | ||
const [compiledMainJsUrl, setCompiledMainJsUrl] = useState< | ||
string | undefined | ||
>(undefined); | ||
const [validSyntax, setValidSyntax] = useState<boolean>(false); | ||
|
||
useEffect(() => { | ||
// if the compiled content is not the same as the current content, | ||
// then the state should not be compiled or failed | ||
if (data.stanFileContent !== theStanFileContentThasHasBeenCompiled) { | ||
if (compileStatus === "compiled" || compileStatus === "failed") { | ||
setCompileStatus(""); | ||
setCompiledMainJsUrl(""); | ||
} | ||
} | ||
}, [ | ||
data.stanFileContent, | ||
theStanFileContentThasHasBeenCompiled, | ||
compileStatus, | ||
setCompiledMainJsUrl, | ||
]); | ||
|
||
const [stanWasmServerUrl, setStanWasmServerUrl] = useState<string>( | ||
initialStanWasmServerUrl, | ||
); | ||
useEffect(() => { | ||
// persist to local storage | ||
localStorage.setItem("stanWasmServerUrl", stanWasmServerUrl); | ||
}, [stanWasmServerUrl]); | ||
|
||
const handleCompile = useCallback(async () => { | ||
setCompileStatus("compiling"); | ||
await new Promise((resolve) => setTimeout(resolve, 500)); // for effect | ||
const onStatus = (msg: string) => { | ||
setCompileMessage(msg); | ||
}; | ||
const { mainJsUrl } = await compileStanProgram( | ||
stanWasmServerUrl, | ||
data.stanFileContent, | ||
onStatus, | ||
); | ||
|
||
if (!mainJsUrl) { | ||
setCompileStatus("failed"); | ||
return; | ||
} | ||
setCompiledMainJsUrl(mainJsUrl); | ||
setCompileStatus("compiled"); | ||
setTheStanFileContentThasHasBeenCompiled(data.stanFileContent); | ||
}, [ | ||
data.stanFileContent, | ||
setCompiledMainJsUrl, | ||
setCompileStatus, | ||
stanWasmServerUrl, | ||
]); | ||
|
||
return ( | ||
<CompileContext.Provider | ||
value={{ | ||
compileStatus, | ||
compileMessage, | ||
compiledMainJsUrl, | ||
validSyntax, | ||
compile: handleCompile, | ||
setValidSyntax, | ||
stanWasmServerUrl, | ||
setStanWasmServerUrl, | ||
}} | ||
> | ||
{children} | ||
</CompileContext.Provider> | ||
); | ||
}; |
File renamed without changes.
File renamed without changes.
Oops, something went wrong.