Skip to content

[FEATURE] Implement on modifier. - #17960

Merged
rwjblue merged 2 commits into
masterfrom
add-on-modifier
Apr 24, 2019
Merged

rwjblue merged 2 commits into
masterfrom
add-on-modifier

Conversation

@rwjblue

@rwjblue rwjblue commented Apr 23, 2019

Copy link
Copy Markdown
Member

@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
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

lol

Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
Member

Choose a reason for hiding this comment

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

@rwjblue rwjblue Apr 24, 2019 •

Copy link
Copy Markdown
Member Author

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
Copy Markdown
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
Copy Markdown
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).

@buschtoens buschtoens left a comment

Copy link
Copy Markdown
Contributor

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

rwjblue commented Apr 24, 2019

Copy link
Copy Markdown
Member Author

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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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

cvx commented May 28, 2019

Copy link
Copy Markdown
Contributor

@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

rwjblue commented May 28, 2019

Copy link
Copy Markdown
Member Author

@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