Skip to content

Commit

Permalink
attempt to fix canister initialization errors
Browse files Browse the repository at this point in the history
  • Loading branch information
lastmjs committed Jan 2, 2025
1 parent b41aa4b commit 825fe6f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
20 changes: 18 additions & 2 deletions src/build/stable/commands/compile/javascript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ function getPrelude(main: string): string {

export function handleClassApiCanister(): string {
return /*TS*/ `
const exportedCanisterClassInstance = new Canister.default();
globalThis._azleExportedCanisterClassInstance = exportedCanisterClassInstance;
const exportedCanisterClassInstance = getExportedCanisterClassInstance();
const canisterIdlType = IDL.Service(exportedCanisterClassInstance._azleCanisterMethodIdlTypes);
const candid = canisterIdlType.accept(new DidVisitor(), {
Expand All @@ -41,6 +40,23 @@ export function handleClassApiCanister(): string {
methodMeta: exportedCanisterClassInstance._azleMethodMeta
});
};
function getExportedCanisterClassInstance() {
try {
new Canister.default();
} catch (error) {
if (globalThis._azleNodeWasmEnvironment === true) {
if (globalThis._azleExportedCanisterClassInstance === undefined) {
throw error;
}
}
else {
throw error;
}
}
return globalThis._azleExportedCanisterClassInstance;
}
`;
}

Expand Down
2 changes: 2 additions & 0 deletions src/lib/stable/canister_methods/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,8 @@ function decoratorImplementation<
handleUncaughtError(error);
}
};

globalThis._azleExportedCanisterClassInstance = this;
});

return originalMethod;
Expand Down

0 comments on commit 825fe6f

Please sign in to comment.