diff --git a/v3/internal/runtime/desktop/@wailsio/runtime/src/events.js b/v3/internal/runtime/desktop/@wailsio/runtime/src/events.js index afbcbb9221c..aefeb8bd411 100644 --- a/v3/internal/runtime/desktop/@wailsio/runtime/src/events.js +++ b/v3/internal/runtime/desktop/@wailsio/runtime/src/events.js @@ -10,9 +10,13 @@ The electron alternative for Go /* jshint esversion: 9 */ -/** - * @typedef {import("./types").WailsEvent} WailsEvent +/** + * @template D + * @callback WailsEventCallback + * @param {WailsEvent} event + * @return {void} */ + import {newRuntimeCallerWithID, objectNames} from "./runtime"; import {EventTypes} from "./event_types"; @@ -38,8 +42,14 @@ class Listener { }; } } - +/** + * @template [D=unknown] + */ export class WailsEvent { + /** + * @param {string} name - The name of the event + * @param {D} data - The data emitted by the event + */ constructor(name, data = null) { this.name = name; this.data = data; @@ -67,11 +77,12 @@ function dispatchWailsEvent(event) { /** * Register a callback function to be called multiple times for a specific event. * + * @template [D=unknown] * @param {string} eventName - The name of the event to register the callback for. - * @param {function} callback - The callback function to be called when the event is triggered. + * @param {WailsEventCallback} callback - The callback function to be called when the event is triggered. * @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. * - @return {function} - A function that, when called, will unregister the callback from the event. + @return {() => void} - A function that, when called, will unregister the callback from the event. */ export function OnMultiple(eventName, callback, maxCallbacks) { let listeners = eventListeners.get(eventName) || []; @@ -84,17 +95,19 @@ export function OnMultiple(eventName, callback, maxCallbacks) { /** * Registers a callback function to be executed when the specified event occurs. * + * @template [D=unknown] * @param {string} eventName - The name of the event. - * @param {function} callback - The callback function to be executed. It takes no parameters. - * @return {function} - A function that, when called, will unregister the callback from the event. */ + * @param {WailsEventCallback} callback - The callback function to be executed. + * @return {() => void} - A function that, when called, will unregister the callback from the event. */ export function On(eventName, callback) { return OnMultiple(eventName, callback, -1); } /** * Registers a callback function to be executed only once for the specified event. * + * @template [D=unknown] * @param {string} eventName - The name of the event. - * @param {function} callback - The function to be executed when the event occurs. - * @return {function} - A function that, when called, will unregister the callback from the event. + * @param {WailsEventCallback} callback - The function to be executed when the event occurs. + * @return {() => void} - A function that, when called, will unregister the callback from the event. */ export function Once(eventName, callback) { return OnMultiple(eventName, callback, 1); } diff --git a/v3/internal/runtime/desktop/@wailsio/runtime/types/events.d.ts b/v3/internal/runtime/desktop/@wailsio/runtime/types/events.d.ts index f9757db6956..f1264c03460 100644 --- a/v3/internal/runtime/desktop/@wailsio/runtime/types/events.d.ts +++ b/v3/internal/runtime/desktop/@wailsio/runtime/types/events.d.ts @@ -2,28 +2,31 @@ export function setup(): void; /** * Register a callback function to be called multiple times for a specific event. * + * @template [D=unknown] * @param {string} eventName - The name of the event to register the callback for. - * @param {function} callback - The callback function to be called when the event is triggered. + * @param {WailsEventCallback} callback - The callback function to be called when the event is triggered. * @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. * - @return {function} - A function that, when called, will unregister the callback from the event. + @return {() => void} - A function that, when called, will unregister the callback from the event. */ -export function OnMultiple(eventName: string, callback: Function, maxCallbacks: number): Function; +export function OnMultiple(eventName: string, callback: WailsEventCallback, maxCallbacks: number): () => void; /** * Registers a callback function to be executed when the specified event occurs. * + * @template [D=unknown] * @param {string} eventName - The name of the event. - * @param {function} callback - The callback function to be executed. It takes no parameters. - * @return {function} - A function that, when called, will unregister the callback from the event. */ -export function On(eventName: string, callback: Function): Function; + * @param {WailsEventCallback} callback - The callback function to be executed. + * @return {() => void} - A function that, when called, will unregister the callback from the event. */ +export function On(eventName: string, callback: WailsEventCallback): () => void; /** * Registers a callback function to be executed only once for the specified event. * + * @template [D=unknown] * @param {string} eventName - The name of the event. - * @param {function} callback - The function to be executed when the event occurs. - * @return {function} - A function that, when called, will unregister the callback from the event. + * @param {WailsEventCallback} callback - The function to be executed when the event occurs. + * @return {() => void} - A function that, when called, will unregister the callback from the event. */ -export function Once(eventName: string, callback: Function): Function; +export function Once(eventName: string, callback: WailsEventCallback): () => void; /** * Removes event listeners for the specified event names. * @@ -232,8 +235,16 @@ export const Types: { ThemeChanged: string; }; }; -export class WailsEvent { - constructor(name: any, data?: any); - name: any; - data: any; +/** + * @template [D=unknown] + */ +export class WailsEvent { + /** + * @param {string} name - The name of the event + * @param {D} data - The data emitted by the event + */ + constructor(name: string, data?: D); + name: string; + data: D; } +export type WailsEventCallback = (event: WailsEvent) => void;