@wailsio/runtime 3.0.0-alpha.73 → 3.0.0-alpha.74

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.
package/dist/calls.js CHANGED
@@ -12,8 +12,6 @@ import { newRuntimeCaller, objectNames } from "./runtime.js";
12
12
  import { nanoid } from "./nanoid.js";
13
13
  // Setup
14
14
  window._wails = window._wails || {};
15
- window._wails.callResultHandler = resultHandler;
16
- window._wails.callErrorHandler = errorHandler;
17
15
  const call = newRuntimeCaller(objectNames.Call);
18
16
  const cancelCall = newRuntimeCaller(objectNames.CancelCall);
19
17
  const callResponses = new Map();
@@ -34,90 +32,6 @@ export class RuntimeError extends Error {
34
32
  this.name = "RuntimeError";
35
33
  }
36
34
  }
37
- /**
38
- * Handles the result of a call request.
39
- *
40
- * @param id - The id of the request to handle the result for.
41
- * @param data - The result data of the request.
42
- * @param isJSON - Indicates whether the data is JSON or not.
43
- */
44
- function resultHandler(id, data, isJSON) {
45
- const resolvers = getAndDeleteResponse(id);
46
- if (!resolvers) {
47
- return;
48
- }
49
- if (!data) {
50
- resolvers.resolve(undefined);
51
- }
52
- else if (!isJSON) {
53
- resolvers.resolve(data);
54
- }
55
- else {
56
- try {
57
- resolvers.resolve(JSON.parse(data));
58
- }
59
- catch (err) {
60
- resolvers.reject(new TypeError("could not parse result: " + err.message, { cause: err }));
61
- }
62
- }
63
- }
64
- /**
65
- * Handles the error from a call request.
66
- *
67
- * @param id - The id of the promise handler.
68
- * @param data - The error data to reject the promise handler with.
69
- * @param isJSON - Indicates whether the data is JSON or not.
70
- */
71
- function errorHandler(id, data, isJSON) {
72
- const resolvers = getAndDeleteResponse(id);
73
- if (!resolvers) {
74
- return;
75
- }
76
- if (!isJSON) {
77
- resolvers.reject(new Error(data));
78
- }
79
- else {
80
- let error;
81
- try {
82
- error = JSON.parse(data);
83
- }
84
- catch (err) {
85
- resolvers.reject(new TypeError("could not parse error: " + err.message, { cause: err }));
86
- return;
87
- }
88
- let options = {};
89
- if (error.cause) {
90
- options.cause = error.cause;
91
- }
92
- let exception;
93
- switch (error.kind) {
94
- case "ReferenceError":
95
- exception = new ReferenceError(error.message, options);
96
- break;
97
- case "TypeError":
98
- exception = new TypeError(error.message, options);
99
- break;
100
- case "RuntimeError":
101
- exception = new RuntimeError(error.message, options);
102
- break;
103
- default:
104
- exception = new Error(error.message, options);
105
- break;
106
- }
107
- resolvers.reject(exception);
108
- }
109
- }
110
- /**
111
- * Retrieves and removes the response associated with the given ID from the callResponses map.
112
- *
113
- * @param id - The ID of the response to be retrieved and removed.
114
- * @returns The response object associated with the given ID, if any.
115
- */
116
- function getAndDeleteResponse(id) {
117
- const response = callResponses.get(id);
118
- callResponses.delete(id);
119
- return response;
120
- }
121
35
  /**
122
36
  * Generates a unique ID using the nanoid library.
123
37
  *
@@ -147,10 +61,13 @@ export function Call(options) {
147
61
  const result = CancellablePromise.withResolvers();
148
62
  callResponses.set(id, { resolve: result.resolve, reject: result.reject });
149
63
  const request = call(CallBinding, Object.assign({ "call-id": id }, options));
150
- let running = false;
151
- request.then(() => {
152
- running = true;
64
+ let running = true;
65
+ request.then((res) => {
66
+ running = false;
67
+ callResponses.delete(id);
68
+ result.resolve(res);
153
69
  }, (err) => {
70
+ running = false;
154
71
  callResponses.delete(id);
155
72
  result.reject(err);
156
73
  });
package/dist/dialogs.js CHANGED
@@ -8,13 +8,9 @@ The electron alternative for Go
8
8
  (c) Lea Anthony 2019-present
9
9
  */
