B4J Question [BANano] [SOLVED] How to loop through an Iterator BANanoObject with next() and access all items?

Mashiane

Expert
Licensed User
Longtime User
Hi there

I am trying the code below, discussed in this article: https://www.cloudsavvyit.com/9852/web-apps-can-interact-with-your-filesystem-now/

I am stuck here in the for await clause

The directoryHandle.values() when assigned to a BANanoObject returns and Iterator with next(), calling next returns an item, however I dont know how to loop through the rest.

B4X:
const directoryHandle = window.showDirectoryPicker();
for await (let handle of directoryHandle.values()) {
    if (handle.type === "file") {
        // file
    }
    if (handle.type === "directory") {
        // subdirectory
    }
}

Try out:
B4X:
Dim values As BANanoObject = BANano.Await(dh.dirHandle.RunMethod("values", Null))
    Log(values)
    Dim item As BANanoObject = BANano.Await(values.RunMethod("next", Null))
    Log(item)

I have tried a do until loop checking the IsNull and IsUndefined of the item read by next(), also


1632043934650.png


Thanks
 

Attachments

  • 1632043685177.png
    1632043685177.png
    18.2 KB · Views: 175
Solution
Every "item" returns if it ended (with the "done" field), so the solution is pretty simple:

B4X:
    Dim dirHandle As BANanoObject = BANano.Await(BANano.Window.RunMethod("showDirectoryPicker", Null))
    Dim values As BANanoObject = BANano.Await(dirHandle.RunMethod("values", Null))
  
    Dim item As BANanoObject = BANano.Await(values.RunMethod("next", Null))
    Do While item.GetField("done").Result = False
        Dim fileObj As BANanoObject = item.GetField("value").Result      
        If fileObj.GetField("kind").Result = "file" Then
            ' file
            Log("File: " & fileObj.GetField("name"))
        Else
            ' folder  
            Log("Folder: " & fileObj.GetField("name"))
        End If
        item =...

alwaysbusy

Expert
Licensed User
Longtime User
Every "item" returns if it ended (with the "done" field), so the solution is pretty simple:

B4X:
    Dim dirHandle As BANanoObject = BANano.Await(BANano.Window.RunMethod("showDirectoryPicker", Null))
    Dim values As BANanoObject = BANano.Await(dirHandle.RunMethod("values", Null))
  
    Dim item As BANanoObject = BANano.Await(values.RunMethod("next", Null))
    Do While item.GetField("done").Result = False
        Dim fileObj As BANanoObject = item.GetField("value").Result      
        If fileObj.GetField("kind").Result = "file" Then
            ' file
            Log("File: " & fileObj.GetField("name"))
        Else
            ' folder  
            Log("Folder: " & fileObj.GetField("name"))
        End If
        item = BANano.Await(values.RunMethod("next", Null))
    Loop

Important note: This API is very experimental and will only work in a very limited number of browsers/devices as e.g. Firefox simply refuses to build-in support for it because of the obvious security risk.

More info on how iterators work in Javascript: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Iterators_and_Generators

Alwaysbusy
 
Upvote 1
Solution

Mashiane

Expert
Licensed User
Longtime User
This API is very experimental and will only work in a very limited number of browsers/devices as e.g
Yes I know, thanks, i'm always just curious about stuff.

"pretty" simple.... its you who is talking remember. I keep learning new stuff everyday. Now I know.

Awesome. thanks a lot!

BTW you must have something like BANano Tips and Tricks... I appreciate this the more I get to understand how things work. I guess also the issue is understanding how javascript works, especially when trying new things.
 
Last edited:
Upvote 0
Top