-
Notifications
You must be signed in to change notification settings - Fork 887
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
Add nothing
sentinel value
#652
Comments
I was always fond of this solution. I'll take a look at implementing this |
I like it, might make it much clearer than using Just a thought about naming. Might I also sometimes see important constants being uppercased (e.g. |
Thumbs up for |
@web-padawan Good point. Might have made less sense than I thought. At least I didn't propose a verb 😄 |
The nice thing about render(nothing, targetElement) |
this is pretty much the null object pattern, i'd disagree with it being called usually with this pattern, you'd call it |
Note that there are two different sentinels that are "null"-like with different purposes.
These naming schemes are more descriptive than |
The name looks like psuedo-code to me, I still think there are better, more technical names that are used in other languages and libraries. Ideally IMO we should just have a null part. If we want to render nothing, we can then pass that static part to avoid creating a new part every time and to support any kind of cache behaviour. This is a pretty common pattern across all languages, as it saves you having to do null checks. Wanting to avoid rendering ( |
Passing new values should not create new parts (with the exception of nested templates or lists). There already exists a part for each dynamic value in a template, and passing new values directs these parts to render something. You don't have to create a new part to render a new value, and with the current implementation of lit-html there is no use for something like a static part. The
|
Couldn't we just use |
@ruphin Of course, it is to render "nothing", i.e. a null part. The equivalent of I suggest we go with |
It is reasonable to use My main concern with it is that render(html`<div attr=${ null }>${ null }</div>`, document.body)
// <div attr="null"></div> Having a distinct sentinel for this case makes the interaction more explicit, and avoids confusion. (Why does On the other hand, using document.getElementById('container').innerHTML = null;
// <div id="container"></div> At this moment I will make a PR that implements |
Note: document.getElementById('container').innerHTML = null;
// <div id="container"></div>
document.getElementById('container').innerHTML = undefined;
// <div id="container">undefined</div> |
Adding null checks will of course introduce overhead too, but @stramel has a point that it is easier to understand / more useable. At large scale it would be a noticeable performance difference i imagine and could introduce the need for more null checks further down the stack. Anyhow the naming isn't great, lets see what the opinions of others are. Plain english and code don't look too good together IMO. |
We already have an explicit check for Adding a sentinel like There is plenty precedent in the public API of this library where we use English language: |
Where do we already check for null parts (or template results)? Out of interest. Introducing the null object pattern generally results in simpler implementation but also becomes useful for removing the need for null checks which definitely do have a performance overhead. If we already need such checks, though, sure, it makes little difference but those are generally the two reasons for such a pattern.
I'll stick with my suggestion for now, |
One phrasing I'd like to clear up is that we don't have "null Parts". Parts exist, usually, as a result of expressions, and value flow into them. So we just have I like |
It holds no descriptive value, has no meaning even in context.
What is It would be nice if there was a slightly more descriptive name that could be agreed on. edit: part of this may even be related to casing. Although |
We check for null when setting values that are primitives (strings, numbers, null, undefined, etc. Basically things that aren't objects or functions) because we render |
As mentioned in the issue, this feature is only to solve a very specific issue regarding fallback content in slots. Most users will never see or use |
I don't think usage amounts justify non-descriptive APIs and naming. It should be slightly more performant than using a new empty template each time, too. I wouldn't like to debate a name into such depth anyway so if justin would like it to still be called |
What do you mean with a new empty template? Maybe I missed a use case that you have for this feature. |
When combined with lit-element, for example:
new template result per render, its something i've already seen people doing quite often. perf benefits would be achieved by using the null obj pattern and having a static empty result (which is essentially the same thing but instantiated once and used by ref). same can be applied to things like |
Just wondering. What happens if something like |
The I will assume you meant something like this: cache(condition ? html`something` : nothing) The short answer is that it works as expected. If the condition is false, it renders nothing. If the condition is true, it renders the template. If the condition switches several times, the template instance is cached. The |
It basically clears my doubts. Thank you for the clear explanation. 👍
…On Thu, Jan 17, 2019, 3:31 AM Goffert van Gool ***@***.***> wrote:
The cache(condition ? 'something' : nothing) statement is a bit strange.
The sole purpose of the cache directive is to cache template instances,
but none of the possible arguments are templates, so it does absolutely
nothing, and it is functionally identical to condition ? 'something' :
nothing except it performs some extra useless checks.
I will assume you meant something like this:
cache(condition ? html`something` : nothing)
The short answer is that it works as expected. If the condition is false,
it renders nothing. If the condition is true, it renders the template. If
the condition switches several times, the template instance is cached.
The cache directive does not care what kind of argument it gets. If the
argument changed since the previous render, and the previous argument was a
template, it "saves" what was rendered previously into a cache. Then it
renders the new argument, which can be another directive, or a builtin like
nothing, or whatever lit-html normally renders. If it gets a template
that it currently holds in the cache, it will use that cached instance to
render instead of creating a new instance for that template.
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#652 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AKHcj8N9bmIp6CYWA6gvj3tSGbyA9c7Lks5vD34FgaJpZM4YtzsC>
.
|
Rendering
nothing
to a Part will cause it to clear. Useful to make sure that elements with ShadowRoots can trigger<slot></slot>
fallback content:The text was updated successfully, but these errors were encountered: