@@ -6,10 +6,14 @@ import {
66 CurrentScripts ,
77 EvaluationViewerSettings ,
88 IDESupportedProgramState ,
9+ IDESupportedVM ,
910} from '../state/types' ;
1011import { unknownValue } from '../utils' ;
1112
1213import { getCurrentScripts , getUsedIds } from './common' ;
14+ import { DebugWorkerResult } from './debug-worker' ;
15+ // eslint-disable-next-line import/no-unresolved
16+ import DebugWorker from './debug-worker.ts?worker' ;
1317import { ImportExportDialog } from './dialogs/import-export-dialog/ImportExportDialog' ;
1418import { ImportScriptDialog } from './dialogs/import-script-dialog/ImportScriptDialog' ;
1519import { NewEntityDialog } from './dialogs/new-entity-dialog/NewEntityDialog' ;
@@ -33,7 +37,8 @@ import { WalletEditor } from './wallet/wallet-editor/WalletEditor';
3337import { WalletHistoryExplorer } from './wallet/wallet-history-explorer/WalletHistoryExplorer' ;
3438import { WelcomePane } from './welcome-pane/WelcomePane' ;
3539
36- import { useCallback , useEffect , useState } from 'react' ;
40+ import { stringify } from '@bitauth/libauth' ;
41+ import { useCallback , useEffect , useRef , useState } from 'react' ;
3742import { Mosaic } from 'react-mosaic-component' ;
3843import { connect } from 'react-redux' ;
3944
@@ -60,13 +65,17 @@ type EditorDispatch = {
6065 changeEvaluationViewerSettings : typeof ActionCreators . changeEvaluationViewerSettings ;
6166 importExport : typeof ActionCreators . importExport ;
6267 switchScenario : typeof ActionCreators . switchScenario ;
68+ startDebugging : typeof ActionCreators . startDebugging ;
69+ finishDebugging : typeof ActionCreators . finishDebugging ;
6370} ;
6471
6572type EditorProps < ProgramState extends IDESupportedProgramState > = {
6673 computed : ComputedEditorState < ProgramState > ;
6774 currentlyEditingInternalId : string | undefined ;
6875 currentScripts : CurrentScripts ;
6976 activeDialog : ActiveDialog ;
77+ currentVmId : IDESupportedVM ;
78+ debug : AppState [ 'debug' ] ;
7079 evaluationViewerSettings : EvaluationViewerSettings ;
7180 usedIds : string [ ] ;
7281} & EditorDispatch ;
@@ -78,6 +87,8 @@ export const Editor = connect(
7887 currentScripts : getCurrentScripts ( state ) ,
7988 activeDialog : state . activeDialog ,
8089 evaluationViewerSettings : state . evaluationViewerSettings ,
90+ currentVmId : state . currentVmId ,
91+ debug : state . debug ,
8192 usedIds : getUsedIds ( state ) ,
8293 } ) ,
8394 {
@@ -92,6 +103,8 @@ export const Editor = connect(
92103 ActionCreators . changeEvaluationViewerSettings ,
93104 importExport : ActionCreators . importExport ,
94105 switchScenario : ActionCreators . switchScenario ,
106+ startDebugging : ActionCreators . startDebugging ,
107+ finishDebugging : ActionCreators . finishDebugging ,
95108 } ,
96109) ( ( props : EditorProps < IDESupportedProgramState > ) => {
97110 const [ projectExplorerWidth , setProjectExplorerWidth ] = useState ( 21 ) ;
@@ -151,6 +164,53 @@ export const Editor = connect(
151164 viewerRefCallbackFrame2 ,
152165 viewerRefCallbackFrame3 ,
153166 ] ;
167+ const workerRef = useRef < Worker > ( ) ;
168+ const lastKey = useRef < string > ( ) ;
169+
170+ const { finishDebugging } = props ;
171+ useEffect ( ( ) => {
172+ workerRef . current = new DebugWorker ( ) ;
173+ workerRef . current . onmessage = ( e : MessageEvent < DebugWorkerResult > ) => {
174+ finishDebugging ( e . data ) ;
175+ } ;
176+ return ( ) => {
177+ workerRef . current ?. terminate ( ) ;
178+ } ;
179+ } , [ finishDebugging ] ) ;
180+
181+ const { computed, currentVmId, startDebugging, debug : debugState } = props ;
182+ useEffect ( ( ) => {
183+ if (
184+ computed . editorMode === ProjectEditorMode . isolatedScriptEditor ||
185+ computed . editorMode === ProjectEditorMode . scriptPairEditor ||
186+ computed . editorMode === ProjectEditorMode . testedScriptEditor
187+ ) {
188+ const scriptMode = computed ;
189+ const key = stringify (
190+ {
191+ vmId : currentVmId ,
192+ config : scriptMode . workerDetails . compilerConfiguration ,
193+ lockingScriptId : scriptMode . workerDetails . lockingScriptId ,
194+ unlockingScriptId : scriptMode . workerDetails . unlockingScriptId ,
195+ scenarioId : scriptMode . workerDetails . scenarioId ,
196+ } ,
197+ 0 ,
198+ ) ;
199+ if ( key !== lastKey . current ) {
200+ lastKey . current = key ;
201+ const compilationId = debugState . compilationId + 1 ;
202+ startDebugging ( ) ;
203+ workerRef . current ?. postMessage ( {
204+ compilerConfiguration : scriptMode . workerDetails . compilerConfiguration ,
205+ lockingScriptId : scriptMode . workerDetails . lockingScriptId ,
206+ unlockingScriptId : scriptMode . workerDetails . unlockingScriptId ,
207+ scenarioId : scriptMode . workerDetails . scenarioId ,
208+ vmId : currentVmId ,
209+ compilationId,
210+ } ) ;
211+ }
212+ }
213+ } , [ computed , currentVmId , startDebugging , debugState . compilationId ] ) ;
154214
155215 useEffect ( ( ) => {
156216 const setKey = ( value : unknown ) => {
@@ -217,6 +277,7 @@ export const Editor = connect(
217277 debugTrace = { computed . debugTrace }
218278 evaluationViewerSettings = { props . evaluationViewerSettings }
219279 importExport = { props . importExport }
280+ isProcessing = { computed . isProcessing }
220281 scenarioDetails = { computed . scenarioDetails }
221282 showControls = { indexFromTop === 0 }
222283 switchScenario = { props . switchScenario }
0 commit comments