-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Add a way to remove a single event listener (#20983) #25535
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ypujante
wants to merge
4
commits into
emscripten-core:main
Choose a base branch
from
ypujante:issue-20983
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+207
−0
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
/* | ||
* Copyright 2025 The Emscripten Authors. All rights reserved. | ||
* Emscripten is available under two separate licenses, the MIT license and the | ||
* University of Illinois/NCSA Open Source License. Both these licenses can be | ||
* found in the LICENSE file. | ||
*/ | ||
|
||
#include <assert.h> | ||
#include <stdio.h> | ||
#include <emscripten.h> | ||
#include <string.h> | ||
#include <emscripten/html5.h> | ||
|
||
const char *emscripten_result_to_string(EMSCRIPTEN_RESULT result) { | ||
if (result == EMSCRIPTEN_RESULT_SUCCESS) return "EMSCRIPTEN_RESULT_SUCCESS"; | ||
if (result == EMSCRIPTEN_RESULT_DEFERRED) return "EMSCRIPTEN_RESULT_DEFERRED"; | ||
if (result == EMSCRIPTEN_RESULT_NOT_SUPPORTED) return "EMSCRIPTEN_RESULT_NOT_SUPPORTED"; | ||
if (result == EMSCRIPTEN_RESULT_FAILED_NOT_DEFERRED) return "EMSCRIPTEN_RESULT_FAILED_NOT_DEFERRED"; | ||
if (result == EMSCRIPTEN_RESULT_INVALID_TARGET) return "EMSCRIPTEN_RESULT_INVALID_TARGET"; | ||
if (result == EMSCRIPTEN_RESULT_UNKNOWN_TARGET) return "EMSCRIPTEN_RESULT_UNKNOWN_TARGET"; | ||
if (result == EMSCRIPTEN_RESULT_INVALID_PARAM) return "EMSCRIPTEN_RESULT_INVALID_PARAM"; | ||
if (result == EMSCRIPTEN_RESULT_FAILED) return "EMSCRIPTEN_RESULT_FAILED"; | ||
if (result == EMSCRIPTEN_RESULT_NO_DATA) return "EMSCRIPTEN_RESULT_NO_DATA"; | ||
return "Unknown EMSCRIPTEN_RESULT!"; | ||
} | ||
|
||
// Report API failure | ||
#define TEST_RESULT(x) if (ret != EMSCRIPTEN_RESULT_SUCCESS) printf("%s returned %s.\n", #x, emscripten_result_to_string(ret)); | ||
|
||
// Like above above but also assert API success | ||
#define ASSERT_RESULT(x) TEST_RESULT(x); assert(ret == EMSCRIPTEN_RESULT_SUCCESS); | ||
|
||
char const *userDataToString(void *userData) { | ||
return userData ? (char const *) userData : "nullptr"; | ||
} | ||
|
||
bool key_callback_1(int eventType, const EmscriptenKeyboardEvent *e, void *userData) { | ||
printf("key_callback_1: eventType=%d, userData=%s\n", eventType, userDataToString(userData)); | ||
return 0; | ||
} | ||
|
||
bool key_callback_2(int eventType, const EmscriptenKeyboardEvent *e, void *userData) { | ||
printf("key_callback_2: eventType=%d, userData=%s\n", eventType, userDataToString(userData)); | ||
return 0; | ||
} | ||
|
||
bool mouse_callback_1(int eventType, const EmscriptenMouseEvent *e, void *userData) { | ||
printf("mouse_callback_1: eventType=%d, userData=%s\n", eventType, userDataToString(userData)); | ||
return 0; | ||
} | ||
|
||
void checkCount(int count) | ||
{ | ||
int eventHandlersCount = EM_ASM_INT({ return JSEvents.eventHandlers.length; }); | ||
printf("Detected [%d] handlers\n", eventHandlersCount); | ||
assert(count == eventHandlersCount); | ||
} | ||
|
||
void test_done() {} | ||
|
||
int main() { | ||
bool useCapture = true; | ||
void *userData3 = "3"; | ||
|
||
// first we check for invalid parameters | ||
assert(emscripten_remove_callback("this_dom_element_does_not_exist", NULL, 0, key_callback_1) == EMSCRIPTEN_RESULT_UNKNOWN_TARGET); | ||
|
||
checkCount(0); | ||
|
||
EMSCRIPTEN_RESULT ret = emscripten_set_keypress_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, NULL, useCapture, key_callback_1); | ||
ASSERT_RESULT(emscripten_set_keypress_callback); | ||
ret = emscripten_set_keydown_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, NULL, useCapture, key_callback_1); | ||
ASSERT_RESULT(emscripten_set_keydown_callback); | ||
ret = emscripten_set_keyup_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, NULL, useCapture, key_callback_1); | ||
ASSERT_RESULT(emscripten_set_keyup_callback); | ||
|
||
checkCount(3); | ||
|
||
// removing keydown event | ||
ret = emscripten_remove_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, NULL, EMSCRIPTEN_EVENT_KEYDOWN, key_callback_1); | ||
ASSERT_RESULT(emscripten_remove_callback); | ||
|
||
checkCount(2); | ||
|
||
// adding another keypress callback on the same target | ||
ret = emscripten_set_keypress_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, NULL, useCapture, key_callback_2); | ||
ASSERT_RESULT(emscripten_set_keypress_callback); | ||
|
||
checkCount(3); | ||
|
||
// adding another keypress callback on the same target with different user data | ||
ret = emscripten_set_keypress_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, userData3, useCapture, key_callback_2); | ||
ASSERT_RESULT(emscripten_set_keypress_callback); | ||
|
||
checkCount(4); | ||
|
||
// removing a combination that does not exist (no mouse_callback_1 registered) | ||
ret = emscripten_remove_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, NULL, EMSCRIPTEN_EVENT_KEYPRESS, mouse_callback_1); | ||
ASSERT_RESULT(emscripten_remove_callback); | ||
|
||
checkCount(4); | ||
|
||
// removing keypress / userData=NULL / key_callback_2 | ||
ret = emscripten_remove_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, NULL, EMSCRIPTEN_EVENT_KEYPRESS, key_callback_2); | ||
ASSERT_RESULT(emscripten_remove_callback); | ||
|
||
checkCount(3); | ||
|
||
// removing keypress / userData=NULL / key_callback_1 | ||
ret = emscripten_remove_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, NULL, EMSCRIPTEN_EVENT_KEYPRESS, key_callback_1); | ||
ASSERT_RESULT(emscripten_remove_callback); | ||
|
||
checkCount(2); | ||
|
||
// removing keypress / userData=3 / key_callback_2 | ||
ret = emscripten_remove_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, userData3, EMSCRIPTEN_EVENT_KEYPRESS, key_callback_2); | ||
ASSERT_RESULT(emscripten_remove_callback); | ||
|
||
checkCount(1); | ||
|
||
// adding the same mouse down callback to 2 different targets | ||
ret = emscripten_set_mousedown_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, NULL, useCapture, mouse_callback_1); | ||
ASSERT_RESULT(emscripten_set_mousedown_callback); | ||
ret = emscripten_set_mousedown_callback("#canvas", NULL, useCapture, mouse_callback_1); | ||
ASSERT_RESULT(emscripten_set_mousedown_callback); | ||
|
||
checkCount(3); | ||
|
||
// removing mousedown / userData=NULL / mouse_callback_1 on the window target | ||
ret = emscripten_remove_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, NULL, EMSCRIPTEN_EVENT_MOUSEDOWN, mouse_callback_1); | ||
ASSERT_RESULT(emscripten_remove_callback); | ||
|
||
checkCount(2); | ||
|
||
return 0; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't really love all the repetition here, or the fact that we need to store two extra fields just for this new API, but I guess it makes sense, and the repetitive nature of this file is kind of a pre-existing condition :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree. I don't love it either, but you are right, this code is replicated over and over (17 times!). I feel like the API could have been simplified in the first place (having just 1 function call for all the calls using the same callback type and providing the eventTypeId... which would have reduced, for example, 9 mouse related calls into just 1...). It is what is now unfortunately. So that is the only solution I can think of to be able to implement this new API.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let me correct what I just said in regards to the count. There are indeed 17 places where I had to change the code, but there are way many more "emscripten_set_xxx_callback". There are 17 types of events supported, so we could not really simplyif this since they all have different callback (function pointer) types.
The example I gave for mouse counts as just 1 on these 17 places because there are 9 "emscripten_set_mousexxx_callback" functions which all funnel into 1
registerMouseEventCallback
function call. I feel like this API is way too verbose and having just oneemscripten_set_mouse_callback
with the eventTypeId as a parameter (which is exactly what the internalregisterMouseEventCallback
is) would have been plenty in the first place...