Skip to content

Commit

Permalink
cache loadNative
Browse files Browse the repository at this point in the history
  • Loading branch information
KSDaemon committed Dec 17, 2024
1 parent 7e56fb4 commit d09fe9e
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions packages/cubejs-backend-native/js/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,25 +118,29 @@ export type TransformDataResponseNative = {
result: string
};

let loadedNative: any = null;

export function loadNative() {
if (loadedNative) {
return loadedNative;
}

// Development version
if (fs.existsSync(path.join(__dirname, '/../../index.node'))) {
return require(path.join(__dirname, '/../../index.node'));
loadedNative = require(path.join(__dirname, '/../../index.node'));
return loadedNative;
}

if (fs.existsSync(path.join(__dirname, '/../../native/index.node'))) {
return require(path.join(__dirname, '/../../native/index.node'));
loadedNative = require(path.join(__dirname, '/../../native/index.node'));
return loadedNative;
}

throw new Error(
`Unable to load @cubejs-backend/native, probably your system (${process.arch}-${process.platform}) with Node.js ${process.version} is not supported.`,
);
}

export function isSupported(): boolean {
return fs.existsSync(path.join(__dirname, '/../../index.node')) || fs.existsSync(path.join(__dirname, '/../../native/index.node'));
}

function wrapNativeFunctionWithChannelCallback(
fn: (extra: any) => unknown | Promise<unknown>,
) {
Expand Down

0 comments on commit d09fe9e

Please sign in to comment.