@@ -13,11 +13,13 @@ import { bracket } from '@src/lib/exampleKcl'
13
13
import makeUrlPathRelative from '@src/lib/makeUrlPathRelative'
14
14
import { PATHS } from '@src/lib/paths'
15
15
import { codeManager , editorManager , kclManager } from '@src/lib/singletons'
16
- import { reportRejection } from '@src/lib/trap'
17
- import { toSync } from '@src/lib/utils'
16
+ import { reportRejection , trap } from '@src/lib/trap'
18
17
import { settingsActor } from '@src/machines/appMachine'
19
18
import { onboardingRoutes } from '@src/routes/Onboarding'
20
19
import { onboardingPaths } from '@src/routes/Onboarding/paths'
20
+ import { parse , resultIsOk } from '@src/lang/wasm'
21
+ import { updateModelingState } from '@src/lang/modelingWorkflows'
22
+ import { EXECUTION_TYPE_REAL } from '@src/lib/constants'
21
23
22
24
export const kbdClasses =
23
25
'py-0.5 px-1 text-sm rounded bg-chalkboard-10 dark:bg-chalkboard-100 border border-chalkboard-50 border-b-2'
@@ -39,26 +41,33 @@ export function useDemoCode() {
39
41
const { overallState, immediateState } = useNetworkContext ( )
40
42
41
43
useEffect ( ( ) => {
42
- // Don't run if the editor isn't loaded or the code is already the bracket
43
- if ( ! editorManager . editorView || codeManager . code === bracket ) {
44
- return
44
+ async function setCodeToDemoIfNeeded ( ) {
45
+ // Don't run if the editor isn't loaded or the code is already the bracket
46
+ if ( ! editorManager . editorView || codeManager . code === bracket ) {
47
+ return
48
+ }
49
+ // Don't run if the network isn't healthy or the connection isn't established
50
+ if (
51
+ overallState === NetworkHealthState . Disconnected ||
52
+ overallState === NetworkHealthState . Issue ||
53
+ immediateState . type !== EngineConnectionStateType . ConnectionEstablished
54
+ ) {
55
+ return
56
+ }
57
+ const pResult = parse ( bracket )
58
+ if ( trap ( pResult ) || ! resultIsOk ( pResult ) ) {
59
+ return Promise . reject ( pResult )
60
+ }
61
+ const ast = pResult . program
62
+ await updateModelingState ( ast , EXECUTION_TYPE_REAL , {
63
+ kclManager : kclManager ,
64
+ editorManager : editorManager ,
65
+ codeManager : codeManager ,
66
+ } )
45
67
}
46
- // Don't run if the network isn't healthy or the connection isn't established
47
- if (
48
- overallState === NetworkHealthState . Disconnected ||
49
- overallState === NetworkHealthState . Issue ||
50
- immediateState . type !== EngineConnectionStateType . ConnectionEstablished
51
- ) {
52
- return
53
- }
54
- setTimeout (
55
- toSync ( async ( ) => {
56
- codeManager . updateCodeStateEditor ( bracket )
57
- await kclManager . executeCode ( )
58
- await codeManager . writeToFile ( )
59
- } , reportRejection )
60
- )
61
- } , [ editorManager . editorView , immediateState , overallState ] )
68
+
69
+ setCodeToDemoIfNeeded ( ) . catch ( reportRejection )
70
+ } , [ editorManager . editorView , immediateState . type , overallState ] )
62
71
}
63
72
64
73
export function useNextClick ( newStatus : string ) {
0 commit comments