|
| 1 | + |
| 2 | +# TaskLoadingAggregate |
| 3 | + |
| 4 | +### *Track your Swift Concurrency Tasks activity in an aggregate with ease.* |
| 5 | + |
| 6 | +## 📄 Description |
| 7 | +Swift's Concurrency makes working with asynchronous tasks through async/await a breeze. Hooking up a loading state for one task is just as easy. But what if you have multiple tasks running? Tracking one single loading state in those cases is a bit harder. |
| 8 | + |
| 9 | +### Introducing `TaskLoadingAggregate` 🎉 |
| 10 | + |
| 11 | +TaskLoadingAggregate makes this a breeze by creating a loading state aggregate for your tasks. Each tracked task will report their status to the aggregate and as long as a task is loading the aggregate will report `isLoading` as `true`. |
| 12 | + |
| 13 | +## 🎮 Usage |
| 14 | + |
| 15 | +Hooking up a task to a TaskLoadingAggregate is as simple as: |
| 16 | + |
| 17 | +``` swift |
| 18 | +let loadingAggregate = TaskLoadingAggregate() |
| 19 | + |
| 20 | +// First task |
| 21 | +Task { |
| 22 | + try await doSomething() |
| 23 | +}.track(loadingManager) |
| 24 | + |
| 25 | +// Second task |
| 26 | +Task { |
| 27 | + try await doSomethingElse() |
| 28 | +}.track(loadingManager) |
| 29 | + |
| 30 | +// You can now bind your UI or whatever to loadingAggregate's @Published isLoading property 🚀 |
| 31 | +``` |
| 32 | + |
| 33 | +#### Q: *Is this only for `Task`?* |
| 34 | + |
| 35 | +No, you can use a TaskLoadingAggregate however you like, but then it is up to you to increment and decrement the aggregates loading counter: |
| 36 | + |
| 37 | +``` swift |
| 38 | +let loadingAggregate = TaskLoadingAggregate() |
| 39 | + |
| 40 | +// In async function |
| 41 | +func doSomething() async { |
| 42 | + loadingAggregate.increment() |
| 43 | + await doSomethingElse() |
| 44 | + loadingAggregate.decrement() |
| 45 | +} |
| 46 | + |
| 47 | +// In classic closure |
| 48 | +loadingAggregate.increment() |
| 49 | +self.doSomething(completion: { |
| 50 | + loadingAggregate.decrement() |
| 51 | +}) |
| 52 | +``` |
| 53 | + |
| 54 | +## 😋 Who cooked it? |
| 55 | + |
| 56 | +[![@amnell][twitter-image]](https://twitter.com/amnell) [![amnell][github-image]](https://github.com/amnell) |
| 57 | + |
| 58 | +## ⚖️ License |
| 59 | + |
| 60 | +**TaskLoadingAggregate** is generously distributed under the *[MIT](https://opensource.org/licenses/MIT)*. |
| 61 | + |
| 62 | +<!-- GitHub's Markdown reference links --> |
| 63 | +[twitter-image]: https://img.shields.io/badge/Twitter-1DA1F2?style=for-the-badge&logo=twitter&logoColor=white |
| 64 | +[github-image]: https://img.shields.io/badge/GitHub-100000?style=for-the-badge&logo=github&logoColor=white |
| 65 | + |
| 66 | +<!-- README generated with: https://github.com/pH-7/cool-readme-generator --> |
0 commit comments