JetBrains / jewel

An implementation of the IntelliJ look and feels in Compose for Desktop
Apache License 2.0
636 stars 30 forks source link

No support for lazy loading of child's in LazyTree #371

Closed Yashobanta-Kumar-Behera closed 2 months ago

Yashobanta-Kumar-Behera commented 2 months ago

Lazy tree child generator scope add nodes only if children's added in blocking mode. If suspended functions are used which are responsible for adding child nodes then not working.

Here I am not changing the tree only using the children generator scope of Tree builder


val coroutineScope = CoroutineScope(Dispatchers.Default)

fun Note.asTree(isDirectory: (Note) -> Unit = {}): Tree<Note> =
    buildTree {
        addNode(this@asTree) {
            dirExpander(isDirectory)
        }
    }

fun ChildrenGeneratorScope<Note>.dirExpander(isDirectory: (Note) -> Unit ) {
    coroutineScope.launch {
        var notes: List<Note>? = null
        parent.data.noteService.getNotesFromStorage(parent.data).collectLatest {
            notes = it.notes
        }
        notes?.forEach { newNote ->
            if (newNote.isDirectory) {
                this@dirExpander.addNode(newNote) {
                    dirExpander(isDirectory)
                }
            } else {
                this@dirExpander.addLeaf(newNote)
            }
        }
    }

}

When ever user double click on the browsable node then i can load nodes

Yashobanta-Kumar-Behera commented 2 months ago

Adding last conversation https://github.com/JetBrains/jewel/issues/345

rock3r commented 2 months ago

I'm sorry but you should not just randomly launch coroutines in the composition and expect it to work. That's just not how Compose works. You're especially not supposed to launch a coroutine inside the tree creation scope. Please use the API in a more appropriate way. Move computation outside of the composition and only present the completed tree to the component.

rock3r commented 2 months ago

I had locked the previous issue because it was not a bug in our implementation, but rather you using things in a rather weird way. Please refrain from asking us again to write your code for you.

Yashobanta-Kumar-Behera commented 2 months ago

Sorry for asking but i cant find any clue to how to load child nodes if i am browsing a remove file service provider like google drive and when ever user double click a folder node it will get expanded with the child's. Do i need to manage it with own or I am missing any API that exists. In Tree Element interface i don't see any functions that i can call when ever child's are ready

rock3r commented 2 months ago

That's not really a question related to Jewel, nor a bug in Jewel, which is what this issue tracker is for. I'd recommend doing some training on Compose first, to get the grasp of the basics, and if you still are unsure how to proceed, you can look at how open source projects do something like what you're going for, and/or asking on stackoverflow for help.