Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .changes/change-pr-13066.md
Original file line number Diff line number Diff line change
@@ -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`).
6 changes: 3 additions & 3 deletions packages/api/src/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
10 changes: 5 additions & 5 deletions packages/api/src/webview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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] || []) {
Expand All @@ -303,7 +303,7 @@ class Webview {
}
return
}
return emit(event, payload)
return emit<T>(event, payload)
}

/**
Expand All @@ -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
Expand All @@ -335,7 +335,7 @@ class Webview {
}
return
}
return emitTo(target, event, payload)
return emitTo<T>(target, event, payload)
}

/** @ignore */
Expand Down
10 changes: 5 additions & 5 deletions packages/api/src/window.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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] || []) {
Expand All @@ -453,7 +453,7 @@ class Window {
}
return
}
return emit(event, payload)
return emit<T>(event, payload)
}

/**
Expand All @@ -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
Expand All @@ -484,7 +484,7 @@ class Window {
}
return
}
return emitTo(target, event, payload)
return emitTo<T>(target, event, payload)
}

/** @ignore */
Expand Down