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

[FEATURE] Implement on modifier. #17960

Merged
merged 2 commits into from
Apr 24, 2019
Merged

[FEATURE] Implement on modifier. #17960

merged 2 commits into from
Apr 24, 2019

Conversation

rwjblue
Copy link
Member

@rwjblue rwjblue commented Apr 23, 2019

See https://emberjs.github.io/rfcs/0471-on-modifier.html for details.

Special thanks to @buschtoens's work on ember-on-modifier which heavily inspired this implementation.

@rwjblue rwjblue requested a review from pzuraq April 23, 2019 21:28
import { isChrome, isFirefox } from '@ember/-internals/browser-environment';
import { privatize as P } from '@ember/-internals/container';

const isIE11 = !window.ActiveXObject && 'ActiveXObject' in window;
Copy link
Contributor

Choose a reason for hiding this comment

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

lol

Copy link
Member Author

Choose a reason for hiding this comment

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

😭 indeed

callback: EventListener,
options?: AddEventListenerOptions
): void {
adds++;
Copy link
Contributor

Choose a reason for hiding this comment

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

Assuming these aren't in DEBUGs because you wanted to run these tests against IE?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, exactly. We can ultimately remove if we care, but it's very very cheap and almost certainly won't matter.

updateFromArgs() {
let { args } = this;

let { once, passive, capture }: AddEventListenerOptions = args.named.value();
Copy link
Contributor

Choose a reason for hiding this comment

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

In ember-on-modifier, we assert that only these three options are passed to guard against typos. Do you think we should add this here as well?

Copy link
Member Author

Choose a reason for hiding this comment

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

Hmm, the RFC specifically states the opposite (that passing other things is "ignored"):

From here:

The following named parameters will be accepted as options to {{on}}:

  • capture
  • once
  • passive

These will be passed forward to addEventListener as options in modern browsers, and will be polyfilled in older browsers. Other options will be ignored.

I don't feel super strongly so I just followed the RFC on this.

@pzuraq - Do you think we should change to assert?

addEventListener(state.element, state.eventName, state.callback, state.options);

state.shouldUpdate = false;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

If I'm reading this correctly, once the modifier is installed, it will only ever update to a new listener, but will never be uninstalled. I can't think of a scenario, where the modifier would need to be uninstalled from an element that does not get removed from the DOM afterwards, but I think @chancancode made a case for this once on Discord.

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member Author

@rwjblue rwjblue Apr 24, 2019

Choose a reason for hiding this comment

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

@buschtoens - We definitely cleanup (see getDestructor just below, and the destroy method on the OnModifierState object). I will also add a test (using the assert count methodology) to ensure we don't accidentally regress this.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, I see! So getDestructor returns a Destroyable, which is implemented through destroy() in OnModifierState. I was not familiar with that part of the Glimmer API yet. 👍

Copy link
Member Author

Choose a reason for hiding this comment

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

Ya, this is one way the "internal" modifiers differ from the public API. The public API effectively does the same thing (the destroy method on the state bucket invokes the destroyModifier hook on the custom manager).

Copy link
Contributor

@buschtoens buschtoens left a comment

Choose a reason for hiding this comment

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

Looks awesome! Thank you for getting this into core so swiftly. ⚡️

* Extract shared utility for a `this` context that errors when used.
* Assert when more positional arguments are provided.
* Avoid wrapping callback needlessly (makes debugging a bit easier,
  since the event handler function is actually the user-land function in
  most cases.
* Incorporates a system to error if any `this` properties are
  accessed from within the callback in debug builds.
* Fixup comments for addEventListener / removeEventListener logic.
* Fix test assertion message.
* Add test ensuring the modifier is destroyed properly.
@rwjblue
Copy link
Member Author

rwjblue commented Apr 24, 2019

FYI - Updated to address review feedback:

  • Extract shared utility for a this context that errors when used.
  • Assert when more positional arguments are provided.
  • Avoid wrapping callback needlessly (makes debugging a bit easier,
    since the event handler function is actually the user-land function in
    most cases.
  • Incorporates a system to error if any this properties are
    accessed from within the callback in debug builds.
  • Fixup comments for addEventListener / removeEventListener logic.
  • Fix test assertion message.
  • Add test ensuring the modifier is destroyed properly.

I think this should be good to go once CI is green, in case anyone wants another crack at a review first...

);
}

const context = buildUntouchableThis('`fn` helper');
Copy link
Contributor

Choose a reason for hiding this comment

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

Can't touch this

Copy link
Contributor

Choose a reason for hiding this comment

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

yaaaaas

});
} else if (DEBUG) {
// prevent the callback from being bound to the element
this.callback = userProvidedCallback.bind(untouchableContext);
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice! 👍

@rwjblue rwjblue merged commit fc1b375 into master Apr 24, 2019
@rwjblue rwjblue deleted the add-on-modifier branch April 24, 2019 17:30
@CvX
Copy link
Contributor

CvX commented May 28, 2019

@rwjblue The current implementation of on modifier isn't FastBoot ready, is it? It throws "TypeError: element.removeEventListener is not a function" where {{action}} didn't.

@rwjblue
Copy link
Member Author

rwjblue commented May 28, 2019

@CvX - That is being tracked over in #17949.

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.

6 participants