Skip to content

Commit

Permalink
jupytercad-lab works
Browse files Browse the repository at this point in the history
  • Loading branch information
trungleduc committed Nov 15, 2023
1 parent 7364969 commit 3038ba3
Show file tree
Hide file tree
Showing 22 changed files with 173 additions and 161 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@
"lint": "jlpm run prettier && jlpm run eslint",
"test": "lerna run test",
"dev": "python scripts/dev-install.py",
"watch": "lerna run --stream watch"
"watch:lib": "lerna run --stream watch",
"watch": "lerna watch --scope @jupytercad/jupytercad-lab -- lerna run build --since",
"build:lab": "lerna run --ignore @jupytercad/jupytercad-app build"
},
"resolutions": {
"@jupyterlab/apputils": "~4.0.0",
Expand Down
1 change: 0 additions & 1 deletion packages/base/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
"@jupyter/collaboration": "^1.0.0",
"@jupyter/docprovider": "^1.0.0",
"@jupyter/ydoc": "^0.3.4 || ^1.0.2",
"@jupytercad/occ-worker": "^0.3.3",
"@jupytercad/opencascade": "^0.3.3",
"@jupytercad/schema": "^0.3.3",
"@jupyterlab/application": "^4.0.0",
Expand Down
1 change: 0 additions & 1 deletion packages/base/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
export * from './commands';
export * from './formdialog';
export * from './mainview';
export * from './token';
export * from './tools';
export * from './types';
export * from './widget';
Expand Down
38 changes: 13 additions & 25 deletions packages/base/src/mainview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import {
IAnnotation,
IDict,
IJcadObjectDocChange,
IJCadWorker,
IJCadWorkerRegistry,
IJupyterCadClientState,
IJupyterCadDoc,
IJupyterCadModel
Expand All @@ -34,7 +36,6 @@ import { v4 as uuid } from 'uuid';
import { FloatingAnnotation } from './annotation/view';
import { getCSSVariableColor, throttle } from './tools';
import { AxeHelper, CameraSettings, ExplodedView } from './types';
import { IJCadWorker, IJCadWorkerRegistry } from './token';

// Apply the BVH extension
THREE.BufferGeometry.prototype.computeBoundsTree = computeBoundsTree;
Expand Down Expand Up @@ -103,28 +104,23 @@ export class MainView extends React.Component<IProps, IStates> {
const lightTheme =
document.body.getAttribute('data-jp-theme-light') === 'true';

this._worker = props.workerRegistry?.getWorker(OCC_WORKER_ID);

const id = this._worker!.initChannel();
this._worker!.registerHandler(id, this.messageHandler.bind(this));

this.state = {
id: uuid(),
id,
lightTheme,
loading: true,
annotations: {},
firstLoad: true
};

this._model = this.props.jcadModel;
this._worker = props.workerRegistry?.getWorker(OCC_WORKER_ID);
console.log('worker is ', this._worker);

this._pointer = new THREE.Vector2();
this._collaboratorPointers = {};
this._messageChannel = new MessageChannel();
this._messageChannel.port1.onmessage = msgEvent => {
this.messageHandler(msgEvent.data);
};
this._postMessage(
{ action: WorkerAction.REGISTER, payload: { id: this.state.id } },
this._messageChannel.port2
);
this._model.themeChanged.connect(this._handleThemeChange, this);

this._model.sharedObjectsChanged.connect(
Expand Down Expand Up @@ -397,8 +393,6 @@ export class MainView extends React.Component<IProps, IStates> {
break;
}
case MainAction.INITIALIZED: {
console.log('im here11');

if (!this._model) {
return;
}
Expand Down Expand Up @@ -701,17 +695,10 @@ export class MainView extends React.Component<IProps, IStates> {
this.setState(old => ({ ...old, loading: false }));
};

private _postMessage = (
msg: Omit<IWorkerMessage, 'id'>,
port?: MessagePort
) => {
private _postMessage = (msg: Omit<IWorkerMessage, 'id'>) => {
if (this._worker) {
const newMsg = { ...msg, id: this.state.id };
if (port) {
this._worker.postMessage(newMsg, [port]);
} else {
this._worker.postMessage(newMsg);
}
this._worker.postMessage(newMsg);
}
};

Expand Down Expand Up @@ -921,11 +908,12 @@ export class MainView extends React.Component<IProps, IStates> {
});
};

private _onSharedObjectsChanged(
private async _onSharedObjectsChanged(
_: IJupyterCadDoc,
change: IJcadObjectDocChange
): void {
): Promise<void> {
if (change.objectChange) {
await this._worker!.ready;
this._postMessage({
action: WorkerAction.LOAD_FILE,
payload: {
Expand Down
7 changes: 5 additions & 2 deletions packages/base/src/panelview/leftpanel.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { IAnnotationModel, JupyterCadDoc } from '@jupytercad/schema';
import {
IAnnotationModel,
JupyterCadDoc,
IJupyterCadTracker
} from '@jupytercad/schema';
import { SidePanel } from '@jupyterlab/ui-components';

import { IJupyterCadTracker } from '../token';
import { IControlPanelModel } from '../types';
import { Annotations } from './annotations';
import { ControlPanelHeader } from './header';
Expand Down
4 changes: 2 additions & 2 deletions packages/base/src/panelview/model.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { IJupyterCadDoc, IJupyterCadModel } from '@jupytercad/schema';
import { ISignal } from '@lumino/signaling';

import { IJupyterCadTracker } from '../token';
import { IControlPanelModel, IJupyterCadWidget } from '../types';
import { IJupyterCadTracker, IJupyterCadWidget } from '@jupytercad/schema';
import { IControlPanelModel } from '../types';

export class ControlPanelModel implements IControlPanelModel {
constructor(options: ControlPanelModel.IOptions) {
Expand Down
60 changes: 0 additions & 60 deletions packages/base/src/token.ts

This file was deleted.

6 changes: 1 addition & 5 deletions packages/base/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { IDocumentWidget } from '@jupyterlab/docregistry';
import { ReactWidget } from '@jupyterlab/ui-components';
import { ISignal } from '@lumino/signaling';
import { IJupyterCadModel, IJupyterCadDoc, IDict } from '@jupytercad/schema';
import { IJupyterCadTracker } from './token';
import { IJupyterCadTracker, IJupyterCadWidget } from '@jupytercad/schema';

export { IDict };
export type ValueOf<T> = T[keyof T];
Expand Down Expand Up @@ -30,8 +28,6 @@ export type CameraSettings = {
type: 'Perspective' | 'Orthographic';
};

export type IJupyterCadWidget = IDocumentWidget<ReactWidget, IJupyterCadModel>;

export interface IControlPanelModel {
disconnect(f: any): void;
documentChanged: ISignal<IJupyterCadTracker, IJupyterCadWidget | null>;
Expand Down
9 changes: 2 additions & 7 deletions packages/base/src/widget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,8 @@ import { ISignal, Signal } from '@lumino/signaling';
import * as React from 'react';

import { MainView } from './mainview';
import {
AxeHelper,
CameraSettings,
ExplodedView,
IJupyterCadWidget
} from './types';
import { IJCadWorkerRegistry } from './token';
import { AxeHelper, CameraSettings, ExplodedView } from './types';
import { IJCadWorkerRegistry, IJupyterCadWidget } from '@jupytercad/schema';

export class JupyterCadWidget
extends DocumentWidget<JupyterCadPanel, IJupyterCadModel>
Expand Down
26 changes: 19 additions & 7 deletions packages/occ-worker/src/occworker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,38 @@ export class OccWorker implements IJCadWorker {
return this._ready.promise;
}

initialize(): string {
initChannel(): string {
const messageChannel = new MessageChannel();
messageChannel.port1.onmessage = this._processMessage.bind(this);
const id = uuid();
this._messageChannels.set(id, messageChannel);
const initMessage = {
id,
action: WorkerAction.REGISTER,
payload: { id }
};
this._nativeWorker.postMessage(initMessage, [messageChannel.port2]);
return id;
}
removeChannel(id: string): void {
this._handlers.delete(id);
this._messageChannels.delete(id);
}

registerHandler(
id: string,
messageHandler: ((msg: any) => void) | ((msg: any) => Promise<void>),
thisArg?: any
): void {
if (!this._handlerSet.has(messageHandler)) {
if (!this._handlers.has(id)) {
if (thisArg) {
messageHandler.bind(thisArg);
}
this._handlerSet.add(messageHandler);
this._handlers.set(id, messageHandler);
} else {
console.error(
`${id} is already registered, remove the handler first before re-registering `
);
}
}

Expand All @@ -44,16 +55,17 @@ export class OccWorker implements IJCadWorker {
}

private _processMessage(msg: any): void {
if (msg.action === MainAction.INITIALIZED) {
if (msg.data.action === MainAction.INITIALIZED) {
this._ready.resolve();
} else {
this._handlerSet.forEach(cb => cb(msg));
this._handlers.forEach(cb => cb(msg.data));
}
}

private _ready = new PromiseDelegate<void>();
private _handlerSet = new Set<CallableFunction>();
private _nativeWorker: Worker;
private _handlers = new Map<string, CallableFunction>();
private _messageChannels = new Map<string, MessageChannel>();
private _nativeWorker: Worker;
}

export namespace OccWorker {
Expand Down
1 change: 1 addition & 0 deletions packages/schema/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './interfaces';
export * from './model';
export * from './token';
51 changes: 50 additions & 1 deletion packages/schema/src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import {
StateChange,
YDocument
} from '@jupyter/ydoc';
import { IWidgetTracker } from '@jupyterlab/apputils';
import { IChangedArgs } from '@jupyterlab/coreutils';
import { DocumentRegistry } from '@jupyterlab/docregistry';
import { DocumentRegistry, IDocumentWidget } from '@jupyterlab/docregistry';
import { User } from '@jupyterlab/services';
import { ReactWidget } from '@jupyterlab/ui-components';
import { JSONObject } from '@lumino/coreutils';
import { ISignal, Signal } from '@lumino/signaling';

Expand Down Expand Up @@ -178,3 +180,50 @@ export interface IAnnotation {
contents: IAnnotationContent[];
parent: string;
}

export interface IJCadWorker {
ready: Promise<void>;
initChannel(): string;
registerHandler(
id: string,
messageHandler: ((msg: any) => void) | ((msg: any) => Promise<void>),
thisArg?: any
): void;
removeChannel(id: string): void;
postMessage(msg: any): void;
}
export interface IJCadWorkerRegistry {
/**
*
*
* @param {string} workerId
* @param {IJCadWorker} worker
*/
registerWorker(workerId: string, worker: IJCadWorker): void;

/**
*
*
* @param {string} workerId
*/
unregisterWorker(workerId: string): void;

/**
*
*
* @param {string} workerId
* @return {*} {(IJCadWorker | undefined)}
*/
getWorker(workerId: string): IJCadWorker | undefined;

/**
*
*
* @return {*} {IJCadWorker[]}
*/
getAllWorkers(): IJCadWorker[];
}

export type IJupyterCadWidget = IDocumentWidget<ReactWidget, IJupyterCadModel>;

export type IJupyterCadTracker = IWidgetTracker<IJupyterCadWidget>;
19 changes: 19 additions & 0 deletions packages/schema/src/token.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Token } from '@lumino/coreutils';

import {
IJCadWorkerRegistry,
IJupyterCadTracker,
IAnnotationModel
} from './interfaces';

export const IJupyterCadDocTracker = new Token<IJupyterCadTracker>(
'jupyterCadDocTracker'
);

export const IAnnotationToken = new Token<IAnnotationModel>(
'jupytercadAnnotationModel'
);

export const IJCadWorkerRegistryToken = new Token<IJCadWorkerRegistry>(
'jupytercadWorkerRegistry'
);
1 change: 1 addition & 0 deletions packages/schema/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ export * from './_interface/sphere';
export * from './_interface/torus';
export * from './interfaces';
export * from './model';
export * from './token';
Loading

0 comments on commit 3038ba3

Please sign in to comment.