Skip to content

Commit

Permalink
Improve event callback types
Browse files Browse the repository at this point in the history
  • Loading branch information
IanVS committed Jan 30, 2025
1 parent b3e81f5 commit a0230c8
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 22 deletions.
31 changes: 22 additions & 9 deletions v3/internal/runtime/desktop/@wailsio/runtime/src/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,13 @@ The electron alternative for Go

/* jshint esversion: 9 */

/**
* @typedef {import("./types").WailsEvent} WailsEvent
/**
* @template D
* @callback WailsEventCallback
* @param {WailsEvent<D>} event
* @return {void}
*/

import {newRuntimeCallerWithID, objectNames} from "./runtime";

import {EventTypes} from "./event_types";
Expand All @@ -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;
Expand Down Expand Up @@ -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<D>} 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) || [];
Expand All @@ -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<D>} 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<D>} 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); }

Expand Down
37 changes: 24 additions & 13 deletions v3/internal/runtime/desktop/@wailsio/runtime/types/events.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<D>} 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<D = unknown>(eventName: string, callback: WailsEventCallback<D>, 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<D>} callback - The callback function to be executed.
* @return {() => void} - A function that, when called, will unregister the callback from the event. */
export function On<D = unknown>(eventName: string, callback: WailsEventCallback<D>): () => 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<D>} 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<D = unknown>(eventName: string, callback: WailsEventCallback<D>): () => void;
/**
* Removes event listeners for the specified event names.
*
Expand Down Expand Up @@ -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<D = unknown> {
/**
* @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<D> = (event: WailsEvent<D>) => void;

0 comments on commit a0230c8

Please sign in to comment.