mirror of
https://github.com/samsonjs/NotificationTask.git
synced 2026-03-28 09:45:51 +00:00
Compare commits
4 commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 1cd6dba627 | |||
| 638c5942c0 | |||
| c089cd253c | |||
| dd679cfd49 |
3 changed files with 39 additions and 6 deletions
7
License.md
Normal file
7
License.md
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
Copyright © 2025 Sami Samhuri, https://samhuri.net <sami@samhuri.net>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
15
Readme.md
15
Readme.md
|
|
@ -1,3 +1,7 @@
|
|||
**Obsoleted by [AsyncMonitor](https://github.com/samsonjs/AsyncMonitor) and no longer maintained**
|
||||
|
||||
----
|
||||
|
||||
# NotificationTask
|
||||
|
||||
[](https://0dependencies.dev)
|
||||
|
|
@ -6,12 +10,19 @@
|
|||
|
||||
## Overview
|
||||
|
||||
`NotificationTask` is
|
||||
NotificationTask is a Swift library that provides a simple and easy-to-use way to manage Swift concurrency `Task`s that observe notifications. The `NotificationTask` class allows you to create tasks that receive notifications and call the given closure with each notification, and optionally also with a context parameter so you don't have to manage its lifetime.
|
||||
|
||||
It uses a Swift `Task` to ensure that all resources are properly cleaned up when the `NotificationTask` is cancelled or deallocated. This library is designed for use in view layer code, making it easy to integrate with your app's user interface and/or view-model layer.
|
||||
|
||||
That's it. It's pretty trivial. I just got tired of writing it over and over.
|
||||
|
||||
## Installation
|
||||
|
||||
Honestly you should probably just copy [NotificationTask.swift]() into your project.
|
||||
|
||||
The only way to install this package is with Swift Package Manager (SPM). Please [file a new issue][] or submit a pull-request if you want to use something else.
|
||||
|
||||
[NotificationTask.swift]: https://github.com/samsonjs/NotificationTask/blob/main/Sources/NotificationTask/NotificationTask.swift
|
||||
[file a new issue]: https://github.com/samsonjs/NotificationTask/issues/new
|
||||
|
||||
### Supported Platforms
|
||||
|
|
@ -46,7 +57,7 @@ import NotificationTask
|
|||
}
|
||||
```
|
||||
|
||||
Example using context to avoid reference cycles with `self`:
|
||||
This example uses the context parameter to avoid reference cycles with `self`:
|
||||
|
||||
```swift
|
||||
import NotificationTask
|
||||
|
|
|
|||
|
|
@ -14,8 +14,8 @@ extension Notification: @unchecked @retroactive Sendable {}
|
|||
/// always receives a strong reference. This one is called ``init(name:context:center:performing:)``.
|
||||
///
|
||||
/// ``NotificationTask`` is bound to the main actor and is intended to be used in your view layer. This keeps it simple
|
||||
@MainActor public final class NotificationTask {
|
||||
var task: Task<Void, Never>?
|
||||
@MainActor public final class NotificationTask: Hashable {
|
||||
let task: Task<Void, Never>
|
||||
|
||||
init(task: Task<Void, Never>) {
|
||||
self.task = task
|
||||
|
|
@ -49,7 +49,22 @@ extension Notification: @unchecked @retroactive Sendable {}
|
|||
}
|
||||
|
||||
deinit {
|
||||
task?.cancel()
|
||||
task = nil
|
||||
task.cancel()
|
||||
}
|
||||
|
||||
public func store(in set: inout Set<NotificationTask>) {
|
||||
set.insert(self)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Hashable conformance
|
||||
|
||||
public extension NotificationTask {
|
||||
nonisolated static func == (lhs: NotificationTask, rhs: NotificationTask) -> Bool {
|
||||
ObjectIdentifier(lhs) == ObjectIdentifier(rhs)
|
||||
}
|
||||
|
||||
nonisolated func hash(into hasher: inout Hasher) {
|
||||
hasher.combine(task)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue