Skip to content

Commit

Permalink
Error handling (#349)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
martinRenou and trungleduc committed Jun 4, 2024
1 parent 125294a commit 5703a01
Show file tree
Hide file tree
Showing 10 changed files with 308 additions and 121 deletions.
59 changes: 49 additions & 10 deletions packages/base/src/3dview/mainviewmodel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ import {
IPostOperatorInput,
IPostResult,
JCadWorkerSupportedFormat,
IDryRunResponsePayload,
MainAction,
WorkerAction
WorkerAction,
IJCadContent
} from '@jupytercad/schema';
import { ObservableMap } from '@jupyterlab/observables';
import { JSONValue } from '@lumino/coreutils';
import { JSONValue, PromiseDelegate, UUID } from '@lumino/coreutils';
import { IDisposable } from '@lumino/disposable';
import { ISignal, Signal } from '@lumino/signaling';
import { v4 as uuid } from 'uuid';
Expand Down Expand Up @@ -74,6 +76,7 @@ export class MainViewModel implements IDisposable {
this
);
}

initWorker(): void {
this._worker = this._workerRegistry.getDefaultWorker();
this._id = this._worker.register({
Expand All @@ -87,7 +90,7 @@ export class MainViewModel implements IDisposable {
});
}

messageHandler = (msg: IMainMessage): void => {
messageHandler(msg: IMainMessage): void {
switch (msg.action) {
case MainAction.DISPLAY_SHAPE: {
const { result, postResult } = msg.payload;
Expand Down Expand Up @@ -126,6 +129,10 @@ export class MainViewModel implements IDisposable {

break;
}
case MainAction.DRY_RUN_RESPONSE: {
this._dryRunResponses[msg.payload.id].resolve(msg.payload);
break;
}
case MainAction.INITIALIZED: {
if (!this._jcadModel) {
return;
Expand All @@ -140,7 +147,7 @@ export class MainViewModel implements IDisposable {
});
}
}
};
}

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

postProcessWorkerHandler = (msg: IMainMessage): void => {
/**
* Send a payload to the worker to test its feasibility.
*
* Return true is the payload is valid, false otherwise.
*/
async dryRun(content: IJCadContent): Promise<IDryRunResponsePayload> {
await this._worker.ready;

const id = UUID.uuid4();

this._dryRunResponses[id] = new PromiseDelegate();

this._workerBusy.emit(true);

this._postMessage({
action: WorkerAction.DRY_RUN,
payload: {
id,
content
}
});

const response = await this._dryRunResponses[id].promise;

delete this._dryRunResponses[id];

this._workerBusy.emit(false);

return response;
}

postProcessWorkerHandler(msg: IMainMessage): void {
switch (msg.action) {
case MainAction.DISPLAY_POST: {
const postShapes: IDict<IPostResult> = {};
Expand All @@ -180,27 +218,27 @@ export class MainViewModel implements IDisposable {
break;
}
}
};
}

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

private _postMessage = (msg: Omit<IWorkerMessage, 'id'>) => {
private _postMessage(msg: Omit<IWorkerMessage, 'id'>) {
if (this._worker) {
const newMsg = { ...msg, id: this._id };
this._worker.postMessage(newMsg);
}
};
}

private _saveMeta = (payload: IDisplayShape['payload']['result']) => {
private _saveMeta(payload: IDisplayShape['payload']['result']) {
if (!this._jcadModel) {
return;
}
Object.entries(payload).forEach(([objName, data]) => {
this._jcadModel.sharedModel.setShapeMeta(objName, data.meta);
});
};
}

private async _onSharedObjectsChanged(
_: IJupyterCadDoc,
Expand All @@ -219,6 +257,7 @@ export class MainViewModel implements IDisposable {
}
}

private _dryRunResponses: IDict<PromiseDelegate<IDryRunResponsePayload>> = {};
private _jcadModel: IJupyterCadModel;
private _viewSetting: ObservableMap<JSONValue>;
private _workerRegistry: IJCadWorkerRegistry;
Expand Down
Loading

0 comments on commit 5703a01

Please sign in to comment.