Skip to content

Commit a0230c8

Browse files
committed
Improve event callback types
1 parent b3e81f5 commit a0230c8

File tree

2 files changed

+46
-22
lines changed

2 files changed

+46
-22
lines changed

v3/internal/runtime/desktop/@wailsio/runtime/src/events.js

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,13 @@ The electron alternative for Go
1010

1111
/* jshint esversion: 9 */
1212

13-
/**
14-
* @typedef {import("./types").WailsEvent} WailsEvent
13+
/**
14+
* @template D
15+
* @callback WailsEventCallback
16+
* @param {WailsEvent<D>} event
17+
* @return {void}
1518
*/
19+
1620
import {newRuntimeCallerWithID, objectNames} from "./runtime";
1721

1822
import {EventTypes} from "./event_types";
@@ -38,8 +42,14 @@ class Listener {
3842
};
3943
}
4044
}
41-
45+
/**
46+
* @template [D=unknown]
47+
*/
4248
export class WailsEvent {
49+
/**
50+
* @param {string} name - The name of the event
51+
* @param {D} data - The data emitted by the event
52+
*/
4353
constructor(name, data = null) {
4454
this.name = name;
4555
this.data = data;
@@ -67,11 +77,12 @@ function dispatchWailsEvent(event) {
6777
/**
6878
* Register a callback function to be called multiple times for a specific event.
6979
*
80+
* @template [D=unknown]
7081
* @param {string} eventName - The name of the event to register the callback for.
71-
* @param {function} callback - The callback function to be called when the event is triggered.
82+
* @param {WailsEventCallback<D>} callback - The callback function to be called when the event is triggered.
7283
* @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.
7384
*
74-
@return {function} - A function that, when called, will unregister the callback from the event.
85+
@return {() => void} - A function that, when called, will unregister the callback from the event.
7586
*/
7687
export function OnMultiple(eventName, callback, maxCallbacks) {
7788
let listeners = eventListeners.get(eventName) || [];
@@ -84,17 +95,19 @@ export function OnMultiple(eventName, callback, maxCallbacks) {
8495
/**
8596
* Registers a callback function to be executed when the specified event occurs.
8697
*
98+
* @template [D=unknown]
8799
* @param {string} eventName - The name of the event.
88-
* @param {function} callback - The callback function to be executed. It takes no parameters.
89-
* @return {function} - A function that, when called, will unregister the callback from the event. */
100+
* @param {WailsEventCallback<D>} callback - The callback function to be executed.
101+
* @return {() => void} - A function that, when called, will unregister the callback from the event. */
90102
export function On(eventName, callback) { return OnMultiple(eventName, callback, -1); }
91103

92104
/**
93105
* Registers a callback function to be executed only once for the specified event.
94106
*
107+
* @template [D=unknown]
95108
* @param {string} eventName - The name of the event.
96-
* @param {function} callback - The function to be executed when the event occurs.
97-
* @return {function} - A function that, when called, will unregister the callback from the event.
109+
* @param {WailsEventCallback<D>} callback - The function to be executed when the event occurs.
110+
* @return {() => void} - A function that, when called, will unregister the callback from the event.
98111
*/
99112
export function Once(eventName, callback) { return OnMultiple(eventName, callback, 1); }
100113

v3/internal/runtime/desktop/@wailsio/runtime/types/events.d.ts

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,31 @@ export function setup(): void;
22
/**
33
* Register a callback function to be called multiple times for a specific event.
44
*
5+
* @template [D=unknown]
56
* @param {string} eventName - The name of the event to register the callback for.
6-
* @param {function} callback - The callback function to be called when the event is triggered.
7+
* @param {WailsEventCallback<D>} callback - The callback function to be called when the event is triggered.
78
* @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.
89
*
9-
@return {function} - A function that, when called, will unregister the callback from the event.
10+
@return {() => void} - A function that, when called, will unregister the callback from the event.
1011
*/
11-
export function OnMultiple(eventName: string, callback: Function, maxCallbacks: number): Function;
12+
export function OnMultiple<D = unknown>(eventName: string, callback: WailsEventCallback<D>, maxCallbacks: number): () => void;
1213
/**
1314
* Registers a callback function to be executed when the specified event occurs.
1415
*
16+
* @template [D=unknown]
1517
* @param {string} eventName - The name of the event.
16-
* @param {function} callback - The callback function to be executed. It takes no parameters.
17-
* @return {function} - A function that, when called, will unregister the callback from the event. */
18-
export function On(eventName: string, callback: Function): Function;
18+
* @param {WailsEventCallback<D>} callback - The callback function to be executed.
19+
* @return {() => void} - A function that, when called, will unregister the callback from the event. */
20+
export function On<D = unknown>(eventName: string, callback: WailsEventCallback<D>): () => void;
1921
/**
2022
* Registers a callback function to be executed only once for the specified event.
2123
*
24+
* @template [D=unknown]
2225
* @param {string} eventName - The name of the event.
23-
* @param {function} callback - The function to be executed when the event occurs.
24-
* @return {function} - A function that, when called, will unregister the callback from the event.
26+
* @param {WailsEventCallback<D>} callback - The function to be executed when the event occurs.
27+
* @return {() => void} - A function that, when called, will unregister the callback from the event.
2528
*/
26-
export function Once(eventName: string, callback: Function): Function;
29+
export function Once<D = unknown>(eventName: string, callback: WailsEventCallback<D>): () => void;
2730
/**
2831
* Removes event listeners for the specified event names.
2932
*
@@ -232,8 +235,16 @@ export const Types: {
232235
ThemeChanged: string;
233236
};
234237
};
235-
export class WailsEvent {
236-
constructor(name: any, data?: any);
237-
name: any;
238-
data: any;
238+
/**
239+
* @template [D=unknown]
240+
*/
241+
export class WailsEvent<D = unknown> {
242+
/**
243+
* @param {string} name - The name of the event
244+
* @param {D} data - The data emitted by the event
245+
*/
246+
constructor(name: string, data?: D);
247+
name: string;
248+
data: D;
239249
}
250+
export type WailsEventCallback<D> = (event: WailsEvent<D>) => void;

0 commit comments

Comments
 (0)