diff options
author | Kevin (Kun) "Kassimo" Qian <kevinkassimo@gmail.com> | 2020-03-24 20:56:40 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-03-24 23:56:40 -0400 |
commit | 5d7bcf86fdb2bfcee0bfac24aeb7aeecb8b3faca (patch) | |
tree | 07e3f9b315253c27924687100a6e1891768696dc /cli/js/runtime_main.ts | |
parent | 3938071e91e7c3dcf7b4e727601e1e99005aceab (diff) |
feat: window.close() (#4474)
Diffstat (limited to 'cli/js/runtime_main.ts')
-rw-r--r-- | cli/js/runtime_main.ts | 27 |
1 files changed, 26 insertions, 1 deletions
diff --git a/cli/js/runtime_main.ts b/cli/js/runtime_main.ts index a59e7513b..4c506e0ce 100644 --- a/cli/js/runtime_main.ts +++ b/cli/js/runtime_main.ts @@ -10,8 +10,10 @@ import * as Deno from "./deno.ts"; import * as domTypes from "./web/dom_types.ts"; import * as csprng from "./ops/get_random_values.ts"; +import { exit } from "./ops/os.ts"; import { readOnly, + getterOnly, writable, windowOrWorkerGlobalScopeMethods, windowOrWorkerGlobalScopeProperties, @@ -21,6 +23,7 @@ import { internalObject } from "./internals.ts"; import { setSignals } from "./signals.ts"; import { replLoop } from "./repl.ts"; import { LocationImpl } from "./web/location.ts"; +import { setTimeout } from "./web/timers.ts"; import * as runtime from "./runtime.ts"; import { symbols } from "./symbols.ts"; import { log, immutableDefine } from "./util.ts"; @@ -31,6 +34,26 @@ import { log, immutableDefine } from "./util.ts"; // @ts-ignore Deno[symbols.internal] = internalObject; +let windowIsClosing = false; + +function windowClose(): void { + if (!windowIsClosing) { + windowIsClosing = true; + // Push a macrotask to exit after a promise resolve. + // This is not perfect, but should be fine for first pass. + Promise.resolve().then(() => + setTimeout.call( + null, + () => { + // This should be fine, since only Window/MainWorker has .close() + exit(0); + }, + 0 + ) + ); + } +} + export const mainRuntimeGlobalProperties = { window: readOnly(globalThis), self: readOnly(globalThis), @@ -38,7 +61,9 @@ export const mainRuntimeGlobalProperties = { // TODO(bartlomieju): from MDN docs (https://developer.mozilla.org/en-US/docs/Web/API/WorkerGlobalScope) // it seems those two properties should be availble to workers as well onload: writable(undefined), - onunload: writable(undefined) + onunload: writable(undefined), + close: writable(windowClose), + closed: getterOnly(() => windowIsClosing) }; let hasBootstrapped = false; |