feat(api): add abstractions to updater and window event listeners#4569
feat(api): add abstractions to updater and window event listeners#4569lucasfernog merged 7 commits intodevfrom
Conversation
|
@FabianLars is this what you had in mind? :P |
|
Well initially i just wanted better docs, but you had me intrigued with this idea and you were right, this is indeed wayyy better, especially for typescript users because they don't have to handle the types themselves with this. I like it! 👍 I liked that it used to be uncoupled from appWindow tho, but i guess that's a fair trade-off (and it's not like you can't use the old way anymore) |
|
what do you mean by |
|
@JonasKruckenberg I've followed #3731 here, anything you want to change? |
For me it's just Edit: Oh another "side-effect" that somewhat came up on discord is that this abstraction makes it less obvious that you have to unlisten when the component unmounts (at least in React) |
|
From an API perspective the events are tied to the appWindow though, so they'll have to be a little smart with the bundler config to only import appWindow for tauri builds - or inject dummy values. Why does it make it "less obvious"? You're still just listening to an event - and it still returns the unlisten function. |
There was a problem hiding this comment.
If you're goal here is to improve the DX especially when using TS, might I suggest doing a simple overload of the listen instead? All these separate handlers add a lot of code while not really feeling all that iditomatic
async listen(event: 'tauri://scale-change', handler: EventCallback<ScaleFactorChanged>): Promise<UnlistenFn>
async listen(event: 'tauri://menu', handler: EventCallback<string>): Promise<UnlistenFn>
async listen(event: 'tauri://file-drop', handler: EventCallback<{ type: 'drop', paths: string[] }>): Promise<UnlistenFn>
async listen(event: 'tauri://file-drop-hover', handler: EventCallback<{ type: 'hover', paths: string[] }>): Promise<UnlistenFn>
async listen(event: 'tauri://file-drop-cancelled', handler: EventCallback<{ type: 'cancel' }>): Promise<UnlistenFn>
async listen(event: 'tauri://theme-changed', handler: EventCallback<Theme>): Promise<UnlistenFn>
async listen(event: 'tauri://close-requested', handler: EventCallback<CloseRequestedEvent>): Promise<UnlistenFn>
async listen<T>(event: EventName, handler: EventCallback<T>): Promise<UnlistenFn> {
// actual implementation
}| shouldUpdate: boolean | ||
| } | ||
|
|
||
| async function onUpdaterEvent( |
There was a problem hiding this comment.
Ideally this would be one object updater which has methods and is an event emitter, so people can use the proper .addEventListener('<updater-event>', () => {}) JavaScript idiomatic style. But for the same of not breaking this this is pretty good 👍🏻
There was a problem hiding this comment.
let's add that to the v2 todo list 😂
|
It adds a lot of code on our end, but it reduces the boilerplate on the devs code. Also it'll look better in the documentation page and it'll be easier to search (I think). |
Hmmmm yeah I see that 🤔 |
| * @param handler | ||
| * @returns A promise resolving to a function to unlisten to the event. | ||
| */ | ||
| async onFileDropEvent( |
There was a problem hiding this comment.
Wdyt about making FileDropEventa proper DragEvent instead? https://developer.mozilla.org/en-US/docs/Web/API/DragEvent
There was a problem hiding this comment.
I think we can file a feature request for that.
| * @returns A promise resolving to a function to unlisten to the event. | ||
| */ | ||
| async onCloseRequested( | ||
| handler: (event: CloseRequestedEvent) => void |
There was a problem hiding this comment.
| handler: (event: CloseRequestedEvent) => void | |
| handler: EventCallback<CloseRequestedEvent> |
There was a problem hiding this comment.
These are not the same, EventCallback<CloseRequestedEvent> translates to (event: Event<CloseRequestedEvent>) => void and we're wrapping the event fields in the CloseRequestedEvent directly (just to add the preventDefault method).
| * @param handler | ||
| * @returns A promise resolving to a function to unlisten to the event. | ||
| */ | ||
| async onFocusChanged(handler: EventCallback<boolean>): Promise<UnlistenFn> { |
There was a problem hiding this comment.
Same here, shouldn't we be reusing DOM concepts as much as possible? I.e. make this FocusEvent https://developer.mozilla.org/en-US/docs/Web/API/FocusEvent with relatedTarget set to null
There was a problem hiding this comment.
I think we can file a feature request for that.
|
Only have one real change suggestion and two ideas for improvements/aliging with web standards that are not blocking |
Yeah just a really minor "drawback" (wouldn't even call it that), but again, nothing to worry/think about 🤷
I know how stupid it sounds, but "less obvious" because it moves further away from |
|
I think we should add a note for it on every listen function lol |
|
@FabianLars better with the docs change now? |
|
Yep, i like it 👍 |
What kind of change does this PR introduce?
Does this PR introduce a breaking change?
Checklist
fix: remove a typo, closes #___, #___)Other information