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

Deprecate accessing jQuery.Event#originalEvent #16690

Merged
merged 1 commit into from
May 26, 2018

Conversation

simonihmig
Copy link
Contributor

Implements the deprecation message for user-land code accessing originalEvent on jQuery.Event instances, as proposed by RFC#294 (https://github.com/emberjs/rfcs/blob/master/text/0294-optional-jquery.md#introducing-ember-jquery-legacy-and-deprecating-jqueryevent-usage)

Relates to #16687 introducing the _JQUERY_INTEGRATION flag also used here.

@simonihmig
Copy link
Contributor Author

Tests failing due to the yarn registry being down...

@simonihmig
Copy link
Contributor Author

After making prettier and IE11 (no Proxy) happy, it's finally green now 😀

@rwjblue would love your review here!

// not trigger a deprecation

/* global Proxy */
return new Proxy(jqEvent, {
Copy link
Member

Choose a reason for hiding this comment

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

Can you restructure this slightly, like:

import { DEBUG } from '@glimmer/env';

export default function addJQueryEventDeprecation(jqEvent) {
  if (!DEBUG || !HAS_NATIVE_PROXY) {
    return jqEvent;
  }

  return new Proxy(...);
}

This will ensure we always just use the jqEvent in production builds...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, that totally makes sense! I was worried a bit about runtime overhead, but somehow didn't think of this... 🙄#dontCodeLateAtNight

I guess uglify (or rollup even before that?) will then strip it away at build time?

Copy link
Member

Choose a reason for hiding this comment

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

Yep, exactly!

import { ENV } from 'ember-environment';
import { HAS_NATIVE_PROXY } from 'ember-utils';

function deprecateJQueryEvent() {
Copy link
Member

Choose a reason for hiding this comment

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

Can you remove this function and inline the deprecate() call directly into addJQueryEventDeprecation? This will reduce the total byte size impact for production builds (this outer function will be stripped as well).

// we need a native Proxy here, so we can make sure that the internal use of originalEvent in jQuery itself does
// not trigger a deprecation

/* global Proxy */
Copy link
Member

Choose a reason for hiding this comment

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

I generally like to add these at the top of the file, would you mind moving it up there?

if (typeof target[name] === 'function') {
// for methods jQuery.Event call them with `target` as the `this` context, so they will use access
// `originalEvent` from the original jQuery event, not our proxy, thus not trigger the deprecation
return target[name].bind(target);
Copy link
Member

Choose a reason for hiding this comment

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

Should we cache these?

Something like:

let boundFunctions = new Map();
// ...snip..
  if (!boundFunctions.has(name)) { 
    boundFucntions.set(name, target[name].bind(target));
  }
  
  return boundFunctions.get(name);

@simonihmig
Copy link
Contributor Author

@rwjblue two tests in the build-tests scenario failed, which test for the __originalEvent property. I guess the tests run against a production build, is that right? Then that would make sense, as this property is only set when the Proxy-wrapper is applied, i.e. not in production anymore after the recent changes. Should I add another guard around these tests, to only run when DEBUG is set, or what would be the appropriate way to handle this?

@rwjblue
Copy link
Member

rwjblue commented May 26, 2018

Should I add another guard around these tests, to only run when DEBUG is set, or what would be the appropriate way to handle this?

Yes, I think that makes sense.

Implements the deprecation message for user-land code accessing `originalEvent` on `jQuery.Event` instances, as proposed by RFC#294 (https://github.com/emberjs/rfcs/blob/master/text/0294-optional-jquery.md#introducing-ember-jquery-legacy-and-deprecating-jqueryevent-usage)
@simonihmig
Copy link
Contributor Author

Fixed!

@rwjblue rwjblue merged commit a81baf3 into emberjs:master May 26, 2018
@rwjblue
Copy link
Member

rwjblue commented May 26, 2018

Awesome, thank you!

@simonihmig simonihmig deleted the rfc294-jquery-dot-event branch June 11, 2018 21:21
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.

3 participants