App lifecycle
Seven events an app can react to, plus one call that answers what it is.
import { app } from "crust";
app.on("ready", () => console.log("started"));
app.on("window-all-closed", () => console.log("last window closed"));
app.on("activate", () => console.log("brought forward"));
app.on("before-quit", async () => {
await saveDraft(); // runs on all-windows-closed too; on ⌘Q AppKit waits, up to its timeout
});
app.on("will-quit", () => console.log("quitting"));
app.on("reopen", () => console.log("clicked in the dock"));
app.on("error", (error) => console.warn("lifecycle", error.message));
const info = app.info();
// { id, name, version, execPath, packaged }
The seven events
| event | when |
|---|---|
ready | the platform started and configured windows opened, just before the event pump |
window-all-closed | every window closed; before-quit and will-quit follow, then the app quits |
activate | the app became the frontmost application |
before-quit | termination was requested and has not happened yet — a listener may still do work |
will-quit | termination is committed; nothing can stop it now |
reopen | the dock icon (or the taskbar button) was clicked while the app had no window open |
error | a lifecycle source refused to yield events; the listener receives the real CrustError |
ready and window-all-closed come from the app itself on every platform. When every window closes the app emits window-all-closed, then before-quit, then will-quit, then quits, so a save-on-quit listener runs on the most common quit and not only on ⌘Q; the window-all-closed listener is still what installs that watcher. A user's ⌘Q reaches before-quit and will-quit through the platform lifecycle source, which also supplies activate and reopen. error is delivered by the watcher when that source refuses to yield events: on a platform with no lifecycle notifications the listener gets the real failure (not-implemented, with requires) once, polling stops, and no event is faked. With no error subscriber the refusal is logged instead, so it is never silent.
before-quit is the only one that can defer. On the ⌘Q path macOS answers NSTerminateLater and replies when every listener has settled; on the all-windows-closed path the app awaits each before-quit listener before it emits will-quit and quits. An await inside it really does run before the process ends either way. A listener that throws is named in the log and never blocks termination — an app that will not quit because one listener is broken is worse than one that quits and says why.
will-quit runs on the way out. It is for the last write that cannot wait, not for anything that
needs a reply.
app.info()
Synchronous, and the same values the bundle and package.json carry:
| field | what |
|---|---|
id | app.id from crust.config.ts |
name | app.name |
version | app.version |
execPath | the running executable, which is inside the bundle in a built app |
packaged | true in a built app, false under crust dev |
What each platform does
macOS provides the four native events and the deferred quit. Windows and Linux answer
not-implemented naming the notification they would need — Windows WM_ACTIVATEAPP, GTK focus
events — rather than emitting an event that never fires. A listener registered on those platforms is
not an error; it simply never hears that event. The refusal reaches app.on("error") once with the
real CrustError (or goes to the log with no subscriber), then polling stops. ready and
window-all-closed are unaffected: the app emits them itself on every platform, and the all-windows-closed path emits before-quit and will-quit before quitting there too. A throwing listener on that path is named in the log and never blocks the quit.
app.info() works everywhere: it reads the config the process already loaded.