2019-08-24 23:56:46 +00:00
|
|
|
/**
|
|
|
|
* Conditional ruffle loader
|
|
|
|
*/
|
|
|
|
|
2021-01-15 22:25:47 +00:00
|
|
|
import init, { Ruffle } from "../pkg/ruffle_web";
|
2020-12-30 19:41:34 +00:00
|
|
|
import { setPolyfillsOnLoad } from "./js-polyfills";
|
2020-12-20 15:06:50 +00:00
|
|
|
|
2019-08-24 23:56:46 +00:00
|
|
|
/**
|
|
|
|
* Load ruffle from an automatically-detected location.
|
2020-05-12 22:24:41 +00:00
|
|
|
*
|
2019-08-24 23:56:46 +00:00
|
|
|
* 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.
|
2019-08-24 23:56:46 +00:00
|
|
|
*/
|
2021-04-30 19:59:59 +00:00
|
|
|
async function fetchRuffle(): Promise<typeof Ruffle> {
|
2020-12-30 19:41:34 +00:00
|
|
|
// Apply some pure JavaScript polyfills to prevent conflicts with external
|
|
|
|
// libraries, if needed.
|
|
|
|
setPolyfillsOnLoad();
|
2020-12-20 15:06:50 +00:00
|
|
|
|
2021-04-21 19:00:49 +00:00
|
|
|
// wasm files are set to be resource assets,
|
|
|
|
// so this import will resolve to the URL of the wasm file.
|
2021-03-09 20:33:20 +00:00
|
|
|
const ruffleWasm = await import(
|
|
|
|
/* webpackMode: "eager" */
|
|
|
|
"../pkg/ruffle_web_bg.wasm"
|
|
|
|
);
|
|
|
|
await init(ruffleWasm.default);
|
2021-01-18 16:10:36 +00:00
|
|
|
|
2021-01-15 22:25:47 +00:00
|
|
|
return Ruffle;
|
2019-08-24 23:56:46 +00:00
|
|
|
}
|
|
|
|
|
2021-04-30 19:59:59 +00:00
|
|
|
let lastLoaded: Promise<typeof Ruffle> | null = null;
|
2019-08-24 23:56:46 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Obtain an instance of `Ruffle`.
|
2020-05-12 22:24:41 +00:00
|
|
|
*
|
2019-08-24 23:56:46 +00:00
|
|
|
* 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.
|
2019-08-24 23:56:46 +00:00
|
|
|
*/
|
2021-04-30 19:59:59 +00:00
|
|
|
export function loadRuffle(): Promise<typeof Ruffle> {
|
2020-11-17 22:16:35 +00:00
|
|
|
if (lastLoaded == null) {
|
2020-11-17 22:17:21 +00:00
|
|
|
lastLoaded = fetchRuffle();
|
2019-08-24 23:56:46 +00:00
|
|
|
}
|
|
|
|
|
2020-11-17 22:16:35 +00:00
|
|
|
return lastLoaded;
|
2020-11-12 22:32:53 +00:00
|
|
|
}
|