10
10
  import { newRuntimeCaller, objectNames } from "./runtime.js";
11
- import { nanoid } from './nanoid.js';
12
11
  // setup
13
12
  window._wails = window._wails || {};
14
- window._wails.dialogErrorCallback = dialogErrorCallback;
15
- window._wails.dialogResultCallback = dialogResultCallback;
16
13
  const call = newRuntimeCaller(objectNames.Dialog);
17
- const dialogResponses = new Map();
18
14
  // Define constants from the `methods` object in Title Case
19
15
  const DialogInfo = 0;
20
16
  const DialogWarning = 1;
@@ -22,63 +18,6 @@ const DialogError = 2;
22
18
  const DialogQuestion = 3;
23
19
  const DialogOpenFile = 4;
24
20
  const DialogSaveFile = 5;
25
- /**
26
- * Handles the result of a dialog request.
27
- *
28
- * @param id - The id of the request to handle the result for.
29
- * @param data - The result data of the request.
30
- * @param isJSON - Indicates whether the data is JSON or not.
31
- */
32
- function dialogResultCallback(id, data, isJSON) {
33
- let resolvers = getAndDeleteResponse(id);
34
- if (!resolvers) {
35
- return;
36
- }
37
- if (isJSON) {
38
- try {
39
- resolvers.resolve(JSON.parse(data));
40
- }
41
- catch (err) {
42
- resolvers.reject(new TypeError("could not parse result: " + err.message, { cause: err }));
43
- }
44
- }
45
- else {
46
- resolvers.resolve(data);
47
- }
48
- }
49
- /**
50
- * Handles the error from a dialog request.
51
- *
52
- * @param id - The id of the promise handler.
53
- * @param message - An error message.
54
- */
55
- function dialogErrorCallback(id, message) {
56
- var _a;
57
- (_a = getAndDeleteResponse(id)) === null || _a === void 0 ? void 0 : _a.reject(new window.Error(message));
58
- }
59
- /**
60
- * Retrieves and removes the response associated with the given ID from the dialogResponses map.
61
- *
62
- * @param id - The ID of the response to be retrieved and removed.
63
- * @returns The response object associated with the given ID, if any.
64
- */
65
- function getAndDeleteResponse(id) {
66
- const response = dialogResponses.get(id);
67
- dialogResponses.delete(id);
68
- return response;
69
- }
70
- /**
71
- * Generates a unique ID using the nanoid library.
72
- *
73
- * @returns A unique ID that does not exist in the dialogResponses set.
74
- */
75
- function generateID() {
76
- let result;
77
- do {
78
- result = nanoid();
79
- } while (dialogResponses.has(result));
80
- return result;
81
- }
82
21
  /**
83
22
  * Presents a dialog of specified type with the given options.
84
23
  *
@@ -87,14 +26,7 @@ function generateID() {
87
26
  * @returns A promise that resolves with result of dialog.
88
27
  */
89
28
  function dialog(type, options = {}) {
90
- const id = generateID();
91
- return new Promise((resolve, reject) => {
92
- dialogResponses.set(id, { resolve, reject });
93
- call(type, Object.assign({ "dialog-id": id }, options)).catch((err) => {
94
- dialogResponses.delete(id);
95
- reject(err);
96
- });
97
- });
29
+ return call(type, options);
98
30
  }
