Skip to content

Commit

Permalink
Mocked workerFactory only for webworker context (#83)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sakrafux authored May 17, 2024
1 parent 581ad6d commit cfd67f9
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 3 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@ report.xml

wf-glsp-server-node.js
wf-glsp-server-node.js.map
wf-glsp-server-webworker.js
wf-glsp-server-webworker.js.map
2 changes: 1 addition & 1 deletion examples/workflow-server-bundled/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"wf-glsp-server-node.js.map"
],
"scripts": {
"clean": "rimraf wf-glsp-server-node.js wf-glsp-server-node.js.map",
"clean": "rimraf wf-glsp-server-node.js wf-glsp-server-node.js.map wf-glsp-server-webworker.js wf-glsp-server-webworker.js.map",
"start": "node --enable-source-maps ./wf-glsp-server-node.js --port 5007",
"start:websocket": "node --enable-source-maps ./wf-glsp-server-node.js -w --port 8081",
"watch": "tsc -w"
Expand Down
6 changes: 5 additions & 1 deletion examples/workflow-server/src/browser/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ export async function launch(argv?: string[]): Promise<void> {
appContainer.load(createAppModule({ logLevel: LogLevel.info }));

const launcher = appContainer.resolve(WorkerServerLauncher);
const elkLayoutModule = configureELKLayoutModule({ algorithms: ['layered'], layoutConfigurator: WorkflowLayoutConfigurator });
const elkLayoutModule = configureELKLayoutModule({
algorithms: ['layered'],
layoutConfigurator: WorkflowLayoutConfigurator,
isWebWorker: true
});

const serverModule = new WorkflowServerModule().configureDiagramModule(
new WorkflowDiagramModule(() => WorkflowMockModelStorage),
Expand Down
11 changes: 10 additions & 1 deletion packages/layout-elk/src/di.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ export interface ElkModuleOptions {
* is bound.
*/
elementFilter?: Constructor<ElementFilter>;
/**
* A flag to indicate whether a WebWorker context is provided. If this option is set, a feature is mocked that would
* only be available in a node environment.
*/
isWebWorker?: boolean;
}

/**
Expand Down Expand Up @@ -82,7 +87,11 @@ export function configureELKLayoutModule(options: ElkModuleOptions): ContainerMo
const elkFactory: ElkFactory = () =>
new ElkConstructor({
algorithms: options.algorithms,
defaultLayoutOptions: options.defaultLayoutOptions
defaultLayoutOptions: options.defaultLayoutOptions,
// The node implementation relied on elkjs' `FakeWorker` to set the `workerFactory`.
// However, since the required file is dynamically loaded and not available in a web-worker context,
// it needs to be mocked manually.
workerFactory: options.isWebWorker ? () => ({ postMessage: () => {} }) as unknown as Worker : undefined
});

bind(ElkFactory).toConstantValue(elkFactory);
Expand Down

0 comments on commit cfd67f9

Please sign in to comment.