Skip to content
This repository was archived by the owner on Aug 31, 2023. It is now read-only.

Commit

Permalink
fix(npm/js-api): Lazy load backend implementations
Browse files Browse the repository at this point in the history
The wasm and backendjsonrpc dependencies are both marked as optional but the implementation loads packages statically, making the `js-api` fail immediately if one of the two isn't installed.

This PR moves the imports into lazy async calls.
  • Loading branch information
MichaReiser committed Nov 10, 2022
1 parent b96f521 commit 1d96641
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 11 deletions.
2 changes: 1 addition & 1 deletion npm/js-api/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@rometools/js-api",
"version": "0.1.2",
"version": "0.1.3",
"description": "JavaScript APIs for the Rome package",
"scripts": {
"tsc": "tsc --noEmit",
Expand Down
10 changes: 5 additions & 5 deletions npm/js-api/src/daemon.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import {
createWorkspace,
createWorkspaceWithBinary,
Workspace,
} from "@rometools/backend-jsonrpc";
import type { Workspace } from "@rometools/backend-jsonrpc";

/**
* Class responsible to communicate with the Rome daemon.
Expand All @@ -20,6 +16,10 @@ export class Deamon {
* It creates a new instance of a workspace connected to the Daemon
*/
public static async connectToDaemon(pathToBinary?: string): Promise<Deamon> {
const { createWorkspace, createWorkspaceWithBinary } = await import(
"@rometools/backend-jsonrpc",
);

if (pathToBinary) {
let workspace = await createWorkspaceWithBinary(pathToBinary);
if (workspace) {
Expand Down
9 changes: 4 additions & 5 deletions npm/js-api/src/nodeWasm.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { main, Workspace } from "@rometools/wasm-nodejs";
import type { Workspace } from "@rometools/wasm-nodejs";

/**
* Class responsible to connect with the WebAssembly backend of Rome.
Expand All @@ -13,13 +13,12 @@ export class NodeWasm {
* It creates a new instance of a workspace connected to the WebAssembly backend
*/
public static async loadWebAssembly(): Promise<NodeWasm> {
return new NodeWasm(await NodeWasm.loadWorkspace());
}
const { main, Workspace } = await import("@rometools/wasm-nodejs");

private static async loadWorkspace(): Promise<Workspace> {
// load the web assembly module
main();
return Promise.resolve(new Workspace());

return new NodeWasm(new Workspace());
}
}

Expand Down

0 comments on commit 1d96641

Please sign in to comment.