You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Coming out of conversation around hydration within the WCCG, was thinking about what might be possible if we could intercept the customElements.define call at the browser level? Maybe we could use it to let a WebComponent bootstrap itself with its own intersection observer logic, so instead of having to defining everything via attributes and / or some framework glue layer, e.g
<my-elementcustom:dsl="x,y,z"></my-element>
A custom element could do this itself via a contract / community protocol, like using a static method or member, which could initialize its own Intersection Observer maybe?
classCounterextendsHTMLElement{static__secret(){console.debug('sssshhh! this is a secret :)')// do stuff with onload function, IntersectionObserver, Mutation Observer}}export{Counter}customElements.define('wcc-counter',Counter)
However, this presumably introduces a chicken and the egg scenario though because at this point a CE is calling customElements.define, the JS would already have been loaded on to the point, which kind of defeats the purpose somewhat.
So there will still need to be a way to decouple the "intersection" trigger from the component itself, which I suppose does benefit a compiler based tool, so there is that. 😄
Details
My thought there is that for the above example, we could extract the static method from the class and just providing that to be include that at runtime.
asyncExpressionStatement(node){const{ expression }=node;if(/* is custom element */){consttagName=node.expression.arguments[0].value;constinstanceName=node.expression.arguments[1].name;constmodule=(awaitimport(moduleURL))[instanceName];consthydrate=module.hydrate||undefine;deps[tagName]={
instanceName
moduleURL,
hydrate
}}}
And then we inject just that function into the runtime. So in theory, there is no wcc overhead, it is purely user based.
What is neat about this I think, is that you can now open up levels and degrees of hydration aside from just top down which would be a limitation of having to express the attributes everywhere up front. So depending on how many
Think about scaling as more CEs are added to the runtime via hydrate since this would push all that JavaScript to the runtime (a la Svelte). Which probably won't be much in the grand scheme of things, be entirely coming from userland, and entirely opt-in.
Bonus points for stripping hydrate out of the custom elements class definition as well to avoid shipping the bytes at runtime
Overview
Coming out of conversation around hydration within the WCCG, was thinking about what might be possible if we could intercept the
customElements.definecall at the browser level? Maybe we could use it to let a WebComponent bootstrap itself with its own intersection observer logic, so instead of having to defining everything via attributes and / or some framework glue layer, e.gA custom element could do this itself via a contract / community protocol, like using a static method or member, which could initialize its own Intersection Observer maybe?
However, this presumably introduces a chicken and the egg scenario though because at this point a CE is calling
customElements.define, the JS would already have been loaded on to the point, which kind of defeats the purpose somewhat.So there will still need to be a way to decouple the "intersection" trigger from the component itself, which I suppose does benefit a compiler based tool, so there is that. 😄
Details
My thought there is that for the above example, we could extract the static method from the class and just providing that to be include that at runtime.
And then we inject just that function into the runtime. So in theory, there is no
wccoverhead, it is purely user based.What is neat about this I think, is that you can now open up levels and degrees of hydration aside from just top down which would be a limitation of having to express the attributes everywhere up front. So depending on how many
Additional thoughts / goals
lazyDefineproposalhydratesince this would push all that JavaScript to the runtime (a la Svelte). Which probably won't be much in the grand scheme of things, be entirely coming from userland, and entirely opt-in.hydrateout of the custom elements class definition as well to avoid shipping the bytes at runtimeOpened an issue in the WCCG's Community Protocols repo.