Compare commits

...

4 commits
0.1 ... main

Author SHA1 Message Date
1cd6dba627
Update Readme.md 2025-04-26 18:07:52 -07:00
638c5942c0
Add Hashable conformance 2025-04-19 13:25:59 -07:00
c089cd253c
Actually write an overview in the readme 2025-04-19 11:37:26 -07:00
dd679cfd49
Add MIT license file 2025-04-19 11:37:26 -07:00
3 changed files with 39 additions and 6 deletions

7
License.md Normal file
View 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.

View file

@ -1,3 +1,7 @@
**Obsoleted by [AsyncMonitor](https://github.com/samsonjs/AsyncMonitor) and no longer maintained**
----
# NotificationTask
[![0 dependencies!](https://0dependencies.dev/0dependencies.svg)](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

View file

@ -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)
}
}