-
Notifications
You must be signed in to change notification settings - Fork 7
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
A Promise for running logic after an Element's attributes have been constructed. #18
Comments
I'm not totally against this, would want to see how practical it is given the implementation. |
Would you care this this promise would never resolve if there are no custom-attributes (only regular attributes)? |
As I think about it, I'm starting to lean against. I see the value in a way to know if an element's attributes are "ready". However I would want to know that for any type of attributes. So I'm not sure if this functionality is a good fit in this library. Something that you are looking for could be written as such (pseudo-code, untested): function attributesReady(element) {
let doc = element.ownerDocument;
if(doc.contains(element)) {
return Promise.resolve();
} else {
return new Promise((resolve) => {
let mo = new MutationObserver(mutations => {
for(let mutation of mutations) {
for(let addedNode of mutation.addedNodes) {
if(addedNode === element) {
mo.disconnect();
resolve();
}
}
}
});
mo.observe(doc.documentElement, {
childList: true,
subtree: true
});
});
}
}
class MyElement extends HTMLElement {
constructor() {
super();
attributesReady(this).then(() => {
// Do whatever
})
}
} |
I wrote an example too, let me post when I get on laptop. We would also need to consider that custom attributes can be defined later, asynchronously, at any point later after initial document parsing, so if(doc.contains(element)) {
return Promise.resolve();
} would be valid only for some cases. I think the best way might be to do it in the initial observation of an element after all |
I meant, if(doc.contains(element)) {
return Promise.resolve();
} would surely tell attributes are ready for the element, but not necessarily Custom Attribute instances. But maybe only the initial attributes during doc parsing matter? Still finding out... |
Here's an initial implementation but not quite working yet: master...trusktr:attribute-ready-promise It's on top of the changes I made in #17, so you'll see those in the diff too. Usage is customAttributes.whenReady(someElement, 'some-attribute').then(...) but it is for specific attributes. Note that the promise resolution happens immediately in It'd be great to have a generic promise as previously described, and that would be like: customAttributes.whenReady(someElement).then(() => {
// any attributes that were already defined and written on the element will have been created.
// Possibly none were created if none were defined or not written on the element.
}) I'll post an update when tested and when I also make the generic one. I'd describe the API something like this:
|
Interestingly, I've used your |
Hello Matthew, I've stumbled back here thinking about if I need this sort of thing again. The reason is, if I am making a custom element, and the custom element is shipped with some custom attributes, then
This is quite complicated actually, because for example, I know there's ways to make this work, for example I could tightly couple my elements to my attributes, and make either the element call methods on the attributes, or the attributes call a method on the element, depending on load order (f.e. depending on If I find some pattern, I'll post back. |
I'm warming up to |
This DOM issue is giving me a hard time, because I'm trying to detect attributes on custom element construction so that I can detect side effects of custom attributes, and I can only get it to work with a
setTimeout(..., 0)
(has to be a macrotask).So I have something like
I see that in
custom-attributes
, the MutationObserver allows us to detect when an element finally has attributes.I can probably replace my
setTimeout
with my own MutationObserver, but maybe it'd be nice to have a Promise-based API for detecting custom attributes instances, which is what I'm interested in, so that not only are the attributes existing, but the custom attributes classes surely instantiated.API might look like
Inside a Custom Element constructor, this could be like
so at this point I know
What do you think?
The text was updated successfully, but these errors were encountered: