|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2025 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +#if FOUNDATION_FRAMEWORK |
| 14 | +internal import _ForSwiftFoundation |
| 15 | +internal import CollectionsInternal |
| 16 | +#elseif canImport(DequeModule) |
| 17 | +internal import DequeModule |
| 18 | +#elseif canImport(_FoundationCollections) |
| 19 | +internal import _FoundationCollections |
| 20 | +#endif |
| 21 | +#if canImport(os) |
| 22 | +internal import os.log |
| 23 | +#endif |
| 24 | + |
| 25 | +@available(FoundationPreview 6.2, *) |
| 26 | +extension NotificationCenter { |
| 27 | + /// Returns an asynchronous sequence of messages produced by this center for a given subject and identifier. |
| 28 | + /// - Parameters: |
| 29 | + /// - subject: The subject to observe. Specify a metatype to observe all values for a given type. |
| 30 | + /// - identifier: An identifier representing a specific message type. |
| 31 | + /// - limit: The maximum number of messages allowed to buffer. |
| 32 | + /// - Returns: An asynchronous sequence of messages produced by this center. |
| 33 | + public func messages<Identifier: MessageIdentifier, Message: AsyncMessage>( |
| 34 | + of subject: Message.Subject, |
| 35 | + for identifier: Identifier, |
| 36 | + bufferSize limit: Int = 10 |
| 37 | + ) -> some AsyncSequence<Message, Never> where Identifier.MessageType == Message, Message.Subject: AnyObject { |
| 38 | + return AsyncMessageSequence<Message>(self, subject, limit) |
| 39 | + } |
| 40 | + |
| 41 | + /// Returns an asynchronous sequence of messages produced by this center for a given subject type and identifier. |
| 42 | + /// - Parameters: |
| 43 | + /// - subject: The metatype to observe all values for a given type. |
| 44 | + /// - identifier: An identifier representing a specific message type. |
| 45 | + /// - limit: The maximum number of messages allowed to buffer. |
| 46 | + /// - Returns: An asynchronous sequence of messages produced by this center. |
| 47 | + public func messages<Identifier: MessageIdentifier, Message: AsyncMessage>( |
| 48 | + of subject: Message.Subject.Type, |
| 49 | + for identifier: Identifier, |
| 50 | + bufferSize limit: Int = 10 |
| 51 | + ) -> some AsyncSequence<Message, Never> where Identifier.MessageType == Message { |
| 52 | + return AsyncMessageSequence<Message>(self, nil, limit) |
| 53 | + } |
| 54 | + |
| 55 | + /// Returns an asynchronous sequence of messages produced by this center for a given subject and message type. |
| 56 | + /// - Parameters: |
| 57 | + /// - subject: The subject to observe. Specify a metatype to observe all values for a given type. |
| 58 | + /// - messageType: The message type to be observed. |
| 59 | + /// - limit: The maximum number of messages allowed to buffer. |
| 60 | + /// - Returns: An asynchronous sequence of messages produced by this center. |
| 61 | + public func messages<Message: AsyncMessage>( |
| 62 | + of subject: Message.Subject? = nil, |
| 63 | + for messageType: Message.Type, |
| 64 | + bufferSize limit: Int = 10 |
| 65 | + ) -> some AsyncSequence<Message, Never> where Message.Subject: AnyObject { |
| 66 | + return AsyncMessageSequence<Message>(self, subject, limit) |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +extension NotificationCenter { |
| 71 | + fileprivate struct AsyncMessageSequence<Message: NotificationCenter.AsyncMessage>: AsyncSequence, Sendable { |
| 72 | + let center: NotificationCenter |
| 73 | + nonisolated(unsafe) weak var object: AnyObject? |
| 74 | + let bufferSize: Int |
| 75 | + |
| 76 | + init(_ center: NotificationCenter, _ object: AnyObject?, _ bufferSize: Int) { |
| 77 | + self.center = center |
| 78 | + self.object = object |
| 79 | + self.bufferSize = bufferSize |
| 80 | + } |
| 81 | + |
| 82 | + func makeAsyncIterator() -> AsyncMessageSequenceIterator<Message> { |
| 83 | + return AsyncMessageSequenceIterator(center: center, object: object, bufferSize: bufferSize) |
| 84 | + } |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +extension NotificationCenter { |
| 89 | + fileprivate final class AsyncMessageSequenceIterator<Message: NotificationCenter.AsyncMessage>: AsyncIteratorProtocol, Sendable { |
| 90 | + typealias Element = Message |
| 91 | + typealias Failure = Never |
| 92 | + |
| 93 | + struct State { |
| 94 | + var observer: NotificationCenter.ObservationToken? |
| 95 | + var continuations: [UnsafeContinuation<Message?, Never>] = [] |
| 96 | + var buffer = Deque<Message>(minimumCapacity: 1) |
| 97 | + let bufferSize: Int |
| 98 | + } |
| 99 | + |
| 100 | + struct Resumption { |
| 101 | + let message: Message? |
| 102 | + let continuations: [UnsafeContinuation<Message?, Never>] |
| 103 | + |
| 104 | + init(message: Message?, continuation: UnsafeContinuation<Message?, Never>) { |
| 105 | + self.message = message |
| 106 | + self.continuations = [continuation] |
| 107 | + } |
| 108 | + |
| 109 | + init(cancelling: [UnsafeContinuation<Message?, Never>]) { |
| 110 | + self.message = nil |
| 111 | + self.continuations = cancelling |
| 112 | + } |
| 113 | + |
| 114 | + func resume() { |
| 115 | + for continuation in continuations { |
| 116 | + continuation.resume(returning: message) |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + let state: LockedState<State> |
| 122 | + |
| 123 | + init(center: NotificationCenter, object: AnyObject?, bufferSize: Int) { |
| 124 | + self.state = LockedState(initialState: State(bufferSize: bufferSize)) |
| 125 | + |
| 126 | +#if FOUNDATION_FRAMEWORK |
| 127 | + let observerBlock: @Sendable (Notification) -> Void = { [weak self] notification in |
| 128 | + guard let message: Message = NotificationCenter._messageFromNotification(notification) else { return } |
| 129 | + |
| 130 | + self?.observationCallback(message) |
| 131 | + } |
| 132 | +#else |
| 133 | + let observerBlock: @Sendable (Message) -> Void = { [weak self] message in |
| 134 | + self?.observationCallback(message) |
| 135 | + } |
| 136 | +#endif |
| 137 | + |
| 138 | + let token = center._addObserver(Message.name, object: object, using: observerBlock) |
| 139 | + |
| 140 | + self.state.withLock { _state in |
| 141 | + _state.observer = ObservationToken(center: center, token: token) |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + deinit { |
| 146 | + teardown() |
| 147 | + } |
| 148 | + |
| 149 | + func teardown() { |
| 150 | + let (observer, resumption) = state.withLock { _state -> (NotificationCenter.ObservationToken?, Resumption) in |
| 151 | + let observer = _state.observer |
| 152 | + _state.observer = nil |
| 153 | + _state.buffer.removeAll(keepingCapacity: false) |
| 154 | + defer { _state.continuations.removeAll(keepingCapacity: false) } |
| 155 | + return (observer, Resumption(cancelling: _state.continuations)) |
| 156 | + } |
| 157 | + |
| 158 | + resumption.resume() |
| 159 | + |
| 160 | + if let observer { |
| 161 | + observer.remove() |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + func observationCallback(_ message: Message) { |
| 166 | + state.withLock { _state -> Resumption? in |
| 167 | + if _state.buffer.count + 1 > _state.bufferSize { |
| 168 | + _state.buffer.removeFirst() |
| 169 | +#if canImport(os) |
| 170 | + NotificationCenter.logger.fault("Notification center message dropped due to buffer limit. Check sequence iterator frequently or increase buffer size. Message: \(String(describing: Message.self))") |
| 171 | +#endif |
| 172 | + } |
| 173 | + _state.buffer.append(message) |
| 174 | + |
| 175 | + if _state.continuations.isEmpty { |
| 176 | + return nil |
| 177 | + } else { |
| 178 | + return Resumption(message: _state.buffer.removeFirst(), continuation: _state.continuations.removeFirst()) |
| 179 | + } |
| 180 | + }?.resume() |
| 181 | + } |
| 182 | + |
| 183 | + func next() async -> Message? { |
| 184 | + await withTaskCancellationHandler { |
| 185 | + return await withUnsafeContinuation { (continuation: UnsafeContinuation<Message?, Never>) in |
| 186 | + state.withLock { _state -> Resumption? in |
| 187 | + _state.continuations.append(continuation) |
| 188 | + if _state.buffer.isEmpty { |
| 189 | + return nil |
| 190 | + } else { |
| 191 | + return Resumption(message: _state.buffer.removeFirst(), continuation: _state.continuations.removeFirst()) |
| 192 | + } |
| 193 | + }?.resume() |
| 194 | + } |
| 195 | + } onCancel: { |
| 196 | + teardown() |
| 197 | + } |
| 198 | + } |
| 199 | + } |
| 200 | +} |
0 commit comments