enhance(api): add generic parameter to emit and emitTo functions (#13066)

closes #13059
This commit is contained in:
阿豪
2025-03-25 11:34:20 +08:00
committed by GitHub
parent f235ec0113
commit dd13728334
4 changed files with 17 additions and 13 deletions

View File

@@ -0,0 +1,4 @@
---
"@tauri-apps/api": patch:enhance
---
Add a generic to `emit` and `emitTo` functions for the `payload` instead of the previously used type (`unknown`).

View File

@@ -174,7 +174,7 @@ async function once<T>(
*
* @since 1.0.0
*/
async function emit(event: string, payload?: unknown): Promise<void> {
async function emit<T>(event: string, payload?: T): Promise<void> {
await invoke('plugin:event|emit', {
event,
payload
@@ -196,10 +196,10 @@ async function emit(event: string, payload?: unknown): Promise<void> {
*
* @since 2.0.0
*/
async function emitTo(
async function emitTo<T>(
target: EventTarget | string,
event: string,
payload?: unknown
payload?: T
): Promise<void> {
const eventTarget: EventTarget =
typeof target === 'string' ? { kind: 'AnyLabel', label: target } : target

View File

@@ -291,7 +291,7 @@ class Webview {
* @param event Event name. Must include only alphanumeric characters, `-`, `/`, `:` and `_`.
* @param payload Event payload.
*/
async emit(event: string, payload?: unknown): Promise<void> {
async emit<T>(event: string, payload?: T): Promise<void> {
if (localTauriEvents.includes(event)) {
// eslint-disable-next-line
for (const handler of this.listeners[event] || []) {
@@ -303,7 +303,7 @@ class Webview {
}
return
}
return emit(event, payload)
return emit<T>(event, payload)
}
/**
@@ -319,10 +319,10 @@ class Webview {
* @param event Event name. Must include only alphanumeric characters, `-`, `/`, `:` and `_`.
* @param payload Event payload.
*/
async emitTo(
async emitTo<T>(
target: string | EventTarget,
event: string,
payload?: unknown
payload?: T
): Promise<void> {
if (localTauriEvents.includes(event)) {
// eslint-disable-next-line
@@ -335,7 +335,7 @@ class Webview {
}
return
}
return emitTo(target, event, payload)
return emitTo<T>(target, event, payload)
}
/** @ignore */

View File

@@ -441,7 +441,7 @@ class Window {
* @param event Event name. Must include only alphanumeric characters, `-`, `/`, `:` and `_`.
* @param payload Event payload.
*/
async emit(event: string, payload?: unknown): Promise<void> {
async emit<T>(event: string, payload?: T): Promise<void> {
if (localTauriEvents.includes(event)) {
// eslint-disable-next-line
for (const handler of this.listeners[event] || []) {
@@ -453,7 +453,7 @@ class Window {
}
return
}
return emit(event, payload)
return emit<T>(event, payload)
}
/**
@@ -468,10 +468,10 @@ class Window {
* @param event Event name. Must include only alphanumeric characters, `-`, `/`, `:` and `_`.
* @param payload Event payload.
*/
async emitTo(
async emitTo<T>(
target: string | EventTarget,
event: string,
payload?: unknown
payload?: T
): Promise<void> {
if (localTauriEvents.includes(event)) {
// eslint-disable-next-line security/detect-object-injection
@@ -484,7 +484,7 @@ class Window {
}
return
}
return emitTo(target, event, payload)
return emitTo<T>(target, event, payload)
}
/** @ignore */