2020-06-27 18:20:00 +03:00
|
|
|
import { invoke, transformCallback } from './tauri'
|
2020-07-13 01:34:44 +03:00
|
|
|
|
|
|
|
export interface Event<T> {
|
|
|
|
type: string
|
|
|
|
payload: T
|
|
|
|
}
|
|
|
|
|
|
|
|
export type EventCallback<T> = (event: Event<T>) => void
|
2020-06-27 18:20:00 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* listen to an event from the backend
|
|
|
|
*
|
|
|
|
* @param event the event name
|
|
|
|
* @param handler the event handler callback
|
|
|
|
*/
|
2020-07-04 21:07:45 +03:00
|
|
|
function listen<T>(event: string, handler: EventCallback<T>, once = false): void {
|
2020-06-27 18:20:00 +03:00
|
|
|
invoke({
|
|
|
|
cmd: 'listen',
|
|
|
|
event,
|
|
|
|
handler: transformCallback(handler, once),
|
|
|
|
once
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* emits an event to the backend
|
|
|
|
*
|
|
|
|
* @param event the event name
|
|
|
|
* @param [payload] the event payload
|
|
|
|
*/
|
|
|
|
function emit(event: string, payload?: string): void {
|
|
|
|
invoke({
|
|
|
|
cmd: 'emit',
|
|
|
|
event,
|
|
|
|
payload
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
export {
|
|
|
|
listen,
|
|
|
|
emit
|
|
|
|
}
|