Skip to content

Instantly share code, notes, and snippets.

@ralfebert
Last active February 12, 2022 18:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ralfebert/99ef89bd3d8ca303766b1d24f3b764f0 to your computer and use it in GitHub Desktop.
Save ralfebert/99ef89bd3d8ca303766b1d24f3b764f0 to your computer and use it in GitHub Desktop.
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