Skip to content

Commit 0600099

Browse files
authored
feat(api): support unknown types for event emit payload, closes #2929 (#2964)
1 parent 983cecb commit 0600099

5 files changed

Lines changed: 12 additions & 7 deletions

File tree

.changes/api-emit-payload-type.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"api": patch
3+
---
4+
5+
Event `emit` now automatically serialize non-string types.

core/tauri/scripts/bundle.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tooling/api/src/event.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,8 @@ async function once<T>(
109109
* @param [payload] Event payload
110110
* @returns
111111
*/
112-
async function emit(event: string, payload?: string): Promise<void> {
113-
return emitEvent(event, undefined, payload)
112+
async function emit(event: string, payload?: unknown): Promise<void> {
113+
return emitEvent(event, null, payload)
114114
}
115115

116116
export type { Event, EventName, EventCallback, UnlistenFn }

tooling/api/src/helpers/event.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,16 @@ import { invokeTauriCommand } from './tauri'
1515
*/
1616
async function emit(
1717
event: string,
18-
windowLabel: WindowLabel,
19-
payload?: string
18+
windowLabel: WindowLabel | null,
19+
payload?: unknown
2020
): Promise<void> {
2121
await invokeTauriCommand({
2222
__tauriModule: 'Event',
2323
message: {
2424
cmd: 'emit',
2525
event,
2626
windowLabel,
27-
payload
27+
payload: typeof payload === 'string' ? payload : JSON.stringify(payload)
2828
}
2929
})
3030
}

tooling/api/src/window.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ class WebviewWindowHandle {
283283
* @param event Event name.
284284
* @param payload Event payload.
285285
*/
286-
async emit(event: string, payload?: string): Promise<void> {
286+
async emit(event: string, payload?: unknown): Promise<void> {
287287
if (localTauriEvents.includes(event)) {
288288
// eslint-disable-next-line
289289
for (const handler of this.listeners[event] || []) {

0 commit comments

Comments
 (0)