Skip to content

Commit c2cb695

Browse files
authored
fix: Annotate potentially blocking event listeners with passivity (#9555)
* fix: Annotate potentially blocking event listeners with passivity * fix: Improve typings for `options` arg to `bind()`/`conditionalBind()`
1 parent e4b2f0a commit c2cb695

4 files changed

Lines changed: 29 additions & 8 deletions

File tree

core/browser_events.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ const PAGE_MODE_MULTIPLIER = 125;
4646
* @param opt_noCaptureIdentifier True if triggering on this event should not
4747
* block execution of other event handlers on this touch or other
4848
* simultaneous touches. False by default.
49+
* @param options An object with options controlling the behavior of the event
50+
* listener. Passed through directly as the third argument to
51+
* `addEventListener`.
4952
* @returns Opaque data that can be passed to unbindEvent_.
5053
*/
5154
export function conditionalBind(
@@ -54,6 +57,7 @@ export function conditionalBind(
5457
thisObject: object | null,
5558
func: Function,
5659
opt_noCaptureIdentifier?: boolean,
60+
options?: AddEventListenerOptions,
5761
): Data {
5862
/**
5963
*
@@ -75,11 +79,11 @@ export function conditionalBind(
7579
if (name in Touch.TOUCH_MAP) {
7680
for (let i = 0; i < Touch.TOUCH_MAP[name].length; i++) {
7781
const type = Touch.TOUCH_MAP[name][i];
78-
node.addEventListener(type, wrapFunc, false);
82+
node.addEventListener(type, wrapFunc, {capture: false, ...options});
7983
bindData.push([node, type, wrapFunc]);
8084
}
8185
} else {
82-
node.addEventListener(name, wrapFunc, false);
86+
node.addEventListener(name, wrapFunc, {capture: false, ...options});
8387
bindData.push([node, name, wrapFunc]);
8488
}
8589
return bindData;
@@ -95,13 +99,17 @@ export function conditionalBind(
9599
* @param name Event name to listen to (e.g. 'mousedown').
96100
* @param thisObject The value of 'this' in the function.
97101
* @param func Function to call when event is triggered.
102+
* @param options An object with options controlling the behavior of the event
103+
* listener. Passed through directly as the third argument to
104+
* `addEventListener`.
98105
* @returns Opaque data that can be passed to unbindEvent_.
99106
*/
100107
export function bind(
101108
node: EventTarget,
102109
name: string,
103110
thisObject: object | null,
104111
func: Function,
112+
options?: AddEventListenerOptions,
105113
): Data {
106114
/**
107115
*
@@ -119,11 +127,11 @@ export function bind(
119127
if (name in Touch.TOUCH_MAP) {
120128
for (let i = 0; i < Touch.TOUCH_MAP[name].length; i++) {
121129
const type = Touch.TOUCH_MAP[name][i];
122-
node.addEventListener(type, wrapFunc, false);
130+
node.addEventListener(type, wrapFunc, {capture: false, ...options});
123131
bindData.push([node, type, wrapFunc]);
124132
}
125133
} else {
126-
node.addEventListener(name, wrapFunc, false);
134+
node.addEventListener(name, wrapFunc, {capture: false, ...options});
127135
bindData.push([node, name, wrapFunc]);
128136
}
129137
return bindData;

core/comments/comment_editor.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,16 @@ export class CommentEditor implements IFocusableNode {
9595
);
9696

9797
// Don't zoom with mousewheel; let it scroll instead.
98-
browserEvents.conditionalBind(this.textArea, 'wheel', this, (e: Event) => {
99-
e.stopPropagation();
100-
});
98+
browserEvents.conditionalBind(
99+
this.textArea,
100+
'wheel',
101+
this,
102+
(e: Event) => {
103+
e.stopPropagation();
104+
},
105+
false,
106+
{passive: true},
107+
);
101108

102109
// Register listener for keydown events that would finish editing.
103110
browserEvents.conditionalBind(

core/flyout_base.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,8 @@ export abstract class Flyout
357357
'wheel',
358358
this,
359359
this.wheel_,
360+
false,
361+
{passive: false},
360362
),
361363
);
362364

core/workspace_svg.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -809,12 +809,16 @@ export class WorkspaceSvg
809809
// which otherwise prevents zoom/scroll events from being observed in
810810
// Safari. Once that bug is fixed it should be removed.
811811
this.dummyWheelListener = () => {};
812-
document.body.addEventListener('wheel', this.dummyWheelListener);
812+
document.body.addEventListener('wheel', this.dummyWheelListener, {
813+
passive: true,
814+
});
813815
browserEvents.conditionalBind(
814816
this.svgGroup_,
815817
'wheel',
816818
this,
817819
this.onMouseWheel,
820+
false,
821+
{passive: false},
818822
);
819823
}
820824

0 commit comments

Comments
 (0)