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

fix(npm/js-api): Lazy load backend implementations #3652

Merged
merged 2 commits into from
Nov 10, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fix(npm/js-api): Lazy load backend implementations
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.
MichaReiser committed Nov 10, 2022
commit 1d96641a90d87757d6f8f16067360e96631d74a7
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",
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.
@@ -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) {
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.
@@ -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());
}
}