Skip to content

Commit

Permalink
Fix Double Event
Browse files Browse the repository at this point in the history
- Removed: webui_interface_get_bind_id()
- Add: webui_event_t -> size_t bind_id

Wrappers that have internal-event-handler may call by mistake the same callback twice when app use all-events. To fix this issue, simply wrappers should replace `webui_interface_get_window_id()` by `e.bind_id`.
  • Loading branch information
hassandraga committed Oct 6, 2023
1 parent 7da0677 commit 6d5612c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 34 deletions.
4 changes: 1 addition & 3 deletions include/webui.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ typedef struct webui_event_t {
size_t event_type; // Event type
char* element; // HTML element ID
size_t event_number; // Internal WebUI
size_t bind_id; // Bind ID
} webui_event_t;

// -- Definitions ---------------------
Expand Down Expand Up @@ -544,7 +545,4 @@ WEBUI_EXPORT bool webui_interface_is_app_running(void);
// Get a unique window ID.
WEBUI_EXPORT size_t webui_interface_get_window_id(size_t window);

// Get a unique ID. Same ID as `webui_bind()`. Return > 0 if bind exist.
WEBUI_EXPORT size_t webui_interface_get_bind_id(size_t window, const char* element);

#endif /* _WEBUI_H */
39 changes: 8 additions & 31 deletions src/webui.c
Original file line number Diff line number Diff line change
Expand Up @@ -1920,6 +1920,7 @@ static void _webui_interface_bind_handler(webui_event_t* e) {
#ifdef WEBUI_LOG
printf("[Core]\t\t_webui_interface_bind_handler() -> Calling user callback...\n[Call]\n");
#endif
e->bind_id = cb_index;
_webui_core.cb_interface[cb_index](e->window, e->event_type, e->element, e->event_number);
}

Expand Down Expand Up @@ -2032,33 +2033,6 @@ size_t webui_interface_get_window_id(size_t window) {
return win->window_number;
}

size_t webui_interface_get_bind_id(size_t window, const char* element) {

#ifdef WEBUI_LOG
printf("[User] webui_interface_get_bind_id([%zu], [%s])...\n", window, element);
#endif

// Initialization
_webui_init();

// Dereference
if (_webui_core.exit_now || _webui_core.wins[window] == NULL) return 0;
_webui_window_t* win = _webui_core.wins[window];

size_t len = _webui_strlen(element);
if (len < 1)
element = "";

// [win num][/][element]
char* webui_internal_id = _webui_malloc(3 + 1 + len);
sprintf(webui_internal_id, "%zu/%s", win->window_number, element);

size_t cb_index = _webui_get_cb_index(webui_internal_id);

_webui_free_mem((void*)webui_internal_id);
return cb_index;
}

// -- Core's Functions ----------------
static bool _webui_ptr_exist(void* ptr) {

Expand Down Expand Up @@ -3273,7 +3247,7 @@ static void _webui_delete_folder(char* folder) {
static char* _webui_generate_internal_id(_webui_window_t* win, const char* element) {

#ifdef WEBUI_LOG
printf("[Core]\t\t_webui_generate_internal_id([%s])...\n", element);
printf("[Core]\t\t_webui_generate_internal_id([%zu], [%s])...\n", win->window_number, element);
#endif

// Generate WebUI internal id
Expand Down Expand Up @@ -4867,7 +4841,7 @@ static bool _webui_show_window(_webui_window_t* win, const char* content, bool i
win->server_port = port;
win->ws_port = ws_port;

if (!webui_is_shown(win->window_number)) {
if (!win->connected) {

// Start a new window

Expand Down Expand Up @@ -4935,10 +4909,11 @@ static void _webui_window_event(_webui_window_t* win, int event_type, char* elem

if (events_cb_index > 0 && _webui_core.cb[events_cb_index] != NULL) {

// Call user all events cb
// Call user all-events cb
#ifdef WEBUI_LOG
printf("[Core]\t\t_webui_window_event() -> Calling user callback...\n[Call]\n");
printf("[Core]\t\t_webui_window_event() -> Calling all-events user callback...\n[Call]\n");
#endif
e.bind_id = events_cb_index;
_webui_core.cb[events_cb_index](&e);
}
}
Expand All @@ -4955,6 +4930,7 @@ static void _webui_window_event(_webui_window_t* win, int event_type, char* elem
#ifdef WEBUI_LOG
printf("[Core]\t\t_webui_window_event() -> Calling user callback...\n[Call]\n");
#endif
e.bind_id = cb_index;
_webui_core.cb[cb_index](&e);
}
}
Expand Down Expand Up @@ -6263,6 +6239,7 @@ static WEBUI_THREAD_RECEIVE
#ifdef WEBUI_LOG
printf("[Core]\t\t[Thread %zu] _webui_receive_thread() -> Calling user callback...\n[Call]\n", recvNum);
#endif
e.bind_id = cb_index;
_webui_core.cb[cb_index](&e);
}

Expand Down

0 comments on commit 6d5612c

Please sign in to comment.