Conversation
| let evt = new Event(interestEventName); | ||
| evt.source = invoker | ||
| target.dispatchEvent(evt); |
There was a problem hiding this comment.
We need to crate an InterestEvent which subclasses Event, and both of these events should use that.
.source is also readonly, so you need to make it a getter and not a setter. You'll also want to retarget source so you don't expose ShadowDOMs.
All together this will look something like:
function getRootNode(node) {
if (node && typeof node.getRootNode === "function") {
return node.getRootNode();
}
if (node && node.parentNode) return getRootNode(node.parentNode);
return node;
}
const eventSourceElements = new WeakMap();
class InterestEvent extends Event {
constructor(type, eventInit = {}) {
super(type, eventInit);
const { source } = eventInit;
if (source != null && !(source instanceof Element)) {
throw new TypeError(`source must be an element`);
}
eventSourceElements.set(this, source || null);
}
get [Symbol.toStringTag]() {
return "InterestEvent";
}
get source() {
if (!eventSourceElements.has(this)) {
throw new TypeError("illegal invocation");
}
const source = eventSourceElements.get(this);
if (!(source instanceof Element)) return null;
// Retarget `source` to the current event's root:
const sourceRoot = getRootNode(source);
if (sourceRoot !== getRootNode(this.target || document)) {
return sourceRoot.host;
}
return source;
}
}In addition, the event is cancelable, and so we need to return early if it's preventdefaulted. We can check dispatchEvent's return value for this.
So each of the dispatch callsites become:
| let evt = new Event(interestEventName); | |
| evt.source = invoker | |
| target.dispatchEvent(evt); | |
| const shouldContinue = target.dispatchEvent(new InterestEvent(interestEventName, { source: invoker })); | |
| if (!shouldContinue) return; |
There was a problem hiding this comment.
Thanks for reviewing this Keith!
|
Pretty interested in getting this in. One thing I'm curious about, is the WeakMap really necessary for some reason, or can it be replaced by a private element? |
I think it's just waiting on a response from Keith's review comments. |
I'd like to be respectful, but it's been over a month with no response. What's a reasonable time to wait before I submit a PR with the changes included? |
I think a month is reasonable. If you'd like to put up a PR with the comments incorporated, we could definitely take a look. |
This adds the source to the interest and loseinterest events.