@@ -12,11 +12,13 @@ import {
12
12
IPostOperatorInput ,
13
13
IPostResult ,
14
14
JCadWorkerSupportedFormat ,
15
+ IDryRunResponsePayload ,
15
16
MainAction ,
16
- WorkerAction
17
+ WorkerAction ,
18
+ IJCadContent
17
19
} from '@jupytercad/schema' ;
18
20
import { ObservableMap } from '@jupyterlab/observables' ;
19
- import { JSONValue } from '@lumino/coreutils' ;
21
+ import { JSONValue , PromiseDelegate , UUID } from '@lumino/coreutils' ;
20
22
import { IDisposable } from '@lumino/disposable' ;
21
23
import { ISignal , Signal } from '@lumino/signaling' ;
22
24
import { v4 as uuid } from 'uuid' ;
@@ -74,6 +76,7 @@ export class MainViewModel implements IDisposable {
74
76
this
75
77
) ;
76
78
}
79
+
77
80
initWorker ( ) : void {
78
81
this . _worker = this . _workerRegistry . getDefaultWorker ( ) ;
79
82
this . _id = this . _worker . register ( {
@@ -87,7 +90,7 @@ export class MainViewModel implements IDisposable {
87
90
} ) ;
88
91
}
89
92
90
- messageHandler = ( msg : IMainMessage ) : void => {
93
+ messageHandler ( msg : IMainMessage ) : void {
91
94
switch ( msg . action ) {
92
95
case MainAction . DISPLAY_SHAPE : {
93
96
const { result, postResult } = msg . payload ;
@@ -126,6 +129,10 @@ export class MainViewModel implements IDisposable {
126
129
127
130
break ;
128
131
}
132
+ case MainAction . DRY_RUN_RESPONSE : {
133
+ this . _dryRunResponses [ msg . payload . id ] . resolve ( msg . payload ) ;
134
+ break ;
135
+ }
129
136
case MainAction . INITIALIZED : {
130
137
if ( ! this . _jcadModel ) {
131
138
return ;
@@ -140,7 +147,7 @@ export class MainViewModel implements IDisposable {
140
147
} ) ;
141
148
}
142
149
}
143
- } ;
150
+ }
144
151
145
152
sendRawGeometryToWorker ( postResult : IDict < IPostOperatorInput > ) : void {
146
153
Object . values ( postResult ) . forEach ( res => {
@@ -167,7 +174,38 @@ export class MainViewModel implements IDisposable {
167
174
} ) ;
168
175
}
169
176
170
- postProcessWorkerHandler = ( msg : IMainMessage ) : void => {
177
+ /**
178
+ * Send a payload to the worker to test its feasibility.
179
+ *
180
+ * Return true is the payload is valid, false otherwise.
181
+ */
182
+ async dryRun ( content : IJCadContent ) : Promise < IDryRunResponsePayload > {
183
+ await this . _worker . ready ;
184
+
185
+ const id = UUID . uuid4 ( ) ;
186
+
187
+ this . _dryRunResponses [ id ] = new PromiseDelegate ( ) ;
188
+
189
+ this . _workerBusy . emit ( true ) ;
190
+
191
+ this . _postMessage ( {
192
+ action : WorkerAction . DRY_RUN ,
193
+ payload : {
194
+ id,
195
+ content
196
+ }
197
+ } ) ;
198
+
199
+ const response = await this . _dryRunResponses [ id ] . promise ;
200
+
201
+ delete this . _dryRunResponses [ id ] ;
202
+
203
+ this . _workerBusy . emit ( false ) ;
204
+
205
+ return response ;
206
+ }
207
+
208
+ postProcessWorkerHandler ( msg : IMainMessage ) : void {
171
209
switch ( msg . action ) {
172
210
case MainAction . DISPLAY_POST : {
173
211
const postShapes : IDict < IPostResult > = { } ;
@@ -180,27 +218,27 @@ export class MainViewModel implements IDisposable {
180
218
break ;
181
219
}
182
220
}
183
- } ;
221
+ }
184
222
185
223
addAnnotation ( value : IAnnotation ) : void {
186
224
this . _jcadModel . annotationModel ?. addAnnotation ( uuid ( ) , value ) ;
187
225
}
188
226
189
- private _postMessage = ( msg : Omit < IWorkerMessage , 'id' > ) => {
227
+ private _postMessage ( msg : Omit < IWorkerMessage , 'id' > ) {
190
228
if ( this . _worker ) {
191
229
const newMsg = { ...msg , id : this . _id } ;
192
230
this . _worker . postMessage ( newMsg ) ;
193
231
}
194
- } ;
232
+ }
195
233
196
- private _saveMeta = ( payload : IDisplayShape [ 'payload' ] [ 'result' ] ) => {
234
+ private _saveMeta ( payload : IDisplayShape [ 'payload' ] [ 'result' ] ) {
197
235
if ( ! this . _jcadModel ) {
198
236
return ;
199
237
}
200
238
Object . entries ( payload ) . forEach ( ( [ objName , data ] ) => {
201
239
this . _jcadModel . sharedModel . setShapeMeta ( objName , data . meta ) ;
202
240
} ) ;
203
- } ;
241
+ }
204
242
205
243
private async _onSharedObjectsChanged (
206
244
_ : IJupyterCadDoc ,
@@ -219,6 +257,7 @@ export class MainViewModel implements IDisposable {
219
257
}
220
258
}
221
259
260
+ private _dryRunResponses : IDict < PromiseDelegate < IDryRunResponsePayload > > = { } ;
222
261
private _jcadModel : IJupyterCadModel ;
223
262
private _viewSetting : ObservableMap < JSONValue > ;
224
263
private _workerRegistry : IJCadWorkerRegistry ;
0 commit comments