|
| 1 | +import Foundation |
| 2 | + |
| 3 | +public enum OperationCombiningServiceError: Error { |
| 4 | + case alreadyRunningOrFinished |
| 5 | + case noResult |
| 6 | +} |
| 7 | + |
| 8 | +public final class OperationCombiningService<T>: Longrunable, @unchecked Sendable { |
| 9 | + enum State { |
| 10 | + case waiting |
| 11 | + case running |
| 12 | + case finished |
| 13 | + } |
| 14 | + |
| 15 | + public typealias ResultType = [T] |
| 16 | + |
| 17 | + let operationsClosure: () throws -> [CompoundOperationWrapper<T>] |
| 18 | + let operationManager: OperationManagerProtocol |
| 19 | + let operationsPerBatch: Int |
| 20 | + |
| 21 | + private(set) var state: State = .waiting |
| 22 | + |
| 23 | + private var wrappers: [CompoundOperationWrapper<T>]? |
| 24 | + |
| 25 | + public init( |
| 26 | + operationManager: OperationManagerProtocol, |
| 27 | + operationsPerBatch: Int = 0, |
| 28 | + operationsClosure: @escaping () throws -> [CompoundOperationWrapper<T>] |
| 29 | + ) { |
| 30 | + self.operationManager = operationManager |
| 31 | + self.operationsClosure = operationsClosure |
| 32 | + self.operationsPerBatch = operationsPerBatch |
| 33 | + } |
| 34 | + |
| 35 | + public func start(with completionClosure: @escaping (Result<ResultType, Error>) -> Void) { |
| 36 | + guard state == .waiting else { |
| 37 | + completionClosure(.failure(OperationCombiningServiceError.alreadyRunningOrFinished)) |
| 38 | + return |
| 39 | + } |
| 40 | + |
| 41 | + state = .waiting |
| 42 | + |
| 43 | + do { |
| 44 | + let wrappers = try operationsClosure() |
| 45 | + |
| 46 | + if operationsPerBatch > 0, wrappers.count > operationsPerBatch { |
| 47 | + for index in operationsPerBatch ..< wrappers.count { |
| 48 | + let prevBatchIndex = index / operationsPerBatch - 1 |
| 49 | + |
| 50 | + let prevStart = prevBatchIndex * operationsPerBatch |
| 51 | + let prevEnd = (prevBatchIndex + 1) * operationsPerBatch |
| 52 | + |
| 53 | + for prevIndex in prevStart ..< prevEnd { |
| 54 | + wrappers[index].addDependency(wrapper: wrappers[prevIndex]) |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + let mapOperation = ClosureOperation<ResultType> { |
| 60 | + try wrappers.map { try $0.targetOperation.extractNoCancellableResultData() } |
| 61 | + } |
| 62 | + |
| 63 | + // TODO: Need to fix Sendable, temporary solution |
| 64 | + nonisolated(unsafe) let completionClosure = completionClosure |
| 65 | + |
| 66 | + mapOperation.completionBlock = { [weak self] in |
| 67 | + self?.state = .finished |
| 68 | + self?.wrappers = nil |
| 69 | + |
| 70 | + let result = Result { try mapOperation.extractNoCancellableResultData() } |
| 71 | + completionClosure(result) |
| 72 | + } |
| 73 | + |
| 74 | + let dependencies = wrappers.flatMap(\.allOperations) |
| 75 | + dependencies.forEach { mapOperation.addDependency($0) } |
| 76 | + |
| 77 | + operationManager.enqueue(operations: dependencies + [mapOperation], in: .transient) |
| 78 | + |
| 79 | + } catch { |
| 80 | + completionClosure(.failure(error)) |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + public func cancel() { |
| 85 | + if state == .running { |
| 86 | + wrappers?.forEach { $0.cancel() } |
| 87 | + wrappers = nil |
| 88 | + } |
| 89 | + |
| 90 | + state = .finished |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +extension OperationCombiningService { |
| 95 | + public func longrunOperation() -> LongrunOperation<[T]> { |
| 96 | + LongrunOperation(longrun: AnyLongrun(longrun: self)) |
| 97 | + } |
| 98 | + |
| 99 | + public static func compoundWrapper( |
| 100 | + operationManager: OperationManagerProtocol, |
| 101 | + wrapperClosure: @escaping () throws -> CompoundOperationWrapper<T>? |
| 102 | + ) -> CompoundOperationWrapper<T?> { |
| 103 | + let loadingOperation: BaseOperation<[T]> = OperationCombiningService(operationManager: operationManager) { |
| 104 | + if let wrapper = try wrapperClosure() { |
| 105 | + [wrapper] |
| 106 | + } else { |
| 107 | + [] |
| 108 | + } |
| 109 | + }.longrunOperation() |
| 110 | + |
| 111 | + let mappingOperation = ClosureOperation<T?> { |
| 112 | + try loadingOperation.extractNoCancellableResultData().first |
| 113 | + } |
| 114 | + |
| 115 | + mappingOperation.addDependency(loadingOperation) |
| 116 | + |
| 117 | + return .init(targetOperation: mappingOperation, dependencies: [loadingOperation]) |
| 118 | + } |
| 119 | + |
| 120 | + public static func compoundOptionalWrapper( |
| 121 | + operationManager: OperationManagerProtocol, |
| 122 | + wrapperClosure: @escaping () throws -> CompoundOperationWrapper<T?>? |
| 123 | + ) -> CompoundOperationWrapper<T?> { |
| 124 | + let loadingOperation: BaseOperation<[T?]> = OperationCombiningService<T?>(operationManager: operationManager) { |
| 125 | + if let wrapper = try wrapperClosure() { |
| 126 | + [wrapper] |
| 127 | + } else { |
| 128 | + [] |
| 129 | + } |
| 130 | + }.longrunOperation() |
| 131 | + |
| 132 | + let mappingOperation = ClosureOperation<T?> { |
| 133 | + let results = try loadingOperation.extractNoCancellableResultData() |
| 134 | + return results.first.flatMap { $0 } |
| 135 | + } |
| 136 | + |
| 137 | + mappingOperation.addDependency(loadingOperation) |
| 138 | + |
| 139 | + return .init(targetOperation: mappingOperation, dependencies: [loadingOperation]) |
| 140 | + } |
| 141 | + |
| 142 | + public static func compoundNonOptionalWrapper( |
| 143 | + operationManager: OperationManagerProtocol, |
| 144 | + wrapperClosure: @escaping () throws -> CompoundOperationWrapper<T> |
| 145 | + ) -> CompoundOperationWrapper<T> { |
| 146 | + let loadingOperation: BaseOperation<[T]> = OperationCombiningService<T>(operationManager: operationManager) { |
| 147 | + let wrapper = try wrapperClosure() |
| 148 | + return [wrapper] |
| 149 | + }.longrunOperation() |
| 150 | + |
| 151 | + let mappingOperation = ClosureOperation<T> { |
| 152 | + guard let result = try loadingOperation.extractNoCancellableResultData().first else { |
| 153 | + throw OperationCombiningServiceError.noResult |
| 154 | + } |
| 155 | + |
| 156 | + return result |
| 157 | + } |
| 158 | + |
| 159 | + mappingOperation.addDependency(loadingOperation) |
| 160 | + |
| 161 | + return .init(targetOperation: mappingOperation, dependencies: [loadingOperation]) |
| 162 | + } |
| 163 | +} |
0 commit comments