99
31
  /**
100
32
  * Presents an info dialog.
package/dist/index.js CHANGED
@@ -32,6 +32,8 @@ export { Application, Browser, Call, Clipboard, Dialogs, Events, Flags, Screens,
32
32
  */
33
33
  export { Create };
34
34
  export * from "./cancellable.js";
35
+ // Export transport interfaces and utilities
36
+ export { setTransport, getTransport, objectNames, clientId, } from "./runtime.js";
35
37
  // Notify backend
36
38
  window._wails.invoke = System.invoke;
37
39
  // Register platform handlers (internal API)
package/dist/runtime.js CHANGED
@@ -7,8 +7,10 @@
7
7
  The electron alternative for Go
8
8
  (c) Lea Anthony 2019-present
9
9
  */
10
- import { nanoid } from './nanoid.js';
10
+ import { nanoid } from "./nanoid.js";
11
11
  const runtimeURL = window.location.origin + "/wails/runtime";
12
+ // Re-export nanoid for custom transport implementations
13
+ export { nanoid };
12
14
  // Object Names
13
15
  export const objectNames = Object.freeze({
14
16
  Call: 0,
@@ -24,6 +26,39 @@ export const objectNames = Object.freeze({
24
26
  CancelCall: 10,
25
27
  });
26
28
  export let clientId = nanoid();
29
+ /**
30
+ * Custom transport implementation (can be set by user)
31
+ */
32
+ let customTransport = null;
33
+ /**
34
+ * Set a custom transport for all Wails runtime calls.
35
+ * This allows you to replace the default HTTP fetch transport with
36
+ * WebSockets, custom protocols, or any other mechanism.
37
+ *
38
+ * @param transport - Your custom transport implementation
39
+ *
40
+ * @example
41
+ * ```typescript
42
+ * import { setTransport } from '/wails/runtime.js';
43
+ *
44
+ * const wsTransport = {
45
+ * call: async (objectID, method, windowName, args) => {
46
+ * // Your WebSocket implementation
47
+ * }
48
+ * };
49
+ *
50
+ * setTransport(wsTransport);
51
+ * ```
52
+ */
53
+ export function setTransport(transport) {
54
+ customTransport = transport;
55
+ }
56
+ /**
57
+ * Get the current transport (useful for extending/wrapping)
58
+ */
59
+ export function getTransport() {
60
+ return customTransport;
61
+ }
27
62
  /**
28
63
  * Creates a new runtime caller with specified ID.
29
64
  *
@@ -38,19 +73,31 @@ export function newRuntimeCaller(object, windowName = '') {
38
73
  }
39
74
  async function runtimeCallWithID(objectID, method, windowName, args) {
40
75
  var _a, _b;
76
+ // Use custom transport if available
77
+ if (customTransport) {
78
+ return customTransport.call(objectID, method, windowName, args);
79
+ }
80
+ // Default HTTP fetch transport
41
81
  let url = new URL(runtimeURL);
42
- url.searchParams.append("object", objectID.toString());
43
- url.searchParams.append("method", method.toString());
44
- if (args) {
45
- url.searchParams.append("args", JSON.stringify(args));
82
+ let body = {
83
+ object: objectID,
84
+ method
85
+ };
86
+ if (args !== null && args !== undefined) {
87
+ body.args = args;
46
88
  }
47
89
  let headers = {
48
- ["x-wails-client-id"]: clientId
90
+ ["x-wails-client-id"]: clientId,
91
+ ["Content-Type"]: "application/json"
49
92
  };
50
93
  if (windowName) {
51
94
  headers["x-wails-window-name"] = windowName;
52
95
  }
53
- let response = await fetch(url, { headers });
96
+ let response = await fetch(url, {
97
+ method: 'POST',
98
+ headers,
99
+ body: JSON.stringify(body)
100
+ });
54
101
  if (!response.ok) {
55
102
  throw new Error(await response.text());
56
103
  }
package/dist/system.js CHANGED
@@ -11,6 +11,7 @@ import { newRuntimeCaller, objectNames } from "./runtime.js";
11
11
  const call = newRuntimeCaller(objectNames.System);
12
12
  const SystemIsDarkMode = 0;
13
13
  const SystemEnvironment = 1;
14
+ const SystemCapabilities = 2;
14
15
  const ApplicationFilesDroppedWithContext = 100; // New method ID for enriched drop event
15
16
  const _invoke = (function () {
16
17
  var _a, _b, _c, _d, _e;
@@ -43,13 +44,7 @@ export function IsDarkMode() {
43
44
  * @returns A promise that resolves to an object containing the capabilities.
44
45
  */
45
46
  export async function Capabilities() {
46
- let response = await fetch("/wails/capabilities");
47
- if (response.ok) {
48
- return response.json();
49
- }
50
- else {
51
- throw new Error("could not fetch capabilities: " + response.statusText);
52
- }
47
+ return call(SystemCapabilities);
53
48
  }
54
49
  /**
55
50
  * Retrieves environment details.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@wailsio/runtime",
3
3
  "type": "module",
4
- "version": "3.0.0-alpha.73",
4
+ "version": "3.0.0-alpha.74",
5
5
  "description": "Wails Runtime",
6
6
  "types": "types/index.d.ts",
7
7
  "exports": {
package/types/index.d.ts CHANGED
@@ -20,3 +20,4 @@ export { Application, Browser, Call, Clipboard, Dialogs, Events, Flags, Screens,
20
20
  */
21
21
  export { Create };
22
22
  export * from "./cancellable.js";
23
+ export { setTransport, getTransport, type RuntimeTransport, objectNames, clientId, } from "./runtime.js";
@@ -1,3 +1,5 @@
1
+ import { nanoid } from "./nanoid.js";
2
+ export { nanoid };
1
3
  export declare const objectNames: Readonly<{
2
4
  Call: 0;
3
5
  Clipboard: 1;
@@ -12,6 +14,48 @@ export declare const objectNames: Readonly<{
12
14
  CancelCall: 10;
13
15
  }>;
14
16
  export declare let clientId: string;
17
+ /**
18
+ * RuntimeTransport defines the interface for custom IPC transport implementations.
19
+ * Implement this interface to use WebSockets, custom protocols, or any other
20
+ * transport mechanism instead of the default HTTP fetch.
21
+ */
22
+ export interface RuntimeTransport {
23
+ /**
24
+ * Send a runtime call and return the response.
25
+ *
26
+ * @param objectID - The Wails object ID (0=Call, 1=Clipboard, etc.)
27
+ * @param method - The method ID to call
28
+ * @param windowName - Optional window name
29
+ * @param args - Arguments to pass (will be JSON stringified if present)
30
+ * @returns Promise that resolves with the response data
31
+ */
32
+ call(objectID: number, method: number, windowName: string, args: any): Promise<any>;
33
+ }
34
+ /**
35
+ * Set a custom transport for all Wails runtime calls.
36
+ * This allows you to replace the default HTTP fetch transport with
37
+ * WebSockets, custom protocols, or any other mechanism.
38
+ *
39
+ * @param transport - Your custom transport implementation
40
+ *
41
+ * @example
42
+ * ```typescript
43
+ * import { setTransport } from '/wails/runtime.js';
44
+ *
45
+ * const wsTransport = {
46
+ * call: async (objectID, method, windowName, args) => {
47
+ * // Your WebSocket implementation
48
+ * }
49
+ * };
50
+ *
51
+ * setTransport(wsTransport);
52
+ * ```
53
+ */
54
+ export declare function setTransport(transport: RuntimeTransport | null): void;
55
+ /**
56
+ * Get the current transport (useful for extending/wrapping)
57
+ */
58
+ export declare function getTransport(): RuntimeTransport | null;
15
59
  /**
16
60
  * Creates a new runtime caller with specified ID.
17
61
  *