Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Program lifecycle [WIP] #922

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 29 additions & 5 deletions runtime/reference/deno_namespace_apis.md
Original file line number Diff line number Diff line change
Expand Up @@ -753,7 +753,8 @@ Deno supports browser compatible lifecycle events:
Scheduling more asynchronous work (like timers or network requests) will cause
the program to continue.
- [`unload`](https://developer.mozilla.org/en-US/docs/Web/API/Window/unload_event):
fired when the document or a child resource is being unloaded.
fired when the program has no more work to do. Scheduling more asynchronous
work (like timers or network requests) **does not** keep the program alive.
- [`unhandledrejection`](https://developer.mozilla.org/en-US/docs/Web/API/Window/unhandledrejection_event):
fired when a promise that has no rejection handler is rejected, ie. a promise
that has no `.catch()` handler or a second argument to `.then()`.
Expand All @@ -762,13 +763,36 @@ Deno supports browser compatible lifecycle events:
rejected. This event is fired only if there's `unhandledrejection` listener
installed that prevents propagation of the event (which would result in the
program terminating with an error).
- [`error`](https://developer.mozilla.org/en-US/docs/Web/API/Window/error_event):
TODO(bartlomieju)

Deno also support Node.js compatible lifecycle events:

- [`process.on("beforeExit")`](https://nodejs.org/api/process.html#event-beforeexit):
fired when the event loop has no more work to do and is about to exit.
Scheduling more asynchronous work (like timers or network requests) will cause
the program to continue. Counterpart to `beforeunload` Web event. Fires
immediatelly after `beforeunload` event.
- [`process.on("exit")`](https://nodejs.org/api/process.html#event-exit): fired
when the program has no more work to do. Scheduling more asynchronous work
(like timers or network requests) **does not** keep the program alive.
Counterpart to `unload` Web event. Fired immediately after `unload` event.
- [`process.on("rejectionHandled")`](https://nodejs.org/api/process.html#event-rejectionhandled):
TODO(bartlomieju)
- [`process.on("uncaughtException")`](https://nodejs.org/api/process.html#event-uncaughtexception):
TODO(bartlomieju) Counterpart to `error` Web event. Fires immediatelly after
`error` event.
- [`process.on("unhandledRejection")`](https://nodejs.org/api/process.html#event-unhandledrejection):
TODO(bartlomieju) Counterpart to `unhandledrejection` Web event. Fires
immediatelly after `unhandledrejection` event.

You can use these events to provide setup and cleanup code in your program.

Listeners for `load` events can be asynchronous and will be awaited, this event
cannot be canceled. Listeners for `beforeunload` need to be synchronous and can
be cancelled to keep the program running. Listeners for `unload` events need to
be synchronous and cannot be cancelled.
// TODO(bartlomieju): what about Node.js listeners? Are they awaited? Listeners
for `load` events can be asynchronous and will be awaited, this event cannot be
canceled. Listeners for `beforeunload` need to be synchronous and can be
cancelled to keep the program running. Listeners for `unload` events need to be
synchronous and cannot be cancelled.

**main.ts**

Expand Down