Skip to content

Commit 5703a01

Browse files
Error handling (#349)
* Draft: Error handling * Iterate * Iterate + linting * Iterate * Iterate * Iterate * Handle multiple parallel promise delegates * Update error message * Achieve a dry run upon object properties update * Apply suggestions from code review Co-authored-by: Duc Trung Le <[email protected]> --------- Co-authored-by: Duc Trung Le <[email protected]>
1 parent 125294a commit 5703a01

File tree

10 files changed

+308
-121
lines changed

10 files changed

+308
-121
lines changed

packages/base/src/3dview/mainviewmodel.ts

Lines changed: 49 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@ import {
1212
IPostOperatorInput,
1313
IPostResult,
1414
JCadWorkerSupportedFormat,
15+
IDryRunResponsePayload,
1516
MainAction,
16-
WorkerAction
17+
WorkerAction,
18+
IJCadContent
1719
} from '@jupytercad/schema';
1820
import { ObservableMap } from '@jupyterlab/observables';
19-
import { JSONValue } from '@lumino/coreutils';
21+
import { JSONValue, PromiseDelegate, UUID } from '@lumino/coreutils';
2022
import { IDisposable } from '@lumino/disposable';
2123
import { ISignal, Signal } from '@lumino/signaling';
2224
import { v4 as uuid } from 'uuid';
@@ -74,6 +76,7 @@ export class MainViewModel implements IDisposable {
7476
this
7577
);
7678
}
79+
7780
initWorker(): void {
7881
this._worker = this._workerRegistry.getDefaultWorker();
7982
this._id = this._worker.register({
@@ -87,7 +90,7 @@ export class MainViewModel implements IDisposable {
8790
});
8891
}
8992

90-
messageHandler = (msg: IMainMessage): void => {
93+
messageHandler(msg: IMainMessage): void {
9194
switch (msg.action) {
9295
case MainAction.DISPLAY_SHAPE: {
9396
const { result, postResult } = msg.payload;
@@ -126,6 +129,10 @@ export class MainViewModel implements IDisposable {
126129

127130
break;
128131
}
132+
case MainAction.DRY_RUN_RESPONSE: {
133+
this._dryRunResponses[msg.payload.id].resolve(msg.payload);
134+
break;
135+
}
129136
case MainAction.INITIALIZED: {
130137
if (!this._jcadModel) {
131138
return;
@@ -140,7 +147,7 @@ export class MainViewModel implements IDisposable {
140147
});
141148
}
142149
}
143-
};
150+
}
144151

145152
sendRawGeometryToWorker(postResult: IDict<IPostOperatorInput>): void {
146153
Object.values(postResult).forEach(res => {
@@ -167,7 +174,38 @@ export class MainViewModel implements IDisposable {
167174
});
168175
}
169176

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 {
171209
switch (msg.action) {
172210
case MainAction.DISPLAY_POST: {
173211
const postShapes: IDict<IPostResult> = {};
@@ -180,27 +218,27 @@ export class MainViewModel implements IDisposable {
180218
break;
181219
}
182220
}
183-
};
221+
}
184222

185223
addAnnotation(value: IAnnotation): void {
186224
this._jcadModel.annotationModel?.addAnnotation(uuid(), value);
187225
}
188226

189-
private _postMessage = (msg: Omit<IWorkerMessage, 'id'>) => {
227+
private _postMessage(msg: Omit<IWorkerMessage, 'id'>) {
190228
if (this._worker) {
191229
const newMsg = { ...msg, id: this._id };
192230
this._worker.postMessage(newMsg);
193231
}
194-
};
232+
}
195233

196-
private _saveMeta = (payload: IDisplayShape['payload']['result']) => {
234+
private _saveMeta(payload: IDisplayShape['payload']['result']) {
197235
if (!this._jcadModel) {
198236
return;
199237
}
200238
Object.entries(payload).forEach(([objName, data]) => {
201239
this._jcadModel.sharedModel.setShapeMeta(objName, data.meta);
202240
});
203-
};
241+
}
204242

205243
private async _onSharedObjectsChanged(
206244
_: IJupyterCadDoc,
@@ -219,6 +257,7 @@ export class MainViewModel implements IDisposable {
219257
}
220258
}
221259

260+
private _dryRunResponses: IDict<PromiseDelegate<IDryRunResponsePayload>> = {};
222261
private _jcadModel: IJupyterCadModel;
223262
private _viewSetting: ObservableMap<JSONValue>;
224263
private _workerRegistry: IJCadWorkerRegistry;

0 commit comments

Comments
 (0)