This is a convenient change if we want to animate the visibility of a collection (See D12469).
Currently, when a collection flag needs to be set, BKE_main_collection_sync(bmain) needs to be called to recreate all Bases and all hierarchy of collections in all ViewLayers.
Adding this function to an update operand in Depsgraph would be inefficient for animation since the depsgraph operand is always called during frame change (it doesn't matter if the flag has changed or not).
(D12469 has a workaround by creating a function that reduces the scope of synchronization).
The solution for this patch is to synchronize all ViewLayers when setting the flag instead of when updating.
This causes the BKE_main_collection_sync(bmain) API to only be called when the flag changes.
Also BKE_main_collection_sync(bmain) has been edited to work with scene and ViewLayer evaluated.