Explorar el Código

fix(tauri.js) event payload type on TS API (#746)

Lucas Fernandes Nogueira hace 5 años
padre
commit
61437b3b3d

+ 1 - 1
cli/tauri.js/api-src/event.ts

@@ -7,7 +7,7 @@ import { EventCallback } from './types/event'
  * @param event the event name
  * @param handler the event handler callback
  */
-function listen(event: string, handler: EventCallback, once = false): void {
+function listen<T>(event: string, handler: EventCallback<T>, once = false): void {
   invoke({
     cmd: 'listen',
     event,

+ 3 - 3
cli/tauri.js/api-src/types/event.ts

@@ -1,6 +1,6 @@
-export interface Event {
+export interface Event<T> {
   type: string
-  payload: unknown
+  payload: T
 }
 
-export type EventCallback = (event: Event) => void
+export type EventCallback<T> = (event: Event<T>) => void

+ 2 - 5
tauri/examples/communication/src-tauri/src/main.rs

@@ -22,11 +22,8 @@ fn main() {
           data: "something else".to_string(),
         };
 
-        tauri::event::emit(
-          &handle,
-          String::from("rust-event"),
-          Some(serde_json::to_string(&reply).unwrap()),
-        );
+        tauri::event::emit(&handle, String::from("rust-event"), Some(reply))
+          .expect("failed to emit");
       });
     })
     .invoke_handler(|_webview, arg| {