From fd56fa89f36016a816450cd0e8df5853c66d170c Mon Sep 17 00:00:00 2001 From: Yoshiya Hinosawa Date: Tue, 12 Jan 2021 19:32:58 +0900 Subject: [PATCH] fix(cli): dispatch unload on exit (#9088) --- cli/tests/078_unload_on_exit.ts | 4 ++++ cli/tests/078_unload_on_exit.ts.out | 1 + cli/tests/integration_tests.rs | 5 +++++ runtime/js/30_os.js | 3 +++ 4 files changed, 13 insertions(+) create mode 100644 cli/tests/078_unload_on_exit.ts create mode 100644 cli/tests/078_unload_on_exit.ts.out diff --git a/cli/tests/078_unload_on_exit.ts b/cli/tests/078_unload_on_exit.ts new file mode 100644 index 0000000000..e8288ef31a --- /dev/null +++ b/cli/tests/078_unload_on_exit.ts @@ -0,0 +1,4 @@ +window.onunload = () => { + console.log("onunload is called"); +}; +Deno.exit(0); diff --git a/cli/tests/078_unload_on_exit.ts.out b/cli/tests/078_unload_on_exit.ts.out new file mode 100644 index 0000000000..e213f9632d --- /dev/null +++ b/cli/tests/078_unload_on_exit.ts.out @@ -0,0 +1 @@ +[WILDCARD]onunload is called diff --git a/cli/tests/integration_tests.rs b/cli/tests/integration_tests.rs index feb3f7f048..40da5c627c 100644 --- a/cli/tests/integration_tests.rs +++ b/cli/tests/integration_tests.rs @@ -2646,6 +2646,11 @@ itest!(_077_fetch_empty { exit_code: 1, }); +itest!(_078_unload_on_exit { + args: "run 078_unload_on_exit.ts", + output: "078_unload_on_exit.ts.out", +}); + itest!(js_import_detect { args: "run --quiet --reload js_import_detect.ts", output: "js_import_detect.ts.out", diff --git a/runtime/js/30_os.js b/runtime/js/30_os.js index 74af821243..1d700b5616 100644 --- a/runtime/js/30_os.js +++ b/runtime/js/30_os.js @@ -24,6 +24,9 @@ } function exit(code = 0) { + // Invokes the `unload` hooks before exiting + // ref: https://github.com/denoland/deno/issues/3603 + window.dispatchEvent(new Event("unload")); core.jsonOpSync("op_exit", { code }); throw new Error("Code not reachable"); }