-
Notifications
You must be signed in to change notification settings - Fork 163
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
ObservableArray without assignment #1027
Comments
I tend to think people will find out quickly enough what kind of semantics the setter has, but I do think #485 would help here from a Web IDL perspective by allowing us to make it more clear that the setter essentially takes a sequence and therefore does not preserve identity. Aside, the CSSOM description doesn't seem to describe the getter and setter? |
tl;dr;
As a library implementor, if I were starting from zero today, I would't have a lot of strong opinions about what the API looked like in its final form. I would want the However, we've got a library in production for 18mo or so, upon which hundreds of components have been built, used internally at Microsoft by at least several hundred engineers, deployed on major sites, that if broken will cause substantial damage. You could argue that we shouldn't have used this API until these details were locked down. At the time there were substantial benefits to doing so, and there was precedent for its use among nearly all major library authors. I had myself been using it prior to Microsoft's adoption of the API, for at least a year IIRC. For better or worse, this is a reality we have to deal with. We are not the only ones in this situation. In light of this, I have a couple of requests:
Because of these needs, at this point in time, I'd prefer to either:
If we go with Option 1 then I don't need to change any code. If we go with Option 2 then my code changes are minor. I can easily feature detect without exceptions being involved. Because our existing implementation already feature detects for The problem with keeping the name the same and then making the setter throw is that existing libraries which may not be upgradeable in time, will all the sudden start to fail. I'd like to avoid that most of all. For reference, the feature detection that has been deployed for Array.isArray(document.adoptedStyleSheets) && "replace" in CSSStyleSheet.prototype It now makes me wonder, does |
It returns true. |
Thanks @EisenbergEffect. Indeed changing the behavior of I understand that so much of this debate is tied up in the specific use of ObservableArray in |
I just want to make sure I'm answering the right question. Are you saying that you want to know if this is surprising or not? const myTextRuns = [];
const text = new FormattedText();
text.textruns = myTextRuns;
assert(myTextRuns !== text.textruns); |
ObservableArray attributes don't have getter or setter steps; instead their getter/setter algorithms are auto-generated by Web IDL in the attribute getter/attribute setter algorithms at https://webidl.spec.whatwg.org/#define-the-attributes . It might be nice if there were some Bikeshed spec mechanism so that the attribute in the IDL block auto-linked to something in the Web IDL spec, so that it doesn't seem like the attributes have no definitions. |
Yes, exactly. If this is not really a surprise, then the current behavior won't be a problem, and this issue is moot. |
I thought about this for a bit...and it's not so easy to answer. On the one hand, when it's expressed as I have above, and if I'm thinking in terms of vanilla JS, I do find that code surprising. However...I don't personally write that kind of code. So, I'm not likely to be affected by it. I can see how some hard-to-track bugs could come from that if you don't have unit tests covering your mutation patterns. Perhaps this is what @annevk means by "people will find out quickly enough what kind of semantics the setter has". On the other hand, after reading the WebIDL docs on So, a bit of a mixed bag, but I'm leaning towards this not being a big issue. Perhaps potential problems can be handled with documentation? Make this very explicit not just in improvements to WebIDL, but also call this out in places like MDN and even in TypeScript d.ts. docs. At least with What other APIs are slated to be switched over to |
The potential concerns raised here sound like they are pretty much the same as the ones discussed previously. And as before, there are ways to argue both sides. Since the previous discussion also came to a consensus (WICG/construct-stylesheets#45 (comment)) with resulting landed spec update, I think it only makes sense to conclude the same in this case and close this issue as no-change.
This is a great question. One potential answer is that |
Alright, I think I am convinced that any developer confusion can be addressed in documentation, tests, or practical experience with the type. Thank you all for participating and again hearing this concern. I look forward to seeing implementations of this type land soon. |
The legacy behavior of adoptedStyleSheets is one thing, but it seems deeply weird that ObservableArray has built-in copy-on-assigment semantics defined at the Web IDL level. But maybe it's less weird than I think. Is there any other Web IDL type where that's true? That said, it's possible for other specs (even |
Arguably this is true for all "primitive" Web IDL types, e.g. if you do document.title = { toString() { return "foo"; } }; this will perform a "copy-on-assignment" of the right-hand side onto the internal title property, and A variant is how numeric properties are treated, i.e. element.tabIndex = 1.1; will perform a "normalizing copy" of Of the remaining types that can be used as the type of a settable attribute in Web IDL, we have:
|
As a follow-up to my comment here, and as a compromise position, I'd like to make one last push to make assignment on an ObservableArray attribute throw.
The downside to the current behavior (as noted) is that JavaScript developers might not be aware of the copy semantics that assignment has, and will assume that the Put results in changing the ObservableArray to refer the the same instance that was assigned (and that subsequent updates to that instance will be reflected in the ObservableArray). To avoid potential confusion, having assignment throw will be sure to prevent any failed expectations. (And such an artificial restriction can be lifted in the future without back-compat concern if necessary.)
There seemed to be at least some support for this idea in the linked issue thread.
Caveat: such a change would not be backwards-compatible with the
adoptedStyleSheets
property, which depends on assignment today to change/update the set of adopted sheets.I'm interested in seeing a mutually acceptable compromise between supporters and objectors--if this proposed change can be that compromise that leads to general agreement on the behavior for ObservableArray among implementers, then I view the loss of good ergonomics as acceptable. The alternative is the status quo, now merged into CSSOM for
adoptedStyleSheets
which I'm happy to support if compromise cannot be reached.Related folks: @rniwa, @emilio, @annevk, @mfreed7, @domenic
The text was updated successfully, but these errors were encountered: