Forum

Reactive patching for async loading and more

Hello, looking for a patch review I guess. I’m trying to go full on Reactive (if that’s a good idea ?)

The goal here is to monitor added and remove files from a folder, and never display the loaded JPG more than one at a time on screen.

So I made this patch which loads JPGs path from a folder and check every second if something has been added or removed (Async).

Compare the path to only create TextureObject when a new files has been added to that folder.

Also compare path to only remove TextureObject when a file has been removed from that folder.

Now when OnQueryRandom is called with CombineLatest (Reactive) the currently displayed is set to false then randomly choose a new TextureObject that is NOT currently displayed and set IsDisplayed to true.

Is there something incorrect here in the way I’m approaching this ? Optimization or shorter patch ?

Thanks
ReactiveTest.vl (75.5 KB)

PS. @tonfilm you mention that Reactive nodes build their graph in the first frame what does that imply ?

I’ve only had a quick look, but here are a few things i noticed:

Generally, if you want a collection to only contain something once, you should use a HashSet or a dirctionary with the file path as key, then you only have to remove the deleted files and add the changed or new files. The dictionary key will make sure that each element only exists once.

this is what the DirectorWatcher does for you, checking every second can create a stutter in the main loop. also, the watcher will give you added and deleted files, so you can operate a dictionary with it directly.

The DistinctUntilChanged will only fire once, because you always send the same spread builder, that means it is never changed, because it is always the same reference.

So select 5 random elements from a collection, you can use the Randomizer node and then GetSpread the first 5 elements.

it means that events that get sent in the first frame don’t go thru the reactive graph if the update of all reactive nodes isn’t finished yet.

Stylewise, pads and pins should have spaces, so the names look visually less dense:

hope that helps

1 Like

It does help a lot.

Thanks.