Currently layer collections are discarded and recreated when collections are moved to another listbase or when their parents are deleted. This results in extraneous code being required in the move function to handle flags and loss of flag data when parents are deleted. This also prevents storing any kind of persistent data in layer collections in the future, i.e. converting them to ID properties.
To solve this problem I have added code to detect invalid layer collections before syncing and then handle them by either deleting layer collections whose corresponding collections have been deleted, or relinking the invalid collections during the sync. Any layer collections that are invalid but did not get relinked during the sync are assumed to have been unlinked, and are deleted. This allows all layer collections to be preserved, including the layer collections of linked collections, and eliminates the need for workarounds to preserve data.
All other, simpler, attempts at perserving layer collections via syncing, that I could think of, did not work. The only other potential alternative solution for this that I can think of would be to
merge layer collections with collections and store view layer specific data within a hashtable that uses view layers as keys, but this would be a far more extensive change and would cause python api breakage. Barring problems I'm not aware of, this seems like it could be a better long term solution for the future.
Limitations include increased code complexity.