Skip to content
25 changes: 21 additions & 4 deletions src/runtime/internal/lifecycle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,7 @@ export function createEventDispatcher<EventMap extends {} = any>(): <
// TODO are there situations where events could be dispatched
// in a server (non-DOM) environment?
const event = custom_event(type, detail, { cancelable });
callbacks.slice().forEach(fn => {
fn.call(component, event);
});
invoke_callbacks(callbacks, component, event);
return !event.defaultPrevented;
}

Expand Down Expand Up @@ -151,6 +149,25 @@ export function bubble(component, event) {

if (callbacks) {
// @ts-ignore
callbacks.slice().forEach(fn => fn.call(this, event));
invoke_callbacks(callbacks, this, event);
}
}

// Invoke all callbacks, even if an error occur
function invoke_callbacks(callbacks, source, event) {
callbacks.slice().forEach(fn => {
try {
fn.call(source, event);
} catch (err) {
// @ts-ignore
if (window.reportError) {
Copy link
Member

Choose a reason for hiding this comment

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

question for more knowledgeable reviewers: will this file be called during ssr? if so, we probably need to fix this reference to window. also note that reportError is not yet supported in Node

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh I miss that component events can be triggered in SSR !
Don't know how to handle this correctly...

// @ts-ignore
window.reportError(err);
} else {
setTimeout(() => {
throw err;
}, 0);
}
}
});
}