Skip to content

Commit

Permalink
feat(getloader): refactor getting loader into closure
Browse files Browse the repository at this point in the history
  • Loading branch information
kwonoj committed Sep 19, 2017
1 parent 36b0f10 commit e0155a0
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/getLoader.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { ENVIRONMENT, getModuleLoader, isNode } from 'emscripten-wasm-loader';
import { CldAsmModule } from './cldAsmModule';
import { CldFactory } from './cldFactory';
import { cldLoader } from './cldLoader';
import { log } from './util/logger';

/**
* @internal
* Creates loadModule function.
*
* @param {binaryPath} string Path to import wasm / asm.js binary via `require`
* @param {binaryEndpoint} [string] For overring path to wasm binary on node.js or browser.
* @param {environment} [ENVIRONMENT] For overriding running environment
*
* @returns {(binaryEndpoint?: string, environment?: ENVIRONMENT) => Promise<CldFactory>} Function to load module
*/
const getLoader = async (binaryPath: string, binaryEndpoint?: string, environment?: ENVIRONMENT) => {
log(`loadModule: load cld3 module loader from `, binaryPath);

//imports MODULARIZED emscripten preamble
//tslint:disable-next-line:no-require-imports no-var-requires
const runtimeModule = require(`${binaryPath}/cld3`);
//do not supply this into moduleLoader, emscripten-wasm-loader does its own job to get environment
const env = environment ? environment : isNode() ? ENVIRONMENT.NODE : ENVIRONMENT.WEB;

const moduleLoader = await getModuleLoader<CldFactory, CldAsmModule>(
(runtime: CldAsmModule, env: ENVIRONMENT) => cldLoader(runtime, env),
{
//tslint:disable-next-line:no-require-imports
dir: env === ENVIRONMENT.NODE ? require('path').dirname(require.resolve(`${binaryPath}/cld3`)) : null,
runtimeModule
}
);

return moduleLoader(binaryEndpoint, environment);
};

export { getLoader };

0 comments on commit e0155a0

Please sign in to comment.