Last active
February 12, 2022 18:34
-
-
Save ralfebert/99ef89bd3d8ca303766b1d24f3b764f0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
actor SyncActor { | |
var syncRequests = 0 | |
private func sync() async { | |
// Make sure only one sync is in progress at the same time | |
// (because of actor re-entrancy another sync could be started while we await for the persistence/network operations) | |
self.syncRequests += 1 | |
if self.syncRequests > 1 { | |
// Sync already in progress | |
return | |
} | |
// ... sync operation with awaiting persistence / networking logic here ... | |
self.syncRequests -= 1 | |
if self.syncRequests > 0 { | |
self.syncRequests = 0 | |
// If another sync was requested during syncing, trigger another sync operation | |
Task.detached { | |
await self.sync() | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment