Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(api): support unknown types for event emit payload, closes #2929 #2964

Merged
merged 2 commits into from
Jan 7, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changes/api-emit-payload-type.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"api": patch
---

Event `emit` now automatically serialize non-string types.
2 changes: 1 addition & 1 deletion core/tauri/scripts/bundle.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions tooling/api/src/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ async function once<T>(
* @param [payload] Event payload
* @returns
*/
async function emit(event: string, payload?: string): Promise<void> {
return emitEvent(event, undefined, payload)
async function emit(event: string, payload?: unknown): Promise<void> {
return emitEvent(event, null, payload)
}

export type { Event, EventName, EventCallback, UnlistenFn }
Expand Down
6 changes: 3 additions & 3 deletions tooling/api/src/helpers/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@ import { invokeTauriCommand } from './tauri'
*/
async function emit(
event: string,
windowLabel: WindowLabel,
payload?: string
windowLabel: WindowLabel | null,
payload?: unknown
): Promise<void> {
await invokeTauriCommand({
__tauriModule: 'Event',
message: {
cmd: 'emit',
event,
windowLabel,
payload
payload: typeof payload === 'string' ? payload : JSON.stringify(payload)
}
})
}
Expand Down
2 changes: 1 addition & 1 deletion tooling/api/src/window.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ class WebviewWindowHandle {
* @param event Event name.
* @param payload Event payload.
*/
async emit(event: string, payload?: string): Promise<void> {
async emit(event: string, payload?: unknown): Promise<void> {
if (localTauriEvents.includes(event)) {
// eslint-disable-next-line
for (const handler of this.listeners[event] || []) {
Expand Down