onnxruntime/js/web/lib/wasm/binding/index.ts
Yulong Wang 4ebc9c3b5e
[JS] onnxruntime-web (#7394)
* add web

* add script and test

* fix lint

* add test/data/ops

* add test/data/node/ to gitignore

* modify scripts

* add onnxjs

* fix tests

* fix test-runner

* fix sourcemap

* fix onnxjs profiling

* update test list

* update README

* resolve comments

* set wasm as default backend

* rename package

* update copyright header

* do not use class "Buffer" in browser context

* revise readme
2021-04-27 00:04:25 -07:00

42 lines
1.1 KiB
TypeScript

// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import wasmModuleFactory, {BackendWasmModule} from './onnxruntime_wasm';
// some global parameters to deal with wasm binding initialization
let wasm: BackendWasmModule;
let initialized = false;
let initializing = false;
/**
* initialize the WASM instance.
*
* this function should be called before any other calls to the WASM binding.
*/
export const init = async(): Promise<void> => {
if (initialized) {
return Promise.resolve();
}
if (initializing) {
throw new Error('multiple calls to \'init()\' detected.');
}
initializing = true;
return new Promise<void>((resolve, reject) => {
wasmModuleFactory().then(
initializedModule => {
// resolve init() promise
wasm = initializedModule;
initializing = false;
initialized = true;
resolve();
},
err => {
initializing = false;
reject(err);
});
});
};
export const getInstance = (): BackendWasmModule => wasm;