-
-
Notifications
You must be signed in to change notification settings - Fork 926
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
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
Replace onbeforeupdate
with m.retain()
#2688
Comments
Love it. Super intuitive. Since it's just a vnode, would it work in any part of a tree, not just components? |
@gilbert Correct. It's intended to replace both the attribute the component method. |
good old |
Yup, and that's where the name came from. I miss it badly. |
So, in light of some recent discussion in #2690, I'd like to propose an alternative: const Comp = {
view(vnode) {
return m.updateIf(vnode.attrs, (prev, next) => cond, () => tree)
}
} This would operate similar to Conveniently, this would allow easy diffing of not only attributes, but other state, too, and unlike React, it's very flexible. It's a little more magical than If you wanted a direct equivalent of const hasOwn = {}.hasOwnProperty
m.memo = C => ({
view: ({attrs}) => m.updateIf(
Object.entries(vnode.attrs),
(prev, next) => (
prev.length === next.length &&
prev.every(([k, v]) => hasOwn.call(vnode.attrs, k) && v === vnode.attrs[k])
),
() => C(m.censor(attrs))
)
}) |
Okay, let me go back a little on that: that's not flexible enough for efficient DOM patching. It only really helps with the diffing (part of the allure of function Comp(ctx) {
return m.compare(ctx.attrs, (prev, next) => cond ? tree : m.retain())
} It's a few more characters, but it's not that much worse, and it also means that combined with #2689, we don't need to provide any "previous" functionality. Here's the above // `m.updateIf`
const hasOwn = {}.hasOwnProperty
m.memo = C => ({attrs}) => m.updateIf(
Object.entries(attrs),
(prev, next) => (
prev.length === next.length &&
prev.every(([k, v]) => hasOwn.call(attrs, k) && v === attrs[k])
),
() => C(attrs)
)
// `m.compare(value, init)` + `m.retain()`
const hasOwn = {}.hasOwnProperty
m.memo = C => ({attrs}) => m.compare(Object.entries(attrs), (prev, next) => (
prev == null ||
prev.length === next.length && prev.every(([k, v]) => hasOwn.call(attrs, k) && v === attrs[k])
? C(attrs)
: m.retain()
))
// Minified `m.updateIf` vs `m.compare`
const h={}.hasOwnProperty;m.memo=C=>({attrs:a})=>m.updateIf(Object.entries(a),(p,n)=>p.length===n.length&&p.every(([k,v])=>h.call(a,k)&&v===a[k]),()=>C(a))
const h={}.hasOwnProperty;m.memo=C=>({attrs:a})=>m.compare(Object.entries(a),(p,n)=>null==p||p.length===n.length&&p.every(([k,v])=>h.call(a,k)&&v===a[k])?C(a):m.retain()) I'm not tied to the name, but that's the idea. Conversely, we can just provide |
I have an idea for an api but first I need to know: how does |
@gilbert It'd require a node slot (like |
Update: I'm reverting back to the original form as described in the issue for now. It's simpler, easier to wrap your head around, and more likely to get places. |
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
Mithril version:
Platform and OS:
Project:
Is this something you're interested in implementing yourself? Very
Description
Replace this pattern:
With this:
m.retain()
would have atag
of"="
, and that's how we'd detect. This can be used anywhere a child is, and on first render would be equivalent toundefined
(you're essentially explicitly "retaining" no tree, so it's consistent).Why
It's simpler for us to implement and simpler for users to implement. It's also more flexible.
This is something we've wanted to do for a while.
Possible Implementation
createVnode
to do nothing onvnode.tag === "="
.updateVnode
to, onvnode.tag === "="
, transfer state much like whatshouldNotUpdate
does when preventing update, but also transfer tags and attributes, in effect modifying them.retain()
vnode to be the actual desired vnode. (This avoids having to replace nodes in the tree, which makes this a lot less complicated.)shouldNotUpdate
check inupdateNode
.updateComponent
to invokevnode.state.view(vnode, old)
.Open Questions
The text was updated successfully, but these errors were encountered: