ruffle/web/packages/core/src/load-ruffle.ts

51 lines
1.3 KiB
TypeScript
Raw Normal View History

/**
* Conditional ruffle loader
*/
import init, { Ruffle } from "../pkg/ruffle_web";
import { setPolyfillsOnLoad } from "./js-polyfills";
/**
* Load ruffle from an automatically-detected location.
*
* This function returns a new instance of Ruffle and downloads it every time.
* You should not use it directly; this module will memoize the resource
* download.
2020-11-17 22:53:17 +00:00
*
* @returns A ruffle constructor that may be used to create new Ruffle
* instances.
*/
async function fetchRuffle(): Promise<typeof Ruffle> {
// Apply some pure JavaScript polyfills to prevent conflicts with external
// libraries, if needed.
setPolyfillsOnLoad();
// wasm files are set to be resource assets,
// so this import will resolve to the URL of the wasm file.
const ruffleWasm = await import(
/* webpackMode: "eager" */
"../pkg/ruffle_web_bg.wasm"
);
await init(ruffleWasm.default);
return Ruffle;
}
let lastLoaded: Promise<typeof Ruffle> | null = null;
/**
* Obtain an instance of `Ruffle`.
*
* This function returns a promise which yields `Ruffle` asynchronously.
2020-11-17 22:53:17 +00:00
*
* @returns A ruffle constructor that may be used to create new Ruffle
* instances.
*/
export function loadRuffle(): Promise<typeof Ruffle> {
2020-11-17 22:16:35 +00:00
if (lastLoaded == null) {
lastLoaded = fetchRuffle();
}
2020-11-17 22:16:35 +00:00
return lastLoaded;
}