Skip to content

Commit

Permalink
No public description
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 565755048
  • Loading branch information
schmidt-sebastian authored and copybara-github committed Sep 15, 2023
1 parent a933e32 commit 94477b1
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 21 deletions.
13 changes: 13 additions & 0 deletions mediapipe/web/graph_runner/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ mediapipe_ts_library(
":graph_runner_factory_api",
":listener_types",
":platform_utils",
":run_script_helper",
":wasm_module_private",
],
)
Expand Down Expand Up @@ -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 = [
Expand Down
22 changes: 1 addition & 21 deletions mediapipe/web/graph_runner/graph_runner.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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<void>((resolve) => {
script.addEventListener('load', () => {
resolve();
}, false);
script.addEventListener('error', () => {
resolve();
}, false);
document.body.appendChild(script);
});
}
}

/** {@override CreateMediaPipeLibApi} */
export const createMediaPipeLib: CreateMediaPipeLibApi = async<LibType>(
constructorFcn: WasmMediaPipeConstructor<LibType>,
Expand Down
28 changes: 28 additions & 0 deletions mediapipe/web/graph_runner/run_script_helper.ts.template
Original file line number Diff line number Diff line change
@@ -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<string|URL>): 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<void>((resolve, revoke) => {
script.addEventListener('load', () => {
resolve();
}, false);
script.addEventListener('error', e => {
revoke(e);
}, false);
document.body.appendChild(script);
});
}
}

0 comments on commit 94477b1

Please sign in to comment.