Skip to content
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

Polyfill the new EventListener API and use passive: true where makes sense. #5109

Closed
wants to merge 7 commits into from

Conversation

aghassemi
Copy link
Contributor

With this polyfill we can pass an options object as the third argument to addEventListener and removeEventListener regardless of browser support. The real need for this polyfill arises from the fact that browsers that expect third-argument to be the boolean capture , would interpret an object as truthy and set capture to true, which is not what we want.

Please note that actual individual options are not polyfilled (something like passive can't truly be polyfilled anyway)

Closes #4819

See #5105 for Closure Compiler extern issues related to this change and hence the @suppress blocks.

/to @jridgewell for review
/cc @dvoytenko @cramforce

@jridgewell
Copy link
Contributor

Code LGTM. I'd rather see this as an exported function, instead of a polyfill.

@aghassemi
Copy link
Contributor Author

I don't feel strongly either way but considering FF and Chrome have it and Safari has it in nightly already, I think polyfill makes sense.

@cramforce
Copy link
Member

One benefit of a function would be that we could make passive the default :)

@dvoytenko
Copy link
Contributor

Let's think about this in terms of API: would we rather continue using listen and listenOnce types of APIs with our unsubscribe concept or do we want to be closer the native DOM addEventListener? I'm gravitating toward our own listen because I'd like to get closer to the auto-unsubscribe system in many cases when resources are freed. This is currently one of our biggest issues on the roadmap to PWA and A4A is to ensure that we free up resources on element/document deletion.

* TODO(aghassemi, #5105): Type check
* @suppress {checkTypes}
*/
(() => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why extra closure?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this way I can get @Suppress to only apply to that code (I tried to do this with code blocks in {} too but looks like these annotation only work at function scope)


/**
* TODO(aghassemi, #5105): Type check
* @suppress {checkTypes}
Copy link
Contributor

@dvoytenko dvoytenko Sep 20, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's complication with type checking? Is it still not in externs? If so, please file a bug against closure.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I talked to them, there is an open PR to update the extern, they said they will review and merge in a week or so.

/**
* Decides whether capture argument should be true/false based on the value of
* then given options argument.
* @param {*} options
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@return

/**
* @param {!Window} win
*/
export function install(win) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is another thing that needs to be polyfiled in a friendly frame. If we do proceed with this, please also call this polyfill from installPolyfillsInChildWindow in extensions-impl.js.

@aghassemi
Copy link
Contributor Author

looks like consensus is not polyfilling, I will move this to listen and related methods. There are not many places that use listen et al., so I can manually check them to ensure we can do passive: true as default.

I will also create an issue for us to migrate all addEventListeners to use listen instead and then we should not allow it anymore in presubmit. There are a lot of them and for each we should make sure the default passive: true is not a breaking change so that change won't be part of this CL and something to do in chunks later.

sounds good?

@jridgewell
Copy link
Contributor

👍

@aghassemi
Copy link
Contributor Author

/cc @dvoytenko

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants