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

on:* #3349

Closed
wants to merge 15 commits into from
2 changes: 1 addition & 1 deletion src/compiler/compile/nodes/EventHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export default class EventHandler extends Node {
constructor(component: Component, parent, template_scope, info) {
super(component, parent, template_scope, info);

this.name = info.name;
this.name = info.name !== '*' ? info.name : 'any';
Copy link
Member

Choose a reason for hiding this comment

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

This would give on:any a different meaning than it already has, which we don't want to do. It's possible that someone has code where they are bubbling an event called any, and this would break it. I think elsewhere should just be looking for handler.name === '*' directly.

Copy link
Author

Choose a reason for hiding this comment

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

Renaming to $$any helped. Is it ok way this way?

this.modifiers = new Set(info.modifiers);

if (info.expression) {
Expand Down
15 changes: 15 additions & 0 deletions src/compiler/compile/render_dom/Block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export default class Block {
};

event_listeners: string[] = [];
any_event_elements: string[] = [];

maintain_context: boolean;
has_animation: boolean;
Expand Down Expand Up @@ -325,6 +326,20 @@ export default class Block {
`);
}

if (this.variables.size > 0) {
const listens = Array.from(this.variables.keys())
.filter(key => this.any_event_elements.includes(key))
.join(', ');

if (listens.length > 0) {
properties.add_block(deindent`
${method_name('bbl', 'bubble')}() {
return [listen, [${listens}]];
},
`);
}
}

if (this.has_intro_method || this.has_outro_method) {
if (this.builders.intro.is_empty()) {
properties.add_line(`i: @noop,`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ export default function add_event_handlers(
block.event_listeners.push(
`@listen(${target}, "${handler.name}", ${snippet}, ${opts_string})`
);
} else if (handler.name === 'any') {
block.any_event_elements.push(target);
// This isn't required but listen is treeshaken otherwise
block.event_listeners.push(
`@listen(${target}, "${handler.name}", ${snippet})`
);
} else {
block.event_listeners.push(
`@listen(${target}, "${handler.name}", ${snippet})`
Expand Down
10 changes: 10 additions & 0 deletions src/runtime/internal/Component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@ export function mount_component(component, target, anchor) {
run_all(new_on_destroy);
}
component.$$.on_mount = [];

if (fragment.bbl) {
Copy link
Member

Choose a reason for hiding this comment

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

Unless I'm mistaken, this mechanism won't forward events with handlers attached after the component has been mounted.

Object.keys(component.$$.callbacks).forEach(type => {
if (!component.$$.ctx[`${type}_handler`]) {
const [listen, els] = fragment.bbl();

els.forEach(el => component.$$.callbacks[type].forEach(cb => listen(el, type, cb)));
}
});
}
});

after_update.forEach(add_render_callback);
Expand Down