htm icon indicating copy to clipboard operation
htm copied to clipboard

Lost static parts and fields information + top level caller

Open luwes opened this issue 5 years ago • 5 comments

I was wondering if there is any way to retain this information from the tagged template. The static parts could be used as a cache key for the output of the html`` call.

It's also useful to know which parts are the fields/holes. Lastly knowing which h call is the root node or top caller.

For the runtime a wrapper function would do the trick I think but babel-plugin-htm compiles to raw h('div', {}, h('span')) calls.

That might need something like

wrap(() => h('div', {}, h('span')), statics, ...fields);

luwes avatar Jan 21 '20 17:01 luwes

Since html is a template string literal tag, you can just wrap it

const whtml = (statics, ...fields) => wrap(() => html(statics, ...fields), statics, ...fields);

ziadkh0 avatar Jan 22 '20 09:01 ziadkh0

thanks @zaygraveyard! that would do it in the runtime yup.

luwes avatar Jan 22 '20 16:01 luwes

I've added it to a customized HTM version here https://github.com/luwes/sinuous/commit/aff75c0e56a3bf9189c19916936bb095f751af10

I'm not sure if it's beneficial to VDOM but for some libraries it can be a powerful pattern to know exactly where the tagged template starts and ends with all the statics and fields information available, caching is one use case but also treating the fields in a special way.

If you like this in HTM let me know, I can create a PR.

luwes avatar Jan 31 '20 18:01 luwes

@luwes seems like a useful change, and should be easy to land since its under a flag.

@jviide is also working on a faster replacement for this that we believe may outperform compiled JSX in all aspects.

developit avatar May 01 '20 17:05 developit

I'm surprised I didn't point it out earlier, but FWIW HTM already does caching internally. Subsequent calls return the previous return value from h() for any static parts of the template. The staticness information is exposed to h() as a bitfield under this[0].

developit avatar Feb 05 '21 03:02 developit