Skip to content

Commit a9cc46d

Browse files
committed
asyncObserverQueue should be unique per NotificationCenter
1 parent 62aaa8e commit a9cc46d

File tree

3 files changed

+20
-13
lines changed

3 files changed

+20
-13
lines changed

Sources/FoundationEssentials/NotificationCenter/NotificationCenter.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ private struct MessageBox {
7676

7777
open class NotificationCenter: @unchecked Sendable {
7878
private var registrar: LockedState<[String? /* Notification name */: [ObjectIdentifier? /* object */ : AutoDictionary<@Sendable (MessageBox) -> Void>]]>
79+
internal lazy var _actorQueueManager = _NotificationCenterActorQueueManager()
7980

8081
public required init() {
8182
registrar = LockedState(initialState: [:])
@@ -149,6 +150,10 @@ open class NotificationCenter: @unchecked Sendable {
149150
_registrar.isEmpty
150151
}
151152
}
153+
154+
internal func _getActorQueueManager() -> _NotificationCenterActorQueueManager {
155+
return _actorQueueManager
156+
}
152157
}
153158

154159
extension NotificationCenter: Equatable {

Sources/FoundationEssentials/NotificationCenter/NotificationCenterMessage.swift

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -135,15 +135,7 @@ extension NotificationCenter {
135135
}
136136

137137
extension NotificationCenter {
138-
#if FOUNDATION_FRAMEWORK
139138
internal var asyncObserverQueue: _NotificationCenterActorQueueManager {
140139
self._getActorQueueManager() as! _NotificationCenterActorQueueManager
141140
}
142-
#else
143-
internal static let _actorQueueManager = _NotificationCenterActorQueueManager()
144-
145-
internal var asyncObserverQueue: _NotificationCenterActorQueueManager {
146-
return Self._actorQueueManager
147-
}
148-
#endif
149141
}

Tests/FoundationEssentialsTests/NotificationCenterTests.swift

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@
77
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
88
//
99

10-
#if !FOUNDATION_FRAMEWORK
11-
10+
#if FOUNDATION_FRAMEWORK
11+
@testable import Foundation
12+
#else
1213
@testable @_spi(SwiftCorelibsFoundation) import FoundationEssentials
14+
#endif
1315

1416
import Testing
1517

@@ -28,7 +30,8 @@ private struct NotificationCenterTests {
2830
let center2 = NotificationCenter()
2931
#expect(center1 != center2)
3032
}
31-
33+
34+
#if !FOUNDATION_FRAMEWORK
3235
@Test func internalPostNotification() {
3336
nonisolated(unsafe) var flag = false
3437

@@ -214,6 +217,13 @@ private struct NotificationCenterTests {
214217
center._removeObserver(observer2)
215218
center._removeObserver(observer3)
216219
}
217-
}
218-
219220
#endif
221+
222+
@MainActor
223+
@Test func uniqueActorQueuePerCenter() {
224+
let center1 = NotificationCenter()
225+
let center2 = NotificationCenter()
226+
227+
#expect(center1.asyncObserverQueue !== center2.asyncObserverQueue)
228+
}
229+
}

0 commit comments

Comments
 (0)