|
| 1 | +// |
| 2 | +// Copyright © 2025 Stream.io Inc. All rights reserved. |
| 3 | +// |
| 4 | + |
| 5 | +import Combine |
| 6 | +import Foundation |
| 7 | + |
| 8 | +/// A thread-safe buffer that collects items for later consumption. |
| 9 | +/// |
| 10 | +/// Supports on-demand appending or reactive observation from a |
| 11 | +/// publisher source. Items can be consumed in batch, with optional |
| 12 | +/// flushing of the internal buffer. |
| 13 | +final class ConsumableBucket<Element>: @unchecked Sendable { |
| 14 | + |
| 15 | + /// Represents the type of bucket: on-demand or publisher-driven. |
| 16 | + enum BucketType { |
| 17 | + /// A manually managed bucket where items are appended directly. |
| 18 | + case onDemand |
| 19 | + /// A bucket that observes and collects items from a publisher. |
| 20 | + case observer(AnyPublisher<Element, Never>) |
| 21 | + } |
| 22 | + |
| 23 | + private let queue = UnfairQueue() |
| 24 | + |
| 25 | + private var items: [Element] = [] |
| 26 | + private var cancellable: AnyCancellable? |
| 27 | + |
| 28 | + /// Initializes the bucket with a transformed publisher source. |
| 29 | + /// |
| 30 | + /// - Parameters: |
| 31 | + /// - source: A publisher of source values. |
| 32 | + /// - transformer: A transformer to map source to bucket elements. |
| 33 | + convenience init<Source, Transformer: ConsumableBucketItemTransformer>( |
| 34 | + _ source: AnyPublisher<Source, Never>, |
| 35 | + transformer: Transformer |
| 36 | + ) where Transformer.Input == Source, Transformer.Output == Element { |
| 37 | + self.init( |
| 38 | + .observer( |
| 39 | + source |
| 40 | + .compactMap { transformer.transform($0) } |
| 41 | + .eraseToAnyPublisher() |
| 42 | + ) |
| 43 | + ) |
| 44 | + } |
| 45 | + |
| 46 | + /// Initializes the bucket with a transformed publisher source and |
| 47 | + /// optionally removes duplicates. |
| 48 | + /// |
| 49 | + /// - Parameters: |
| 50 | + /// - source: A publisher of equatable source values. |
| 51 | + /// - transformer: A transformer to map source to bucket elements. |
| 52 | + /// - removeDuplicates: Whether to remove duplicate values. |
| 53 | + convenience init<Source: Equatable, Transformer: ConsumableBucketItemTransformer>( |
| 54 | + _ source: AnyPublisher<Source, Never>, |
| 55 | + transformer: Transformer, |
| 56 | + removeDuplicates: Bool |
| 57 | + ) where Transformer.Input == Source, Transformer.Output == Element { |
| 58 | + if removeDuplicates { |
| 59 | + self.init( |
| 60 | + .observer( |
| 61 | + source |
| 62 | + .removeDuplicates() |
| 63 | + .compactMap { transformer.transform($0) } |
| 64 | + .eraseToAnyPublisher() |
| 65 | + ) |
| 66 | + ) |
| 67 | + } else { |
| 68 | + self.init(source, transformer: transformer) |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + /// Initializes the bucket with a publisher of bucket elements. |
| 73 | + /// |
| 74 | + /// - Parameter source: The publisher emitting elements to store. |
| 75 | + convenience init( |
| 76 | + _ source: AnyPublisher<Element, Never> |
| 77 | + ) { |
| 78 | + self.init(.observer(source)) |
| 79 | + } |
| 80 | + |
| 81 | + /// Initializes the bucket for manual on-demand use. |
| 82 | + convenience init() { |
| 83 | + self.init(.onDemand) |
| 84 | + } |
| 85 | + |
| 86 | + private init( |
| 87 | + _ bucketType: BucketType |
| 88 | + ) { |
| 89 | + switch bucketType { |
| 90 | + case .onDemand: |
| 91 | + break |
| 92 | + case let .observer(publisher): |
| 93 | + cancellable = publisher.sink { [weak self] in self?.process($0) } |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + /// Appends a single element to the bucket. |
| 98 | + /// |
| 99 | + /// - Parameter element: The item to be appended. |
| 100 | + func append(_ element: Element) { |
| 101 | + queue.sync { [weak self] in self?.items.append(element) } |
| 102 | + } |
| 103 | + |
| 104 | + /// Returns the items in the bucket. |
| 105 | + /// |
| 106 | + /// - Parameter flush: Whether to clear the buffer after returning. |
| 107 | + /// - Returns: An array of stored elements. |
| 108 | + func consume(flush: Bool = false) -> [Element] { |
| 109 | + if flush { |
| 110 | + return queue.sync { |
| 111 | + let result = items |
| 112 | + items = [] |
| 113 | + return result |
| 114 | + } |
| 115 | + } else { |
| 116 | + return queue.sync { items } |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + /// Inserts elements at a specific position in the buffer. |
| 121 | + /// |
| 122 | + /// - Parameters: |
| 123 | + /// - items: The elements to insert. |
| 124 | + /// - index: The position at which to insert them. |
| 125 | + func insert(_ items: [Element], at index: Int) { |
| 126 | + guard !items.isEmpty else { |
| 127 | + return |
| 128 | + } |
| 129 | + queue.sync { |
| 130 | + self.items.insert(contentsOf: items, at: index) |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + // MARK: - Private Helpers |
| 135 | + |
| 136 | + /// Processes an incoming item from the observed publisher. |
| 137 | + /// |
| 138 | + /// - Parameter newItem: The new item to append. |
| 139 | + private func process(_ newItem: Element) { |
| 140 | + queue.sync { |
| 141 | + self.items.append(newItem) |
| 142 | + } |
| 143 | + } |
| 144 | +} |
0 commit comments