@wailsio/runtime 3.0.0-alpha.9 → 3.0.0-alpha.95

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.
Files changed (71) hide show
  1. package/dist/android.js +39 -0
  2. package/{src → dist}/application.js +32 -46
  3. package/dist/browser.js +20 -0
  4. package/dist/callable.js +144 -0
  5. package/dist/calls.js +114 -0
  6. package/dist/cancellable.js +792 -0
  7. package/dist/clipboard.js +30 -0
  8. package/dist/contextmenu.js +83 -0
  9. package/dist/create.js +99 -0
  10. package/dist/dialogs.js +69 -0
  11. package/dist/drag.js +268 -0
  12. package/dist/environment.js +17 -0
  13. package/dist/event_types.js +292 -0
  14. package/dist/events.js +119 -0
  15. package/dist/flags.js +23 -0
  16. package/dist/index.js +88 -0
  17. package/dist/ios.js +28 -0
  18. package/dist/listener.js +44 -0
  19. package/dist/nanoid.js +38 -0
  20. package/dist/plugins/vite.js +64 -0
  21. package/dist/runtime.js +198 -0
  22. package/dist/screens.js +58 -0
  23. package/dist/system.js +158 -0
  24. package/dist/updater.js +74 -0
  25. package/dist/utils.js +98 -0
  26. package/dist/window.js +734 -0
  27. package/dist/wml.js +179 -0
  28. package/package.json +71 -26
  29. package/types/android.d.ts +21 -0
  30. package/types/application.d.ts +3 -10
  31. package/types/browser.d.ts +4 -4
  32. package/types/callable.d.ts +3 -0
  33. package/types/calls.d.ts +52 -24
  34. package/types/cancellable.d.ts +433 -0
  35. package/types/clipboard.d.ts +6 -5
  36. package/types/contextmenu.d.ts +1 -1
  37. package/types/create.d.ts +36 -0
  38. package/types/dialogs.d.ts +132 -20
  39. package/types/drag.d.ts +1 -4
  40. package/types/environment.d.ts +8 -0
  41. package/types/event_types.d.ts +281 -0
  42. package/types/events.d.ts +70 -33
  43. package/types/flags.d.ts +3 -3
  44. package/types/index.d.ts +32 -48
  45. package/types/ios.d.ts +13 -0
  46. package/types/listener.d.ts +9 -0
  47. package/types/nanoid.d.ts +1 -0
  48. package/types/plugins/vite.d.ts +10 -0
  49. package/types/runtime.d.ts +61 -22
  50. package/types/screens.d.ts +64 -7
  51. package/types/system.d.ts +74 -39
  52. package/types/updater.d.ts +65 -0
  53. package/types/utils.d.ts +21 -0
  54. package/types/window.d.ts +327 -13
  55. package/types/wml.d.ts +5 -3
  56. package/src/browser.js +0 -24
  57. package/src/calls.js +0 -123
  58. package/src/clipboard.js +0 -35
  59. package/src/contextmenu.js +0 -98
  60. package/src/dialogs.js +0 -138
  61. package/src/drag.js +0 -125
  62. package/src/events.js +0 -134
  63. package/src/flags.js +0 -63
  64. package/src/index.d.ts +0 -136
  65. package/src/index.js +0 -58
  66. package/src/runtime.js +0 -122
  67. package/src/screens.js +0 -45
  68. package/src/system.js +0 -122
  69. package/src/window.js +0 -122
  70. package/src/wml.js +0 -151
  71. package/tsconfig.json +0 -20
