mobx-state-tree icon indicating copy to clipboard operation
mobx-state-tree copied to clipboard

Walk is not walking the tree

Open dirtyrolf opened this issue 5 years ago • 6 comments

Bug report I'm trying to use walk on a tree, but it only seems to visit the root node, not any of the children.

The current implementation looks like this

export function walk(
    target: IAnyStateTreeNode,
    processor: (item: IAnyStateTreeNode) => void
): void {
    // check all arguments
    assertIsStateTreeNode(target, 1)
    assertIsFunction(processor, 2)

    const node = getStateTreeNode(target)
    // tslint:disable-next-line:no_unused-variable
    node.getChildren().forEach(child => {
        if (isStateTreeNode(child.storedValue)) walk(child.storedValue, processor)
    })
    processor(node.storedValue)
}

While debugging it seems like storedValue is undefined for all the children, so it doesn't walk any further down the tree. If I change it to value, it has the behaviour I'm expecting (with very limited testing), but I don't know enough about the internals to know the difference between storedValue and value, or if this is the way to do it.

  • [x] I've checked documentation and searched for existing issues
  • [x] I've made sure my project is based on the latest MST version
  • [x] Fork this code sandbox or another minimal reproduction.

Sandbox link or minimal reproduction code

https://codesandbox.io/s/mobx-state-tree-todolist-q92tw

Describe the expected behavior

I expect the sandbox to print the tree, not just the root

Describe the observed behavior

It only prints the root

dirtyrolf avatar Dec 03 '19 15:12 dirtyrolf

I am seeing the same problem. I don't have a strong grasp of the internals either, but it feels like storedValue is lazy loaded at some point. Did you find a work-around/solution?

stewartlord avatar Apr 17 '20 19:04 stewartlord

Update: I was able to work-around this issue by writing my own walk function that calls resolvePath on each node of the tree and then iterates through its children. It does not feel like an elegant solution and I would welcome a proper fix to this bug 🙏

stewartlord avatar Apr 17 '20 21:04 stewartlord

@stewartlord The workaround I used was working with value instead of storedValue. It worked for my extremely limited testing, but I never explored it any further because I didn't end up using mst in the project (for other reasons).

dirtyrolf avatar Apr 19 '20 08:04 dirtyrolf

Hey @dirtyrolf - thanks for all the repo steps here. I am going to mark this as a bug for now and see if we can find some time or volunteers to investigate and resolve if need be.

Sorry it took so long for anyone to get back to you on this!

coolsoftwaretyler avatar Jun 30 '23 04:06 coolsoftwaretyler

Any news on this? Using value instead of storedValue doesn't work for me.

Sinled avatar Dec 05 '23 16:12 Sinled

Hey @Sinled - we haven't had anyone volunteer to help. I'm digging further into the internals myself, so it's possible I'll stumble on the problem/solution again, but I'm not specifically working on it.

Do you want to pitch in? I'd be happy to help you get started. Or if you know anyone looking to get some open source contributions under their belt, I'd love to help them out.

coolsoftwaretyler avatar Dec 05 '23 20:12 coolsoftwaretyler