Skip to content

Commit b8eb878

Browse files
committed
Don't restrict anything to the main actor
1 parent 9a7fbdf commit b8eb878

4 files changed

Lines changed: 13 additions & 13 deletions

File tree

Readme.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ When you're integrating this into an app with Xcode then go to your project's Pa
3131
When you're integrating this using SPM on its own then add this to the list of dependencies your Package.swift file:
3232

3333
```swift
34-
.package(url: "https://github.com/samsonjs/AsyncMonitor.git", .upToNextMajor(from: "0.1.0"))
34+
.package(url: "https://github.com/samsonjs/AsyncMonitor.git", .upToNextMajor(from: "0.1.1"))
3535
```
3636

3737
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:
4343
```swift
4444
import AsyncMonitor
4545

46-
@MainActor class SimplestVersion {
46+
class SimplestVersion {
4747
let cancellable = NotificationCenter.default
4848
.notifications(named: .NSCalendarDayChanged).map(\.name)
4949
.monitor { _ in
@@ -57,7 +57,7 @@ This example uses the context parameter to avoid reference cycles with `self`:
5757
```swift
5858
import AsyncMonitor
5959

60-
@MainActor class WithContext {
60+
class WithContext {
6161
var cancellables = Set<AnyAsyncCancellable>()
6262

6363
init() {

Sources/AsyncMonitor/NSObject+AsyncKVO.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ public extension NSObjectProtocol where Self: NSObject {
66
func values<Value: Sendable>(
77
for keyPath: KeyPath<Self, Value>,
88
options: NSKeyValueObservingOptions = [],
9-
changeHandler: @escaping @MainActor (Value) -> Void
9+
changeHandler: @escaping (Value) -> Void
1010
) -> any AsyncCancellable {
1111
let (stream, continuation) = AsyncStream<Value>.makeStream()
1212
let token = self.observe(keyPath, options: options) { object, _ in
1313
continuation.yield(object[keyPath: keyPath])
1414
}
15-
return stream.monitor { @MainActor value in
15+
return stream.monitor { value in
1616
_ = token // keep this alive
1717
changeHandler(value)
1818
}

Tests/AsyncMonitorTests/AsyncMonitorTests.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import Foundation
22
import Testing
33
@testable import AsyncMonitor
44

5-
@MainActor class AsyncMonitorTests {
5+
class AsyncMonitorTests {
66
let center = NotificationCenter()
77
let name = Notification.Name("a random notification")
88

@@ -24,18 +24,18 @@ import Testing
2424
subject = center.notifications(named: name).map(\.name).monitor { receivedName in
2525
Issue.record("Called for irrelevant notification \(receivedName)")
2626
}
27-
Task {
27+
Task { [center] in
2828
center.post(name: Notification.Name("something else"), object: nil)
2929
}
3030
try await Task.sleep(for: .milliseconds(10))
3131
}
3232

33-
@Test func stopsCallingBlockWhenDeallocated() async throws {
33+
@Test @MainActor func stopsCallingBlockWhenDeallocated() async throws {
3434
subject = center.notifications(named: name).map(\.name).monitor { _ in
3535
Issue.record("Called after deallocation")
3636
}
3737

38-
Task {
38+
Task { @MainActor in
3939
subject = nil
4040
center.post(name: name, object: nil)
4141
}
@@ -48,7 +48,7 @@ import Testing
4848

4949
private var cancellable: (any AsyncCancellable)?
5050

51-
@MainActor init(center: NotificationCenter, deinitHook: @escaping () -> Void) {
51+
init(center: NotificationCenter, deinitHook: @escaping () -> Void) {
5252
self.deinitHook = deinitHook
5353
let name = Notification.Name("irrelevant name")
5454
cancellable = center.notifications(named: name).map(\.name)
@@ -78,7 +78,7 @@ import Testing
7878
Issue.record("Called after context was deallocated")
7979
}
8080
context = nil
81-
Task {
81+
Task { [center, name] in
8282
center.post(name: name, object: nil)
8383
}
8484
try await Task.sleep(for: .milliseconds(10))

Tests/AsyncMonitorTests/ReadmeExamples.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import Foundation
22
@testable import AsyncMonitor
33

4-
@MainActor class SimplestVersion {
4+
class SimplestVersion {
55
let cancellable = NotificationCenter.default
66
.notifications(named: .NSCalendarDayChanged).map(\.name)
77
.monitor { _ in
88
print("The date is now \(Date.now)")
99
}
1010
}
1111

12-
@MainActor class WithContext {
12+
class WithContext {
1313
var cancellables = Set<AnyAsyncCancellable>()
1414

1515
init() {

0 commit comments

Comments
 (0)