Skip to content

Commit b7599da

Browse files
authored
Cleanup JSEvents in library_html5.js (#21019)
- Only define inEventHandler when needed - Call registerRemoveEventListeners only when EXIT_RUNTIME is set. Both MINIMAL_RUNTIME and the regular runtime support __ATEXIT__ if and only if EXIT_RUNTIME is set. - Wrap some long comments - Avoid extra local when creating jsEventHandler
1 parent 04b4d32 commit b7599da

File tree

1 file changed

+49
-48
lines changed

1 file changed

+49
-48
lines changed

src/library_html5.js

Lines changed: 49 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@ var LibraryHTML5 = {
1212
],
1313
$JSEvents: {
1414

15-
/* We do not depend on the exact initial values of falsey member fields - these fields can be populated on-demand
16-
to save code size.
15+
/* We do not depend on the exact initial values of falsey member fields - these
16+
fields can be populated on-demand to save code size.
1717
(but still documented here to keep track of what is supposed to be present)
18+
1819
// pointers to structs malloc()ed to Emscripten HEAP for JS->C interop.
1920
keyEvent: 0,
2021
mouseEvent: 0,
@@ -28,42 +29,41 @@ var LibraryHTML5 = {
2829
visibilityChangeEvent: 0,
2930
touchEvent: 0,
3031
31-
// When we transition from fullscreen to windowed mode, we remember here the element that was just in fullscreen mode
32-
// so that we can report information about that element in the event message.
32+
// When we transition from fullscreen to windowed mode, we remember here the
33+
// element that was just in fullscreen mode so that we can report
34+
// information about that element in the event message.
3335
previousFullscreenElement: null,
3436
3537
#if MIN_SAFARI_VERSION <= 80000 || MIN_CHROME_VERSION <= 21 // https://caniuse.com/#search=movementX
36-
// Remember the current mouse coordinates in case we need to emulate movementXY generation for browsers that don't support it.
38+
// Remember the current mouse coordinates in case we need to emulate
39+
// movementXY generation for browsers that don't support it.
3740
// Some browsers (e.g. Safari 6.0.5) only give movementXY when Pointerlock is active.
3841
previousScreenX: null,
3942
previousScreenY: null,
4043
#endif
4144
42-
// When the C runtime exits via exit(), we unregister all event handlers added by this library to be nice and clean.
45+
// When the C runtime exits via exit(), we unregister all event handlers
46+
// added by this library to be nice and clean.
4347
// Track in this field whether we have yet registered that __ATEXIT__ handler.
4448
removeEventListenersRegistered: false,
4549
4650
#if HTML5_SUPPORT_DEFERRING_USER_SENSITIVE_REQUESTS
47-
// If we are in an event handler, specifies the event handler object from the eventHandlers array that is currently running.
51+
// If we are in an event handler, specifies the event handler object from
52+
// the eventHandlers array that is currently running.
4853
currentEventHandler: null,
4954
#endif
5055
*/
5156

52-
// If positive, we are currently executing in a JS event handler.
53-
// (this particular property must be initialized to zero, as we ++/-- it)
54-
inEventHandler: 0,
55-
5657
removeAllEventListeners() {
57-
for (var i = JSEvents.eventHandlers.length-1; i >= 0; --i) {
58-
JSEvents._removeHandler(i);
58+
while (JSEvents.eventHandlers.length) {
59+
JSEvents._removeHandler(JSEvents.eventHandlers.length - 1);
5960
}
60-
JSEvents.eventHandlers = [];
6161
#if HTML5_SUPPORT_DEFERRING_USER_SENSITIVE_REQUESTS
6262
JSEvents.deferredCalls = [];
6363
#endif
6464
},
6565

66-
#if !MINIMAL_RUNTIME || EXIT_RUNTIME // In minimal runtime, there is no concept of the page running vs being closed, and hence __ATEXIT__ is not present
66+
#if EXIT_RUNTIME
6767
registerRemoveEventListeners() {
6868
if (!JSEvents.removeEventListenersRegistered) {
6969
__ATEXIT__.push(JSEvents.removeAllEventListeners);
@@ -73,6 +73,10 @@ var LibraryHTML5 = {
7373
#endif
7474

7575
#if HTML5_SUPPORT_DEFERRING_USER_SENSITIVE_REQUESTS
76+
// If positive, we are currently executing in a JS event handler.
77+
// (this particular property must be initialized to zero, as we ++/-- it)
78+
inEventHandler: 0,
79+
7680
deferredCalls: [],
7781

7882
// Queues the given function call to occur the next time we enter an event handler.
@@ -170,29 +174,30 @@ var LibraryHTML5 = {
170174
#endif
171175
return {{{ cDefs.EMSCRIPTEN_RESULT_UNKNOWN_TARGET }}};
172176
}
173-
var jsEventHandler = function jsEventHandler(event) {
174-
#if HTML5_SUPPORT_DEFERRING_USER_SENSITIVE_REQUESTS
175-
// Increment nesting count for the event handler.
176-
++JSEvents.inEventHandler;
177-
JSEvents.currentEventHandler = eventHandler;
178-
// Process any old deferred calls the user has placed.
179-
JSEvents.runDeferredCalls();
180-
#endif
181-
// Process the actual event, calls back to user C code handler.
182-
eventHandler.handlerFunc(event);
177+
if (eventHandler.callbackfunc) {
183178
#if HTML5_SUPPORT_DEFERRING_USER_SENSITIVE_REQUESTS
184-
// Process any new deferred calls that were placed right now from this event handler.
185-
JSEvents.runDeferredCalls();
186-
// Out of event handler - restore nesting count.
187-
--JSEvents.inEventHandler;
179+
eventHandler.eventListenerFunc = function(event) {
180+
// Increment nesting count for the event handler.
181+
++JSEvents.inEventHandler;
182+
JSEvents.currentEventHandler = eventHandler;
183+
// Process any old deferred calls the user has placed.
184+
JSEvents.runDeferredCalls();
185+
// Process the actual event, calls back to user C code handler.
186+
eventHandler.handlerFunc(event);
187+
// Process any new deferred calls that were placed right now from this event handler.
188+
JSEvents.runDeferredCalls();
189+
// Out of event handler - restore nesting count.
190+
--JSEvents.inEventHandler;
191+
};
192+
#else
193+
eventHandler.eventListenerFunc = eventHandler.handlerFunc;
188194
#endif
189-
};
190195

191-
if (eventHandler.callbackfunc) {
192-
eventHandler.eventListenerFunc = jsEventHandler;
193-
eventHandler.target.addEventListener(eventHandler.eventTypeString, jsEventHandler, eventHandler.useCapture);
196+
eventHandler.target.addEventListener(eventHandler.eventTypeString,
197+
eventHandler.eventListenerFunc,
198+
eventHandler.useCapture);
194199
JSEvents.eventHandlers.push(eventHandler);
195-
#if !MINIMAL_RUNTIME // In minimal runtime, there is no concept of the page running vs being closed, and hence __ATEXIT__ is not present
200+
#if EXIT_RUNTIME
196201
JSEvents.registerRemoveEventListeners();
197202
#endif
198203
} else {
@@ -678,7 +683,7 @@ var LibraryHTML5 = {
678683
#if DISABLE_DEPRECATED_FIND_EVENT_TARGET_BEHAVIOR
679684
target = findEventTarget(target);
680685
#else
681-
if (eventTypeString == "scroll" && !target) {
686+
if (eventTypeId == {{{ cDefs.EMSCRIPTEN_EVENT_SCROLL }}} && !target) {
682687
target = document; // By default read scroll events on document rather than window.
683688
} else {
684689
target = findEventTarget(target);
@@ -778,27 +783,23 @@ var LibraryHTML5 = {
778783

779784
emscripten_set_blur_callback_on_thread__proxy: 'sync',
780785
emscripten_set_blur_callback_on_thread__deps: ['$registerFocusEventCallback'],
781-
emscripten_set_blur_callback_on_thread: (target, userData, useCapture, callbackfunc, targetThread) => {
782-
return registerFocusEventCallback(target, userData, useCapture, callbackfunc, {{{ cDefs.EMSCRIPTEN_EVENT_BLUR }}}, "blur", targetThread);
783-
},
786+
emscripten_set_blur_callback_on_thread: (target, userData, useCapture, callbackfunc, targetThread) =>
787+
registerFocusEventCallback(target, userData, useCapture, callbackfunc, {{{ cDefs.EMSCRIPTEN_EVENT_BLUR }}}, "blur", targetThread),
784788

785789
emscripten_set_focus_callback_on_thread__proxy: 'sync',
786790
emscripten_set_focus_callback_on_thread__deps: ['$registerFocusEventCallback'],
787-
emscripten_set_focus_callback_on_thread: (target, userData, useCapture, callbackfunc, targetThread) => {
788-
return registerFocusEventCallback(target, userData, useCapture, callbackfunc, {{{ cDefs.EMSCRIPTEN_EVENT_FOCUS }}}, "focus", targetThread);
789-
},
791+
emscripten_set_focus_callback_on_thread: (target, userData, useCapture, callbackfunc, targetThread) =>
792+
registerFocusEventCallback(target, userData, useCapture, callbackfunc, {{{ cDefs.EMSCRIPTEN_EVENT_FOCUS }}}, "focus", targetThread),
790793

791794
emscripten_set_focusin_callback_on_thread__proxy: 'sync',
792795
emscripten_set_focusin_callback_on_thread__deps: ['$registerFocusEventCallback'],
793-
emscripten_set_focusin_callback_on_thread: (target, userData, useCapture, callbackfunc, targetThread) => {
794-
return registerFocusEventCallback(target, userData, useCapture, callbackfunc, {{{ cDefs.EMSCRIPTEN_EVENT_FOCUSIN }}}, "focusin", targetThread);
795-
},
796+
emscripten_set_focusin_callback_on_thread: (target, userData, useCapture, callbackfunc, targetThread) =>
797+
registerFocusEventCallback(target, userData, useCapture, callbackfunc, {{{ cDefs.EMSCRIPTEN_EVENT_FOCUSIN }}}, "focusin", targetThread),
796798

797799
emscripten_set_focusout_callback_on_thread__proxy: 'sync',
798800
emscripten_set_focusout_callback_on_thread__deps: ['$registerFocusEventCallback'],
799-
emscripten_set_focusout_callback_on_thread: (target, userData, useCapture, callbackfunc, targetThread) => {
800-
return registerFocusEventCallback(target, userData, useCapture, callbackfunc, {{{ cDefs.EMSCRIPTEN_EVENT_FOCUSOUT }}}, "focusout", targetThread);
801-
},
801+
emscripten_set_focusout_callback_on_thread: (target, userData, useCapture, callbackfunc, targetThread) =>
802+
registerFocusEventCallback(target, userData, useCapture, callbackfunc, {{{ cDefs.EMSCRIPTEN_EVENT_FOCUSOUT }}}, "focusout", targetThread),
802803

803804
$fillDeviceOrientationEventData__deps: ['$JSEvents'],
804805
$fillDeviceOrientationEventData: (eventStruct, e, target) => {
@@ -968,7 +969,7 @@ var LibraryHTML5 = {
968969
if ({{{ makeDynCall('iipp', 'callbackfunc') }}}(eventTypeId, orientationChangeEvent, userData)) e.preventDefault();
969970
};
970971

971-
if (eventTypeString == "orientationchange" && screen.mozOrientation !== undefined) {
972+
if (eventTypeId == {{{ cDefs.EMSCRIPTEN_EVENT_ORIENTATIONCHANGE }}} && screen.mozOrientation !== undefined) {
972973
eventTypeString = "mozorientationchange";
973974
}
974975

0 commit comments

Comments
 (0)