diff --git a/mediapipe/web/graph_runner/BUILD b/mediapipe/web/graph_runner/BUILD index 8f30297b..9d84713a 100644 --- a/mediapipe/web/graph_runner/BUILD +++ b/mediapipe/web/graph_runner/BUILD @@ -60,6 +60,7 @@ mediapipe_ts_library( ":graph_runner_factory_api", ":listener_types", ":platform_utils", + ":run_script_helper", ":wasm_module_private", ], ) @@ -98,6 +99,18 @@ mediapipe_ts_library( deps = [":platform_utils"], ) +genrule( + name = "run_script_genrule", + srcs = ["run_script_helper.ts.template"], + outs = ["run_script_helper.ts"], + cmd = "cp $< $@", +) + +mediapipe_ts_library( + name = "run_script_helper", + srcs = ["run_script_helper.ts"], +) + jasmine_node_test( name = "platform_utils_test", deps = [ diff --git a/mediapipe/web/graph_runner/graph_runner.ts b/mediapipe/web/graph_runner/graph_runner.ts index 3ef4d4aa..2ede0ae4 100644 --- a/mediapipe/web/graph_runner/graph_runner.ts +++ b/mediapipe/web/graph_runner/graph_runner.ts @@ -1,6 +1,6 @@ // Placeholder for internal dependency on assertTruthy -// Placeholder for internal dependency on jsloader import {isWebKit} from '../../web/graph_runner/platform_utils'; +import {runScript} from '../../web/graph_runner/run_script_helper'; // Placeholder for internal dependency on trusted resource url import {GraphRunnerApi, ImageSource} from './graph_runner_api'; @@ -717,26 +717,6 @@ export class GraphRunner implements GraphRunnerApi { } } -// Quick private helper to run the given script safely -async function runScript(scriptUrl: string|string) { - if (typeof importScripts === 'function') { - importScripts(scriptUrl.toString()); - } else { - const script = document.createElement('script'); - script.setAttribute('src', scriptUrl); - script.setAttribute('crossorigin', 'anonymous'); - return new Promise((resolve) => { - script.addEventListener('load', () => { - resolve(); - }, false); - script.addEventListener('error', () => { - resolve(); - }, false); - document.body.appendChild(script); - }); - } -} - /** {@override CreateMediaPipeLibApi} */ export const createMediaPipeLib: CreateMediaPipeLibApi = async( constructorFcn: WasmMediaPipeConstructor, diff --git a/mediapipe/web/graph_runner/run_script_helper.ts.template b/mediapipe/web/graph_runner/run_script_helper.ts.template new file mode 100644 index 00000000..b73d30f6 --- /dev/null +++ b/mediapipe/web/graph_runner/run_script_helper.ts.template @@ -0,0 +1,28 @@ +// Placeholder for internal dependency on jsloader +// Placeholder for internal dependency on trusted resource url + +/** + * Fetches each URL in urls, executes them one-by-one in the order they are + * passed, and then returns (or throws if something went amiss). + */ +declare function importScripts(...urls: Array): void; + +// Quick helper to run the given script safely +export async function runScript(scriptUrl: string) { + if (typeof importScripts === 'function') { + importScripts(scriptUrl.toString()); + } else { + const script = document.createElement('script'); + script.setAttribute('src', scriptUrl); + script.setAttribute('crossorigin', 'anonymous'); + return new Promise((resolve, revoke) => { + script.addEventListener('load', () => { + resolve(); + }, false); + script.addEventListener('error', e => { + revoke(e); + }, false); + document.body.appendChild(script); + }); + } +}