electrobun 0.2.1-beta.0 → 0.3.1-beta.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,7 +2,8 @@ type WebviewEventTypes =
2
2
  | "did-navigate"
3
3
  | "did-navigate-in-page"
4
4
  | "did-commit-navigation"
5
- | "dom-ready";
5
+ | "dom-ready"
6
+ | "host-message";
6
7
 
7
8
  type Rect = { x: number; y: number; width: number; height: number };
8
9
 
@@ -97,14 +97,25 @@ const startRPCServer = () => {
97
97
  }
98
98
  },
99
99
  close(ws, code, reason) {
100
+ if (!ws?.data) {
101
+ return;
102
+ }
100
103
  const { webviewId } = ws.data;
101
104
  console.log("Closed:", webviewId, code, reason);
102
- socketMap[webviewId].socket = null;
105
+ if (socketMap[webviewId]) {
106
+ socketMap[webviewId].socket = null;
107
+ }
103
108
  },
104
109
 
105
110
  message(ws, message) {
111
+ if (!ws?.data) {
112
+ return;
113
+ }
106
114
  const { webviewId } = ws.data;
107
115
  const browserView = BrowserView.getById(webviewId);
116
+ if (!browserView) {
117
+ return;
118
+ }
108
119
 
109
120
  if (browserView.rpcHandler) {
110
121
  if (typeof message === "string") {
@@ -12,7 +12,7 @@ export default {
12
12
  domReady: (data) =>
13
13
  new ElectrobunEvent<{ detail: string }, {}>("dom-ready", data),
14
14
  newWindowOpen: (data) =>
15
- new ElectrobunEvent<{
15
+ new ElectrobunEvent<{
16
16
  detail: string | {
17
17
  url: string;
18
18
  isCmdClick: boolean;
@@ -21,4 +21,6 @@ export default {
21
21
  userGesture?: boolean;
22
22
  }
23
23
  }, {}>("new-window-open", data),
24
+ hostMessage: (data) =>
25
+ new ElectrobunEvent<{ detail: string }, {}>("host-message", data),
24
26
  };
@@ -573,13 +573,19 @@ export const ffi = {
573
573
  })();
574
574
  ` + `
575
575
  function emitWebviewEvent (eventName, detail) {
576
- // Note: There appears to be some race bug with Bun FFI where sites can
576
+ // Note: There appears to be some race bug with Bun FFI where sites can
577
577
  // init (like views://myview/index.html) so fast while the Bun FFI to load a url is still executing
578
578
  // or something where the JSCallback that this postMessage fires is not available or busy or
579
579
  // its memory is allocated to something else or something and the handler receives garbage data in Bun.
580
- setTimeout(() => {
580
+ setTimeout(() => {
581
581
  window.__electrobunInternalBridge?.postMessage(JSON.stringify({id: 'webviewEvent', type: 'message', payload: {id: window.__electrobunWebviewId, eventName, detail}}));
582
582
  });
583
+ };
584
+
585
+ // Allow preload scripts to send custom messages to the host webview
586
+ // Usage: window.__electrobunSendToHost({ type: 'myEvent', data: {...} })
587
+ window.__electrobunSendToHost = function(message) {
588
+ emitWebviewEvent('host-message', JSON.stringify(message));
583
589
  };
584
590
 
585
591
  window.addEventListener('load', function(event) {
@@ -983,10 +989,10 @@ const webviewEventHandler = (id, eventName, detail) => {
983
989
 
984
990
  // This is a webviewtag so we should send the event into the parent as well
985
991
  // TODO XX: escape event name and detail to remove `
986
- // NOTE: for new-window-open the detail is a json string that needs to be parsed
992
+ // NOTE: for new-window-open and host-message the detail is a json string that needs to be parsed
987
993
  let js;
988
- if (eventName === 'new-window-open') {
989
- js = `document.querySelector('#electrobun-webview-${id}').emit(\`${eventName}\`, ${detail});`
994
+ if (eventName === 'new-window-open' || eventName === 'host-message') {
995
+ js = `document.querySelector('#electrobun-webview-${id}').emit(\`${eventName}\`, ${detail});`
990
996
  } else {
991
997
  js = `document.querySelector('#electrobun-webview-${id}').emit(\`${eventName}\`, \`${detail}\`);`
992
998
  }
@@ -1001,6 +1007,7 @@ const webviewEventHandler = (id, eventName, detail) => {
1001
1007
  "did-commit-navigation": "didCommitNavigation",
1002
1008
  "dom-ready": "domReady",
1003
1009
  "new-window-open": "newWindowOpen",
1010
+ "host-message": "hostMessage",
1004
1011
  };
1005
1012
 
1006
1013
  // todo: the events map should use the same hyphenated names instead of camelCase
@@ -1012,11 +1019,11 @@ const webviewEventHandler = (id, eventName, detail) => {
1012
1019
  return { success: false };
1013
1020
  }
1014
1021
 
1015
- // Parse JSON data for new-window-open events
1022
+ // Parse JSON data for new-window-open and host-message events
1016
1023
  let parsedDetail = detail;
1017
- if (eventName === "new-window-open") {
1024
+ if (eventName === "new-window-open" || eventName === "host-message") {
1018
1025
  try {
1019
- parsedDetail = JSON.parse(detail);
1026
+ parsedDetail = JSON.parse(detail);
1020
1027
  } catch (e) {
1021
1028
  console.error('[webviewEventHandler] Failed to parse JSON:', e);
1022
1029
  // Fallback to string if parsing fails (backward compatibility)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "electrobun",
3
- "version": "0.2.1-beta.0",
3
+ "version": "0.3.1-beta.0",
4
4
  "description": "Build ultra fast, tiny, and cross-platform desktop apps with Typescript.",
5
5
  "license": "MIT",
6
6
  "author": "Blackboard Technologies Inc.",