Skip to content

Commit dd13728

Browse files
authored
enhance(api): add generic parameter to emit and emitTo functions (#13066)
closes #13059
1 parent f235ec0 commit dd13728

File tree

4 files changed

+17
-13
lines changed

4 files changed

+17
-13
lines changed

.changes/change-pr-13066.md

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

packages/api/src/event.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ async function once<T>(
174174
*
175175
* @since 1.0.0
176176
*/
177-
async function emit(event: string, payload?: unknown): Promise<void> {
177+
async function emit<T>(event: string, payload?: T): Promise<void> {
178178
await invoke('plugin:event|emit', {
179179
event,
180180
payload
@@ -196,10 +196,10 @@ async function emit(event: string, payload?: unknown): Promise<void> {
196196
*
197197
* @since 2.0.0
198198
*/
199-
async function emitTo(
199+
async function emitTo<T>(
200200
target: EventTarget | string,
201201
event: string,
202-
payload?: unknown
202+
payload?: T
203203
): Promise<void> {
204204
const eventTarget: EventTarget =
205205
typeof target === 'string' ? { kind: 'AnyLabel', label: target } : target

packages/api/src/webview.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ class Webview {
291291
* @param event Event name. Must include only alphanumeric characters, `-`, `/`, `:` and `_`.
292292
* @param payload Event payload.
293293
*/
294-
async emit(event: string, payload?: unknown): Promise<void> {
294+
async emit<T>(event: string, payload?: T): Promise<void> {
295295
if (localTauriEvents.includes(event)) {
296296
// eslint-disable-next-line
297297
for (const handler of this.listeners[event] || []) {
@@ -303,7 +303,7 @@ class Webview {
303303
}
304304
return
305305
}
306-
return emit(event, payload)
306+
return emit<T>(event, payload)
307307
}
308308

309309
/**
@@ -319,10 +319,10 @@ class Webview {
319319
* @param event Event name. Must include only alphanumeric characters, `-`, `/`, `:` and `_`.
320320
* @param payload Event payload.
321321
*/
322-
async emitTo(
322+
async emitTo<T>(
323323
target: string | EventTarget,
324324
event: string,
325-
payload?: unknown
325+
payload?: T
326326
): Promise<void> {
327327
if (localTauriEvents.includes(event)) {
328328
// eslint-disable-next-line
@@ -335,7 +335,7 @@ class Webview {
335335
}
336336
return
337337
}
338-
return emitTo(target, event, payload)
338+
return emitTo<T>(target, event, payload)
339339
}
340340

341341
/** @ignore */

packages/api/src/window.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,7 @@ class Window {
441441
* @param event Event name. Must include only alphanumeric characters, `-`, `/`, `:` and `_`.
442442
* @param payload Event payload.
443443
*/
444-
async emit(event: string, payload?: unknown): Promise<void> {
444+
async emit<T>(event: string, payload?: T): Promise<void> {
445445
if (localTauriEvents.includes(event)) {
446446
// eslint-disable-next-line
447447
for (const handler of this.listeners[event] || []) {
@@ -453,7 +453,7 @@ class Window {
453453
}
454454
return
455455
}
456-
return emit(event, payload)
456+
return emit<T>(event, payload)
457457
}
458458

459459
/**
@@ -468,10 +468,10 @@ class Window {
468468
* @param event Event name. Must include only alphanumeric characters, `-`, `/`, `:` and `_`.
469469
* @param payload Event payload.
470470
*/
471-
async emitTo(
471+
async emitTo<T>(
472472
target: string | EventTarget,
473473
event: string,
474-
payload?: unknown
474+
payload?: T
475475
): Promise<void> {
476476
if (localTauriEvents.includes(event)) {
477477
// eslint-disable-next-line security/detect-object-injection
@@ -484,7 +484,7 @@ class Window {
484484
}
485485
return
486486
}
487-
return emitTo(target, event, payload)
487+
return emitTo<T>(target, event, payload)
488488
}
489489

490490
/** @ignore */

0 commit comments

Comments
 (0)