From b8eb8780973439874eee49e719de5b1b9d7bf6e1 Mon Sep 17 00:00:00 2001 From: Sami Samhuri Date: Sat, 26 Apr 2025 11:05:19 -0700 Subject: [PATCH] Don't restrict anything to the main actor --- Readme.md | 6 +++--- Sources/AsyncMonitor/NSObject+AsyncKVO.swift | 4 ++-- Tests/AsyncMonitorTests/AsyncMonitorTests.swift | 12 ++++++------ Tests/AsyncMonitorTests/ReadmeExamples.swift | 4 ++-- 4 files changed, 13 insertions(+), 13 deletions(-) diff --git a/Readme.md b/Readme.md index ee3ea3c..c5ba4c0 100644 --- a/Readme.md +++ b/Readme.md @@ -31,7 +31,7 @@ When you're integrating this into an app with Xcode then go to your project's Pa When you're integrating this using SPM on its own then add this to the list of dependencies your Package.swift file: ```swift -.package(url: "https://github.com/samsonjs/AsyncMonitor.git", .upToNextMajor(from: "0.1.0")) +.package(url: "https://github.com/samsonjs/AsyncMonitor.git", .upToNextMajor(from: "0.1.1")) ``` and then add `"AsyncMonitor"` to the list of dependencies in your target as well. @@ -43,7 +43,7 @@ The simplest example uses a closure that receives the notification: ```swift import AsyncMonitor -@MainActor class SimplestVersion { +class SimplestVersion { let cancellable = NotificationCenter.default .notifications(named: .NSCalendarDayChanged).map(\.name) .monitor { _ in @@ -57,7 +57,7 @@ This example uses the context parameter to avoid reference cycles with `self`: ```swift import AsyncMonitor -@MainActor class WithContext { +class WithContext { var cancellables = Set() init() { diff --git a/Sources/AsyncMonitor/NSObject+AsyncKVO.swift b/Sources/AsyncMonitor/NSObject+AsyncKVO.swift index 50ce00f..dba9c54 100644 --- a/Sources/AsyncMonitor/NSObject+AsyncKVO.swift +++ b/Sources/AsyncMonitor/NSObject+AsyncKVO.swift @@ -6,13 +6,13 @@ public extension NSObjectProtocol where Self: NSObject { func values( for keyPath: KeyPath, options: NSKeyValueObservingOptions = [], - changeHandler: @escaping @MainActor (Value) -> Void + changeHandler: @escaping (Value) -> Void ) -> any AsyncCancellable { let (stream, continuation) = AsyncStream.makeStream() let token = self.observe(keyPath, options: options) { object, _ in continuation.yield(object[keyPath: keyPath]) } - return stream.monitor { @MainActor value in + return stream.monitor { value in _ = token // keep this alive changeHandler(value) } diff --git a/Tests/AsyncMonitorTests/AsyncMonitorTests.swift b/Tests/AsyncMonitorTests/AsyncMonitorTests.swift index c219bde..c3802bb 100644 --- a/Tests/AsyncMonitorTests/AsyncMonitorTests.swift +++ b/Tests/AsyncMonitorTests/AsyncMonitorTests.swift @@ -2,7 +2,7 @@ import Foundation import Testing @testable import AsyncMonitor -@MainActor class AsyncMonitorTests { +class AsyncMonitorTests { let center = NotificationCenter() let name = Notification.Name("a random notification") @@ -24,18 +24,18 @@ import Testing subject = center.notifications(named: name).map(\.name).monitor { receivedName in Issue.record("Called for irrelevant notification \(receivedName)") } - Task { + Task { [center] in center.post(name: Notification.Name("something else"), object: nil) } try await Task.sleep(for: .milliseconds(10)) } - @Test func stopsCallingBlockWhenDeallocated() async throws { + @Test @MainActor func stopsCallingBlockWhenDeallocated() async throws { subject = center.notifications(named: name).map(\.name).monitor { _ in Issue.record("Called after deallocation") } - Task { + Task { @MainActor in subject = nil center.post(name: name, object: nil) } @@ -48,7 +48,7 @@ import Testing private var cancellable: (any AsyncCancellable)? - @MainActor init(center: NotificationCenter, deinitHook: @escaping () -> Void) { + init(center: NotificationCenter, deinitHook: @escaping () -> Void) { self.deinitHook = deinitHook let name = Notification.Name("irrelevant name") cancellable = center.notifications(named: name).map(\.name) @@ -78,7 +78,7 @@ import Testing Issue.record("Called after context was deallocated") } context = nil - Task { + Task { [center, name] in center.post(name: name, object: nil) } try await Task.sleep(for: .milliseconds(10)) diff --git a/Tests/AsyncMonitorTests/ReadmeExamples.swift b/Tests/AsyncMonitorTests/ReadmeExamples.swift index f691053..43c2036 100644 --- a/Tests/AsyncMonitorTests/ReadmeExamples.swift +++ b/Tests/AsyncMonitorTests/ReadmeExamples.swift @@ -1,7 +1,7 @@ import Foundation @testable import AsyncMonitor -@MainActor class SimplestVersion { +class SimplestVersion { let cancellable = NotificationCenter.default .notifications(named: .NSCalendarDayChanged).map(\.name) .monitor { _ in @@ -9,7 +9,7 @@ import Foundation } } -@MainActor class WithContext { +class WithContext { var cancellables = Set() init() {