-
Notifications
You must be signed in to change notification settings - Fork 16
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
Update Proposal from Feedback #21
Comments
If you would like to model the behaviour of window.addEventListener("custom-event", {
prefix: "hello",
handleEvent(event) {
console.log("%s %s", this.prefix, event.detail);
},
});
window.dispatchEvent(new CustomEvent("custom-event", { detail: "world" }));
// The above line will cause "hello world" to be printed The implementation shown as an example would not work in the same way. class GroupByEmplacer {
#value;
setValue(value) {
this.#value = value;
return this;
}
update(group) {
group.push(this.#value);
return group;
}
insert() {
return [this.#value];
}
}
function groupBy(iterable, key) {
const keyMapper = typeof key === "function" ? key : x => x[key];
const emplacer = new GroupByEmplacer();
const map = new Map();
for (const value of iterable) {
const mappedKey = keyMapper(value);
map.emplace(mappedKey, emplacer.setValue(value));
}
return map;
} While this can be trivially restructured to not depend on the methods being called with a receiver, I see no utility in calling them without receivers. |
Seems fine here, just was personal style to drop receivers since when you pass a function in it gains implicit access to the object via |
It seems very strange to me that (Separately, I find "emplace" to be a hugely confusing name as a non-C++ dev, but that bikeshed likely isn't worth having until the semantics and API are largely settled) |
@ljharb a decent amount of the feedback was of 2 categories:
Per an insert operation being required, this is to deal with existing errors you see when checking JS code: let map; // Map<string, string>
let x; // string
if (map.has(k)) {
x = map.get(k);
} else {
x = {};
map.set(k, x);
} Will actually give warnings/errors from tools since has/get not being atomic means that in theory |
@ljharb to be clear it is a blocker that we not use functions as 2 parameters. |
In that case, why not require one object with all the functions, rather than accepting the bare function form? That way the semantics of "one function" are similarly clear. |
@ljharb seems fine to me but convenient to have a shorter hand, idk about feedback from others if that would be problematic to require it to always be an object. |
If convenience is the goal then I'd expect two functions :-) if readability is more important (and if the 2 function form is somehow deemed unreadable), then I'd expect convenience to take a backseat. |
@ljharb I've updated the design to not allow a single function and now must be an options bag. |
My remaining feedback:
|
@ljharb given implementor feedback precedent for Proxy and tc39/proposal-collection-normalization#15 (comment) I don't think we should mandate either of those designs, and I won't be changing the behavior prior to plenary. It seems fine to discuss in committee and if no objections occur we can change them then. |
Review feedback:
|
|
🚫 Blocking🚫 We seem to have a variety of places in spec that do both eager or lazy grabbing of arguments. I'd note that in this case the execution of these paths for It seems the spec currently has a variety of scenarios but very few with mutual exclusive invocation of callbacks. I'd note that if this is eager it may invoke a MOP proxy handler that is not used. We have heard some criticism that any property lookup is potentially to expensive, so I'd prefer lazy given that they are mutually exclusive and only 1 is used for any given usage of
This is a good and thorough question!
I feel that throwing is likely the right choice. I don't believe we will add a
I'd prefer we had some kind of write up explaining this somewhere for future reference if we can agree on the points above being reasons that any given proposal may want to avoid ToObject or conversely in the case of Object.assign to actively call ToObject for the given proposal's use case. |
If a property lookup - which should be one of the cheapest operations we have - is too expensive, then I'd say that throws the entire idea of a "handler" object into question.
we do, in fact, have to take into account someone installing a |
Following a meeting to try and understand the underlying concerns from Mozilla I think we should move the API to focus on insertion but allow for co-location of updating. Following the DOM API design for
.addEventListener
can take an options bag to define the handler I think the following API would be sufficient:emplace
might be a naming bike shed hazard but it fairly well matches C++ and I cannot think of a simple name that would improve upon readability.Such an implementation ~=
Usage would be like:
Which would have improved readability vs an insert-only approach to this use case:
It would also have improved readability vs an update and error if missing approach:
The text was updated successfully, but these errors were encountered: