Skip to content

Commit

Permalink
feat: window.close() (denoland#4474)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinkassimo committed Mar 25, 2020
1 parent 3938071 commit 5d7bcf8
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 1 deletion.
8 changes: 8 additions & 0 deletions cli/js/globals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,14 @@ export function readOnly(value: unknown): PropertyDescriptor {
};
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function getterOnly(getter: () => any): PropertyDescriptor {
return {
get: getter,
enumerable: true
};
}

// https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope
export const windowOrWorkerGlobalScopeMethods = {
atob: writable(textEncoding.atob),
Expand Down
2 changes: 2 additions & 0 deletions cli/js/lib.deno.window.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ declare interface Window extends WindowOrWorkerGlobalScope {
onload: Function | undefined;
onunload: Function | undefined;
crypto: Crypto;
close: () => void;
closed: boolean;
Deno: typeof Deno;
}

Expand Down
27 changes: 26 additions & 1 deletion cli/js/runtime_main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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";
Expand All @@ -31,14 +34,36 @@ 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),
crypto: readOnly(csprng),
// 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;
Expand Down
18 changes: 18 additions & 0 deletions cli/tests/058_tasks_microtasks_close.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
console.log("sync 1");
setTimeout(() => {
console.log("setTimeout 1");
Promise.resolve().then(() => {
console.log("Promise resolve in setTimeout 1");
});
});
Promise.resolve().then(() => {
console.log("promise 1");
});
window.close();
console.log("sync 2");
setTimeout(() => {
console.log("setTimeout 2");
});
setTimeout(() => {
console.log("setTimeout 3");
}, 100);
6 changes: 6 additions & 0 deletions cli/tests/058_tasks_microtasks_close.ts.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
sync 1
sync 2
promise 1
setTimeout 1
Promise resolve in setTimeout 1
setTimeout 2
5 changes: 5 additions & 0 deletions cli/tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1078,6 +1078,11 @@ itest!(_057_revoke_permissions {
output: "057_revoke_permissions.out",
});

itest!(_058_tasks_microtasks_close {
args: "run 058_tasks_microtasks_close.ts",
output: "058_tasks_microtasks_close.ts.out",
});

itest!(js_import_detect {
args: "run --reload js_import_detect.ts",
output: "js_import_detect.ts.out",
Expand Down

0 comments on commit 5d7bcf8

Please sign in to comment.