webui icon indicating copy to clipboard operation
webui copied to clipboard

How to unbind an event?

Open xland opened this issue 1 year ago • 6 comments

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?

xland avatar May 22 '24 00:05 xland

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

fibodevy avatar May 22 '24 00:05 fibodevy

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.

AlbertShown avatar May 22 '24 11:05 AlbertShown

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 avatar May 22 '24 12:05 ttytm

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.

ttytm avatar May 27 '24 11:05 ttytm

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?

xland avatar May 28 '24 00:05 xland

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.

hassandraga avatar May 31 '24 23:05 hassandraga

Better to just ignore bind events with this ID.

void events(webui_event_t* e) {

    //     size_t window;          // The window object number
    //     size_t event_type;      // Event type
    //     char* element;          // HTML element ID
    //     size_t event_number;    // Internal WebUI
    //     size_t bind_id;         // Bind ID
    //     size_t client_id;       // Client's unique ID
    //     size_t connection_id;   // Client's connection ID
    //     char* cookies;          // Client's full cookies

    if (e->bind_id ....... )
        printf("ignoring this ID.... \n");
}

AlbertShown avatar Jan 29 '25 19:01 AlbertShown