webui-dev / webui

Use any web browser or WebView as GUI, with your preferred language in the backend and HTML5 in the frontend, all in a lightweight portable lib.
https://webui.me
MIT License
2.36k stars 144 forks source link

How to unbind an event? #360

Open xland opened 1 month ago

xland commented 1 month ago

When I bind a callback method using webui_bind, How do I unbind it?

I know that webui_bind returns a unique bind ID. But what is the use of this ID? Isn't it used to unbind?

fibodevy commented 1 month ago

You cant unbind it, but you can "ignore" events for specific IDs in your app

AlbertShown commented 1 month ago

How do I unbind it?

The webui.js get generated at runtime based on your bind elements, and you can not unbind it. But, as @fibodevy said, you can easily ignore it in your back-end app.

what is the use of this ID?

Many wrappers has an internal table to map events with back-end functions, like Python wrapper, those wrappers use those IDs to identify the back-end function. So, you can save the IDs if you want and ignore their events later when you need, or use .element to get the bind name (element name) instead of ID, whatever you prefer.

ttytm commented 1 month ago

Yes, as you mention @xland, the call to webui_bind returns the ID. It is used e.g. to identify a function in an event. This would also allow to conditionally ignore the bound function when a state is reached where it is necessary, so you can store the ID and keep it in the relevant context.

Maybe you can share an example use-case for unbinding, then things can be further concertized.

ttytm commented 1 month ago

Regarding providing an example. An API for unbinding could be provided theoretically. So if there is a good use-case maybe its gonna be added.

xland commented 1 month ago

I didn't delve into the webui code. So I'm not sure if it's necessary to add the webui_unbind API.

Let's say I'm going to keep creating btn in HTML : <button id='btn_n'>btn_n</button>. And keep binding events to those buttons : webui_bind(winId, "btn_n", same_function); Then keep removing the previously created Button : document.getElementById("btn_n").remove();

This could be what happens when the user switches between tab pages. btn_1 may be repeatedly bound to an event. btn_999999... may be bound to an event.

Does this cause OOM exception?

hassandraga commented 1 month ago

A propre unbind will be removing the ID from the core's table, and from webui.js as well, new internal cmd is needed, and a new API. Need time to implement, but good to have.

Thank you @xland for the suggestion.