Skip to content

Replace onbeforeupdate with m.retain() #2688

@dead-claudia

Description

@dead-claudia

Mithril version:

Platform and OS:

Project:

Is this something you're interested in implementing yourself? Very

Description

Replace this pattern:

const Comp = {
    onbeforeupdate(vnode, old) {
        return cond
    },
    view(vnode) {
        return tree
    }
}

With this:

const Comp = {
    // On first render, `old` is not passed
    view(vnode, old) {
        if (!cond) return m.retain()
        return tree
    }
}

m.retain() would have a tag of "=", and that's how we'd detect. This can be used anywhere a child is, and on first render would be equivalent to undefined (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

  1. Modify createVnode to do nothing on vnode.tag === "=".
  2. Modify updateVnode to, on vnode.tag === "=", transfer state much like what shouldNotUpdate does when preventing update, but also transfer tags and attributes, in effect modifying the m.retain() vnode to be the actual desired vnode. (This avoids having to replace nodes in the tree, which makes this a lot less complicated.)
  3. Remove the shouldNotUpdate check in updateNode.
  4. Modify updateComponent to invoke vnode.state.view(vnode, old).

Open Questions

Metadata

Metadata

Assignees

Labels

Area: CoreFor anything dealing with Mithril core itselfType: Breaking ChangeFor any feature request or suggestion that could reasonably break existing codeType: EnhancementFor any feature request or suggestion that isn't a bug fix

Type

No type

Projects

Status

Completed/Declined

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions