https://github.com/vergegroup/swift-concurrency-task-manager
Managing concurrency tasks in queueing or switching
https://github.com/vergegroup/swift-concurrency-task-manager
concurrency swift
Last synced: 6 months ago
JSON representation
Managing concurrency tasks in queueing or switching
- Host: GitHub
- URL: https://github.com/vergegroup/swift-concurrency-task-manager
- Owner: VergeGroup
- License: apache-2.0
- Created: 2023-05-26T13:09:42.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2024-09-24T08:44:28.000Z (8 months ago)
- Last Synced: 2024-11-30T19:52:03.422Z (6 months ago)
- Topics: concurrency, swift
- Language: Swift
- Homepage:
- Size: 34.2 KB
- Stars: 4
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# TaskManager - Swift Concurrency
## Overview
swift-concurrency supports structured concurrency but it also supports unstructured concurrency using Task API.
Task API runs immediately its work item. Although using closure can make deferred tasks.TaskManager accepts work items to run in a serial queue isolated by the key.
Passing key and mode (drop current items / wait in current items)## Usage
```swift
enum MyTask: TaskKeyType {}
let manager = TaskManager()
// this `await` is just for appending task item since TaskManager is Actor.
let ref = await manager.task(key: .init(MyTask.self), mode: .dropCurrent) {
// work
}// to wait the completion of the task, use `ref.value`.
await ref.value
```## Use cases
**Toggle user status**
Picture some social service - toggling the user status like favorite or not.
For the client, it needs to dispatch asynchronous requests for them.
In some case final user state would be different from what the client expected if the client dispatched multiple requests for the toggle - like the user tapped the update button continuously.```swift
enum SomeRequestKey: TaskKeyType {}let key = TaskKey(SomeRequestKey.self).combined(targetUserID)
await taskManager.task(key: key, mode: .dropCurrent) { ... }
```To avoid that case, the client stops the current request before starting a new request in the queue.
The above example binds the requests with a typed request key and target user identifier, that makes a queue for that.