I spent so many hours already trying to wrap my head around this. Can someone please give me a conceptual idea how to solve it.
The task is to traverse Box folder using Box API in most efficient way possible.
In order to get folder content you have to send a separate request (sometimes multiple, because of pagination). And another for its sub-folder and yet another for sub-sub-folder and so on.
So my idea was:
- to feed a bunch of urls (specific to a folder-id) to an async channel
- then in a go-block for when the response comes back:
- for every subfolder - keep adding urls into the
urls-ch
channel - put folder info (alongside with the vector of its parents) into a
responses-ch
channel
- for every subfolder - keep adding urls into the
- After all that - read values off
responses-ch
and “stitch” things together usingparents
vector of the folder,- with every change - push the resulting tree into a
final-tree-ch
channel.
- with every change - push the resulting tree into a
Now, all this dandy and works nicely. Except the very last, missing piece.
How do I determine that it’s done fetching and there’s no more updates to the final-tree
?
Things are happening asynchronously, I can’t just block main thread - channels don’t ever close. There’s no way to find out how many items currently sitting in a urls-ch
or/and responses-ch
. I guess simply keeping their count and calculating the difference could work (I dunno).
I’ve been trying all sorts of things so far unsuccessfully. Please help me.
upd: Maybe using pub-sub? But I still can’t wrap my head around that either