package/src/browser.js DELETED
@@ -1,24 +0,0 @@
1
- /*
2
- _ __ _ __
3
- | | / /___ _(_) /____
4
- | | /| / / __ `/ / / ___/
5
- | |/ |/ / /_/ / / (__ )
6
- |__/|__/\__,_/_/_/____/
7
- The electron alternative for Go
8
- (c) Lea Anthony 2019-present
9
- */
10
-
11
- /* jshint esversion: 9 */
12
- import {newRuntimeCallerWithID, objectNames} from "./runtime";
13
-
14
- const call = newRuntimeCallerWithID(objectNames.Browser, '');
15
- const BrowserOpenURL = 0;
16
-
17
- /**
18
- * Open a browser window to the given URL
19
- * @param {string} url - The URL to open
20
- * @returns {Promise<string>}
21
- */
22
- export function OpenURL(url) {
23
- return call(BrowserOpenURL, {url});
24
- }
package/src/calls.js DELETED
@@ -1,123 +0,0 @@
1
- /*
2
- _ __ _ __
3
- | | / /___ _(_) /____
4
- | | /| / / __ `/ / / ___/
5
- | |/ |/ / /_/ / / (__ )
6
- |__/|__/\__,_/_/_/____/
7
- The electron alternative for Go
8
- (c) Lea Anthony 2019-present
9
- */
10
-
11
- /* jshint esversion: 9 */
12
- import { newRuntimeCallerWithID, objectNames } from "./runtime";
13
- import { nanoid } from 'nanoid/non-secure';
14
-
15
- const CallBinding = 0;
16
- const call = newRuntimeCallerWithID(objectNames.Call, '');
17
- let callResponses = new Map();
18
-
19
- window._wails = window._wails || {};
20
- window._wails.callResultHandler = resultHandler;
21
- window._wails.callErrorHandler = errorHandler;
22
-
23
- function generateID() {
24
- let result;
25
- do {
26
- result = nanoid();
27
- } while (callResponses.has(result));
28
- return result;
29
- }
30
-
31
- export function resultHandler(id, data, isJSON) {
32
- const promiseHandler = getAndDeleteResponse(id);
33
- if (promiseHandler) {
34
- promiseHandler.resolve(isJSON ? JSON.parse(data) : data);
35
- }
36
- }
37
-
38
- export function errorHandler(id, message) {
39
- const promiseHandler = getAndDeleteResponse(id);
40
- if (promiseHandler) {
41
- promiseHandler.reject(message);
42
- }
43
- }
44
-
45
- function getAndDeleteResponse(id) {
46
- const response = callResponses.get(id);
47
- callResponses.delete(id);
48
- return response;
49
- }
50
-
51
- function callBinding(type, options = {}) {
52
- return new Promise((resolve, reject) => {
53
- const id = generateID();
54
- options["call-id"] = id;
55
- callResponses.set(id, { resolve, reject });
56
- call(type, options).catch((error) => {
57
- reject(error);
58
- callResponses.delete(id);
59
- });
60
- });
61
- }
62
-
63
- /**
64
- * Call method.
65
- *
66
- * @param {Object} options - The options for the method.
67
- * @returns {Object} - The result of the call.
68
- */
69
- export function Call(options) {
70
- return callBinding(CallBinding, options);
71
- }
72
-
73
- /**
74
- * Executes a method by name.
75
- *
76
- * @param {string} name - The name of the method in the format 'package.struct.method'.
77
- * @param {...*} args - The arguments to pass to the method.
78
- * @throws {Error} If the name is not a string or is not in the correct format.
79
- * @returns {*} The result of the method execution.
80
- */
81
- export function ByName(name, ...args) {
82
- if (typeof name !== "string" || name.split(".").length !== 3) {
83
- throw new Error("CallByName requires a string in the format 'package.struct.method'");
84
- }
85
- let [packageName, structName, methodName] = name.split(".");
86
- return callBinding(CallBinding, {
87
- packageName,
88
- structName,
89
- methodName,
90
- args
91
- });
92
- }
93
-
94
- /**
95
- * Calls a method by its ID with the specified arguments.
96
- *
97
- * @param {string} methodID - The ID of the method to call.
98
- * @param {...*} args - The arguments to pass to the method.
99
- * @return {*} - The result of the method call.
100
- */
101
- export function ByID(methodID, ...args) {
102
- return callBinding(CallBinding, {
103
- methodID,
104
- args
105
- });
106
- }
107
-
108
- /**
109
- * Calls a method on a plugin.
110
- *
111
- * @param {string} pluginName - The name of the plugin.
112
- * @param {string} methodName - The name of the method to call.
113
- * @param {...*} args - The arguments to pass to the method.
114
- * @returns {*} - The result of the method call.
115
- */
116
- export function Plugin(pluginName, methodName, ...args) {
117
- return callBinding(CallBinding, {
118
- packageName: "wails-plugins",
119
- structName: pluginName,
120
- methodName,
121
- args
122
- });
123
- }
package/src/clipboard.js DELETED
@@ -1,35 +0,0 @@
1
- /*
2
- _ __ _ __
3
- | | / /___ _(_) /____
4
- | | /| / / __ `/ / / ___/
5
- | |/ |/ / /_/ / / (__ )
6
- |__/|__/\__,_/_/_/____/
7
- The electron alternative for Go
8
- (c) Lea Anthony 2019-present
9
- */
10
-
11
- /* jshint esversion: 9 */
12
-
13
- import {newRuntimeCallerWithID, objectNames} from "./runtime";
14
-
15
- const call = newRuntimeCallerWithID(objectNames.Clipboard, '');
16
- const ClipboardSetText = 0;
17
- const ClipboardText = 1;
18
-
19
- /**
20
- * Sets the text to the Clipboard.
21
- *
22
- * @param {string} text - The text to be set to the Clipboard.
23
- * @return {Promise} - A Promise that resolves when the operation is successful.
24
- */
25
- export function SetText(text) {
26
- return call(ClipboardSetText, {text});
27
- }
28
-
29
- /**
30
- * Get the Clipboard text
31
- * @returns {Promise<string>} A promise that resolves with the text from the Clipboard.
32
- */
33
- export function Text() {
34
- return call(ClipboardText);
35
- }
@@ -1,98 +0,0 @@
1
- /*
2
- _ __ _ __
3
- | | / /___ _(_) /____
4
- | | /| / / __ `/ / / ___/
5
- | |/ |/ / /_/ / / (__ )
6
- |__/|__/\__,_/_/_/____/
7
- The electron alternative for Go
8
- (c) Lea Anthony 2019-present
9
- */
10
-
11
- /* jshint esversion: 9 */
12
-
13
- import {newRuntimeCallerWithID, objectNames} from "./runtime";
14
- import {IsDebug} from "./system";
15
-
16
- const call = newRuntimeCallerWithID(objectNames.ContextMenu, '');
17
- const ContextMenuOpen = 0;
18
-
19
- function openContextMenu(id, x, y, data) {
20
- void call(ContextMenuOpen, {id, x, y, data});
21
- }
22
-
23
- export function setupContextMenus() {
24
- window.addEventListener('contextmenu', contextMenuHandler);
25
- }
26
-
27
- function contextMenuHandler(event) {
28
- // Check for custom context menu
29
- let element = event.target;
30
- let customContextMenu = window.getComputedStyle(element).getPropertyValue("--custom-contextmenu");
31
- customContextMenu = customContextMenu ? customContextMenu.trim() : "";
32
- if (customContextMenu) {
33
- event.preventDefault();
34
- let customContextMenuData = window.getComputedStyle(element).getPropertyValue("--custom-contextmenu-data");
35
- openContextMenu(customContextMenu, event.clientX, event.clientY, customContextMenuData);
36
- return
37
- }
38
-
39
- processDefaultContextMenu(event);
40
- }
41
-
42
-
43
- /*
44
- --default-contextmenu: auto; (default) will show the default context menu if contentEditable is true OR text has been selected OR element is input or textarea
45
- --default-contextmenu: show; will always show the default context menu
46
- --default-contextmenu: hide; will always hide the default context menu
47
-
48
- This rule is inherited like normal CSS rules, so nesting works as expected
49
- */
50
- function processDefaultContextMenu(event) {
51
-
52
- // Debug builds always show the menu
53
- if (IsDebug()) {
54
- return;
55
- }
56
-
57
- // Process default context menu
58
- const element = event.target;
59
- const computedStyle = window.getComputedStyle(element);
60
- const defaultContextMenuAction = computedStyle.getPropertyValue("--default-contextmenu").trim();
61
- switch (defaultContextMenuAction) {
62
- case "show":
63
- return;
64
- case "hide":
65
- event.preventDefault();
66
- return;
67
- default:
68
- // Check if contentEditable is true
69
- if (element.isContentEditable) {
70
- return;
71
- }
72
-
73
- // Check if text has been selected
74
- const selection = window.getSelection();
75
- const hasSelection = (selection.toString().length > 0)
76
- if (hasSelection) {
77
- for (let i = 0; i < selection.rangeCount; i++) {
78
- const range = selection.getRangeAt(i);
79
- const rects = range.getClientRects();
80
- for (let j = 0; j < rects.length; j++) {
81
- const rect = rects[j];
82
- if (document.elementFromPoint(rect.left, rect.top) === element) {
83
- return;
84
- }
85
- }
86
- }
87
- }
88
- // Check if tagname is input or textarea
89
- if (element.tagName === "INPUT" || element.tagName === "TEXTAREA") {
90
- if (hasSelection || (!element.readOnly && !element.disabled)) {
91
- return;
92
- }
93
- }
94
-
95
- // hide default context menu
96
- event.preventDefault();
97
- }
98
- }
package/src/dialogs.js DELETED
@@ -1,138 +0,0 @@
1
- /*
2
- _ __ _ __
3
- | | / /___ _(_) /____
4
- | | /| / / __ `/ / / ___/
5
- | |/ |/ / /_/ / / (__ )
6
- |__/|__/\__,_/_/_/____/
7
- The electron alternative for Go
8
- (c) Lea Anthony 2019-present
9
- */
10
-
11
- /* jshint esversion: 9 */
12
-
13
- /**
14
- * @typedef {import("./types").MessageDialogOptions} MessageDialogOptions
15
- * @typedef {import("./types").OpenDialogOptions} OpenDialogOptions
16
- * @typedef {import("./types").SaveDialogOptions} SaveDialogOptions
17
- */
18
-
19
- import {newRuntimeCallerWithID, objectNames} from "./runtime";
20
-
21
- import { nanoid } from 'nanoid/non-secure';
22
-
23
- // Define constants from the `methods` object in Title Case
24
- const DialogInfo = 0;
25
- const DialogWarning = 1;
26
- const DialogError = 2;
27
- const DialogQuestion = 3;
28
- const DialogOpenFile = 4;
29
- const DialogSaveFile = 5;
30
-
31
- const call = newRuntimeCallerWithID(objectNames.Dialog, '');
32
- const dialogResponses = new Map();
33
-
34
- /**
35
- * Generates a unique id that is not present in dialogResponses.
36
- * @returns {string} unique id
37
- */
38
- function generateID() {
39
- let result;
40
- do {
41
- result = nanoid();
42
- } while (dialogResponses.has(result));
43
- return result;
44
- }
45
-
46
- /**
47
- * Shows a dialog of specified type with the given options.
48
- * @param {number} type - type of dialog
49
- * @param {object} options - options for the dialog
50
- * @returns {Promise} promise that resolves with result of dialog
51
- */
52
- function dialog(type, options = {}) {
53
- const id = generateID();
54
- options["dialog-id"] = id;
55
- return new Promise((resolve, reject) => {
56
- dialogResponses.set(id, {resolve, reject});
57
- call(type, options).catch((error) => {
58
- reject(error);
59
- dialogResponses.delete(id);
60
- });
61
- });
62
- }
63
-
64
- /**
65
- * Handles the callback from a dialog.
66
- *
67
- * @param {string} id - The ID of the dialog response.
68
- * @param {string} data - The data received from the dialog.
69
- * @param {boolean} isJSON - Flag indicating whether the data is in JSON format.
70
- *
71
- * @return {undefined}
72
- */
73
- export function dialogResultCallback(id, data, isJSON) {
74
- let p = dialogResponses.get(id);
75
- if (p) {
76
- if (isJSON) {
77
- p.resolve(JSON.parse(data));
78
- } else {
79
- p.resolve(data);
80
- }
81
- dialogResponses.delete(id);
82
- }
83
- }
84
-
85
- /**
86
- * Callback function for handling errors in dialog.
87
- *
88
- * @param {string} id - The id of the dialog response.
89
- * @param {string} message - The error message.
90
- *
91
- * @return {void}
92
- */
93
- export function dialogErrorCallback(id, message) {
94
- let p = dialogResponses.get(id);
95
- if (p) {
96
- p.reject(message);
97
- dialogResponses.delete(id);
98
- }
99
- }
100
-
101
-
102
- // Replace `methods` with constants in Title Case
103
-
104
- /**
105
- * @param {MessageDialogOptions} options - Dialog options
106
- * @returns {Promise<string>} - The label of the button pressed
107
- */
108
- export const Info = (options) => dialog(DialogInfo, options);
109
-
110
- /**
111
- * @param {MessageDialogOptions} options - Dialog options
112
- * @returns {Promise<string>} - The label of the button pressed
113
- */
114
- export const Warning = (options) => dialog(DialogWarning, options);
115
-
116
- /**
117
- * @param {MessageDialogOptions} options - Dialog options
118
- * @returns {Promise<string>} - The label of the button pressed
119
- */
120
- export const Error = (options) => dialog(DialogError, options);
121
-
122
- /**
123
- * @param {MessageDialogOptions} options - Dialog options
124
- * @returns {Promise<string>} - The label of the button pressed
125
- */
126
- export const Question = (options) => dialog(DialogQuestion, options);
127
-
128
- /**
129
- * @param {OpenDialogOptions} options - Dialog options
130
- * @returns {Promise<string[]|string>} Returns selected file or list of files. Returns blank string if no file is selected.
131
- */
132
- export const OpenFile = (options) => dialog(DialogOpenFile, options);
133
-
134
- /**
135
- * @param {SaveDialogOptions} options - Dialog options
136
- * @returns {Promise<string>} Returns the selected file. Returns blank string if no file is selected.
137
- */
138
- export const SaveFile = (options) => dialog(DialogSaveFile, options);
package/src/drag.js DELETED
@@ -1,125 +0,0 @@
1
- /*
2
- _ __ _ __
3
- | | / /___ _(_) /____
4
- | | /| / / __ `/ / / ___/
5
- | |/ |/ / /_/ / / (__ )
6
- |__/|__/\__,_/_/_/____/
7
- The electron alternative for Go
8
- (c) Lea Anthony 2019-present
9
- */
10
-
11
- /* jshint esversion: 9 */
12
-
13
- import {invoke, IsWindows} from "./system";
14
- import {GetFlag} from "./flags";
15
-
16
- let shouldDrag = false;
17
- let resizeEdge = null;
18
- let resizable = false;
19
- let defaultCursor = "auto";
20
- window._wails = window._wails || {};
21
- window._wails.setResizable = setResizable;
22
- window._wails.endDrag = endDrag;
23
-
24
- export function dragTest(e) {
25
- let val = window.getComputedStyle(e.target).getPropertyValue("--webkit-app-region");
26
- if (val && val.trim() !== "drag" || e.buttons !== 1) {
27
- return false;
28
- }
29
- return e.detail === 1;
30
- }
31
-
32
- export function setupDrag() {
33
- window.addEventListener('mousedown', onMouseDown);
34
- window.addEventListener('mousemove', onMouseMove);
35
- window.addEventListener('mouseup', onMouseUp);
36
- }
37
-
38
- export function setResizable(value) {
39
- resizable = value;
40
- }
41
-
42
- export function endDrag() {
43
- document.body.style.cursor = 'default';
44
- shouldDrag = false;
45
- }
46
-
47
- function testResize() {
48
- if( resizeEdge ) {
49
- invoke(`resize:${resizeEdge}`);
50
- return true
51
- }
52
- return false;
53
- }
54
-
55
- function onMouseDown(e) {
56
- if(IsWindows() && testResize() || dragTest(e)) {
57
- shouldDrag = !!isValidDrag(e);
58
- }
59
- }
60
-
61
- function isValidDrag(e) {
62
- // Ignore drag on scrollbars
63
- return !(e.offsetX > e.target.clientWidth || e.offsetY > e.target.clientHeight);
64
- }
65
-
66
- function onMouseUp(e) {
67
- let mousePressed = e.buttons !== undefined ? e.buttons : e.which;
68
- if (mousePressed > 0) {
69
- endDrag();
70
- }
71
- }
72
-
73
- function setResize(cursor = defaultCursor) {
74
- document.documentElement.style.cursor = cursor;
75
- resizeEdge = cursor;
76
- }
77
-
78
- function onMouseMove(e) {
79
- shouldDrag = checkDrag(e);
80
- if (IsWindows() && resizable) {
81
- handleResize(e);
82
- }
83
- }
84
-
85
- function checkDrag(e) {
86
- let mousePressed = e.buttons !== undefined ? e.buttons : e.which;
87
- if(shouldDrag && mousePressed > 0) {
88
- invoke("drag");
89
- return false;
90
- }
91
- return shouldDrag;
92
- }
93
-
94
- function handleResize(e) {
95
- let resizeHandleHeight = GetFlag("system.resizeHandleHeight") || 5;
96
- let resizeHandleWidth = GetFlag("system.resizeHandleWidth") || 5;
97
-
98
- // Extra pixels for the corner areas
99
- let cornerExtra = GetFlag("resizeCornerExtra") || 10;
100
-
101
- let rightBorder = window.outerWidth - e.clientX < resizeHandleWidth;
102
- let leftBorder = e.clientX < resizeHandleWidth;
103
- let topBorder = e.clientY < resizeHandleHeight;
104
- let bottomBorder = window.outerHeight - e.clientY < resizeHandleHeight;
105
-
106
- // Adjust for corners
107
- let rightCorner = window.outerWidth - e.clientX < (resizeHandleWidth + cornerExtra);
108
- let leftCorner = e.clientX < (resizeHandleWidth + cornerExtra);
109
- let topCorner = e.clientY < (resizeHandleHeight + cornerExtra);
110
- let bottomCorner = window.outerHeight - e.clientY < (resizeHandleHeight + cornerExtra);
111
-
112
- // If we aren't on an edge, but were, reset the cursor to default
113
- if (!leftBorder && !rightBorder && !topBorder && !bottomBorder && resizeEdge !== undefined) {
114
- setResize();
115
- }
116
- // Adjusted for corner areas
117
- else if (rightCorner && bottomCorner) setResize("se-resize");
118
- else if (leftCorner && bottomCorner) setResize("sw-resize");
119
- else if (leftCorner && topCorner) setResize("nw-resize");
120
- else if (topCorner && rightCorner) setResize("ne-resize");
121
- else if (leftBorder) setResize("w-resize");
122
- else if (topBorder) setResize("n-resize");
123
- else if (bottomBorder) setResize("s-resize");
124
- else if (rightBorder) setResize("e-resize");
125
- }
package/src/events.js DELETED
@@ -1,134 +0,0 @@
1
- /*
2
- _ __ _ __
3
- | | / /___ _(_) /____
4
- | | /| / / __ `/ / / ___/
5
- | |/ |/ / /_/ / / (__ )
6
- |__/|__/\__,_/_/_/____/
7
- The electron alternative for Go
8
- (c) Lea Anthony 2019-present
9
- */
10
-
11
- /* jshint esversion: 9 */
12
-
13
- /**
14
- * @typedef {import("./types").WailsEvent} WailsEvent
15
- */
16
- import {newRuntimeCallerWithID, objectNames} from "./runtime";
17
-
18
- const call = newRuntimeCallerWithID(objectNames.Events, '');
19
- const EmitMethod = 0;
20
- const eventListeners = new Map();
21
-
22
- class Listener {
23
- constructor(eventName, callback, maxCallbacks) {
24
- this.eventName = eventName;
25
- this.maxCallbacks = maxCallbacks || -1;
26
- this.Callback = (data) => {
27
- callback(data);
28
- if (this.maxCallbacks === -1) return false;
29
- this.maxCallbacks -= 1;
30
- return this.maxCallbacks === 0;
31
- };
32
- }
33
- }
34
-
35
- export class WailsEvent {
36
- constructor(name, data = null) {
37
- this.name = name;
38
- this.data = data;
39
- }
40
- }
41
-
42
-
43
- window._wails = window._wails || {};
44
- window._wails.dispatchWailsEvent = dispatchWailsEvent;
45
-
46
- export function dispatchWailsEvent(event) {
47
- let listeners = eventListeners.get(event.name);
48
- if (listeners) {
49
- let toRemove = listeners.filter(listener => {
50
- let remove = listener.Callback(event);
51
- if (remove) return true;
52
- });
53
- if (toRemove.length > 0) {
54
- listeners = listeners.filter(l => !toRemove.includes(l));
55
- if (listeners.length === 0) eventListeners.delete(event.name);
56
- else eventListeners.set(event.name, listeners);
57
- }
58
- }
59
- }
60
-
61
- /**
62
- * Register a callback function to be called multiple times for a specific event.
63
- *
64
- * @param {string} eventName - The name of the event to register the callback for.
65
- * @param {function} callback - The callback function to be called when the event is triggered.
66
- * @param {number} maxCallbacks - The maximum number of times the callback can be called for the event. Once the maximum number is reached, the callback will no longer be called.
67
- *
68
- @return {function} - A function that, when called, will unregister the callback from the event.
69
- */
70
- export function OnMultiple(eventName, callback, maxCallbacks) {
71
- let listeners = eventListeners.get(eventName) || [];
72
- const thisListener = new Listener(eventName, callback, maxCallbacks);
73
- listeners.push(thisListener);
74
- eventListeners.set(eventName, listeners);
75
- return () => listenerOff(thisListener);
76
- }
77
-
78
- /**
79
- * Registers a callback function to be executed when the specified event occurs.
80
- *
81
- * @param {string} eventName - The name of the event.
82
- * @param {function} callback - The callback function to be executed. It takes no parameters.
83
- * @return {function} - A function that, when called, will unregister the callback from the event. */
84
- export function On(eventName, callback) { return OnMultiple(eventName, callback, -1); }
85
-
86
- /**
87
- * Registers a callback function to be executed only once for the specified event.
88
- *
89
- * @param {string} eventName - The name of the event.
90
- * @param {function} callback - The function to be executed when the event occurs.
91
- * @return {void@return {function} - A function that, when called, will unregister the callback from the event.
92
- */
93
- export function Once(eventName, callback) { return OnMultiple(eventName, callback, 1); }
94
-
95
- /**
96
- * Removes the specified listener from the event listeners collection.
97
- * If all listeners for the event are removed, the event key is deleted from the collection.
98
- *
99
- * @param {Object} listener - The listener to be removed.
100
- */
101
- function listenerOff(listener) {
102
- const eventName = listener.eventName;
103
- let listeners = eventListeners.get(eventName).filter(l => l !== listener);
104
- if (listeners.length === 0) eventListeners.delete(eventName);
105
- else eventListeners.set(eventName, listeners);
106
- }
107
-
108
-
109
- /**
110
- * Removes event listeners for the specified event names.
111
- *
112
- * @param {string} eventName - The name of the event to remove listeners for.
113
- * @param {...string} additionalEventNames - Additional event names to remove listeners for.
114
- * @return {undefined}
115
- */
116
- export function Off(eventName, ...additionalEventNames) {
117
- let eventsToRemove = [eventName, ...additionalEventNames];
118
- eventsToRemove.forEach(eventName => eventListeners.delete(eventName));
119
- }
120
- /**
121
- * Removes all event listeners.
122
- *
123
- * @function OffAll
124
- * @returns {void}
125
- */
126
- export function OffAll() { eventListeners.clear(); }
127
-
128
- /**
129
- * Emits an event using the given event name.
130
- *
131
- * @param {WailsEvent} event - The name of the event to emit.
132
- * @returns {any} - The result of the emitted event.
133
- */
134
- export function Emit(event) { return call(EmitMethod, event); }