Skip to content

Events

The default main-process export exposes the global event emitter. Window, webview, menu, and tray objects also expose scoped listeners. In the native SDKs there is no emitter: window and webview events are C callbacks registered at creation time.

import Electrobun, { BrowserWindow } from "electrobun/main";
const win = new BrowserWindow({
title: "Events",
url: "views://mainview/index.html",
});
Electrobun.events.on("will-navigate", (event) => {
console.log("Any view will navigate", event);
});
win.webview.on("will-navigate", (event: unknown) => {
console.log("This view will navigate", event);
});

The rest of this page documents the TypeScript emitter surface used by Cottontail and Bun.

Event objects

Electrobun events expose:

  • name: emitted event name.
  • data: event-specific payload.
  • response: optional response consumed by a cancellable native action.
  • responseWasSet: whether a handler assigned a response.
  • clearResponse(): remove a response set by an earlier handler.

Handlers run synchronously in registration order. A cancellable action reads the response after emission, so assign event.response before returning from the callback.

Application events

Current application event names are application-menu-clicked, context-menu-clicked, open-url, reopen, and before-quit.

import Electrobun from "electrobun/main";
Electrobun.events.on("open-url", (event) => {
const data = event.data as { url: string };
const url = new URL(data.url);
console.log(url.protocol, url.pathname);
});
Electrobun.events.on("before-quit", (event) => {
const shouldCancel = false;
if (shouldCancel) {
event.response = { allow: false };
}
});

On macOS, custom schemes and associated files arrive through open-url; files use file:// URLs. Configure them with app.urlSchemes and app.fileAssociations.

Window events

Current window event names are close, will-close, resize, move, focus, blur, keyDown, and keyUp. Per-window close handlers run before the internal last-window shutdown logic.

Webview events

Current webview event names are will-navigate, did-navigate, did-navigate-in-page, did-commit-navigation, dom-ready, new-window-open, host-message, download-started, download-progress, download-completed, and download-failed.

The BrowserView.on() typed surface currently includes navigation, readiness, and download events. <electrobun-webview> exposes its browser-side event surface documented on the webview tag page.

Shutdown

Use before-quit to synchronously save in-memory state, initiate already prepared cleanup, or cancel shutdown. The current event emitter does not await a returned promise.

import Electrobun from "electrobun/main";
Electrobun.events.on("before-quit", (event) => {
const canQuit = true;
if (!canQuit) {
event.response = { allow: false };
return;
}
console.log("Application is quitting");
});
process.on("exit", (code) => {
console.log("Process exited with code", code);
});