|
| 1 | +import Foundation |
| 2 | + |
| 3 | +public final class CancellableCallStore { |
| 4 | + private var cancellableCall: CancellableCall? |
| 5 | + |
| 6 | + public init() {} |
| 7 | + |
| 8 | + public var hasCall: Bool { |
| 9 | + cancellableCall != nil |
| 10 | + } |
| 11 | + |
| 12 | + public func cancel() { |
| 13 | + let copy = cancellableCall |
| 14 | + cancellableCall = nil |
| 15 | + copy?.cancel() |
| 16 | + } |
| 17 | + |
| 18 | + func store(call: CancellableCall) { |
| 19 | + cancellableCall = call |
| 20 | + } |
| 21 | + |
| 22 | + func clear() { |
| 23 | + cancellableCall = nil |
| 24 | + } |
| 25 | + |
| 26 | + func clearIfMatches(call: CancellableCall) -> Bool { |
| 27 | + guard matches(call: call) else { |
| 28 | + return false |
| 29 | + } |
| 30 | + |
| 31 | + cancellableCall = nil |
| 32 | + |
| 33 | + return true |
| 34 | + } |
| 35 | + |
| 36 | + func matches(call: CancellableCall) -> Bool { |
| 37 | + cancellableCall === call |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +public func execute<T>( |
| 42 | + wrapper: CompoundOperationWrapper<T>, |
| 43 | + inOperationQueue operationQueue: OperationQueue, |
| 44 | + runningCallbackIn callbackQueue: DispatchQueue?, |
| 45 | + callbackClosure: @escaping (Result<T, Error>) -> Void |
| 46 | +) { |
| 47 | + wrapper.targetOperation.completionBlock = { |
| 48 | + dispatchInQueueWhenPossible(callbackQueue) { |
| 49 | + do { |
| 50 | + let value = try wrapper.targetOperation.extractNoCancellableResultData() |
| 51 | + callbackClosure(.success(value)) |
| 52 | + } catch { |
| 53 | + callbackClosure(.failure(error)) |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + operationQueue.addOperations(wrapper.allOperations, waitUntilFinished: false) |
| 59 | +} |
| 60 | + |
| 61 | +public func executeCancellable<T>( |
| 62 | + wrapper: CompoundOperationWrapper<T>, |
| 63 | + inOperationQueue operationQueue: OperationQueue, |
| 64 | + backingCallIn callStore: CancellableCallStore, |
| 65 | + runningCallbackIn callbackQueue: DispatchQueue?, |
| 66 | + mutex: NSLock? = nil, |
| 67 | + callbackClosure: @escaping (Result<T, Error>) -> Void |
| 68 | +) { |
| 69 | + wrapper.targetOperation.completionBlock = { |
| 70 | + dispatchInQueueWhenPossible(callbackQueue, locking: mutex) { |
| 71 | + guard callStore.clearIfMatches(call: wrapper) else { |
| 72 | + return |
| 73 | + } |
| 74 | + |
| 75 | + do { |
| 76 | + let value = try wrapper.targetOperation.extractNoCancellableResultData() |
| 77 | + callbackClosure(.success(value)) |
| 78 | + } catch { |
| 79 | + callbackClosure(.failure(error)) |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + callStore.store(call: wrapper) |
| 85 | + |
| 86 | + operationQueue.addOperations(wrapper.allOperations, waitUntilFinished: false) |
| 87 | +} |
| 88 | + |
| 89 | +public func execute<T>( |
| 90 | + operation: BaseOperation<T>, |
| 91 | + inOperationQueue operationQueue: OperationQueue, |
| 92 | + runningCallbackIn callbackQueue: DispatchQueue?, |
| 93 | + callbackClosure: @escaping (Result<T, Error>) -> Void |
| 94 | +) { |
| 95 | + operation.completionBlock = { |
| 96 | + dispatchInQueueWhenPossible(callbackQueue) { |
| 97 | + do { |
| 98 | + let value = try operation.extractNoCancellableResultData() |
| 99 | + callbackClosure(.success(value)) |
| 100 | + } catch { |
| 101 | + callbackClosure(.failure(error)) |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + operationQueue.addOperations([operation], waitUntilFinished: false) |
| 107 | +} |
| 108 | + |
| 109 | +public func execute<T>( |
| 110 | + operation: BaseOperation<T>, |
| 111 | + inOperationQueue operationQueue: OperationQueue, |
| 112 | + backingCallIn callStore: CancellableCallStore, |
| 113 | + runningCallbackIn callbackQueue: DispatchQueue?, |
| 114 | + callbackClosure: @escaping (Result<T, Error>) -> Void |
| 115 | +) { |
| 116 | + operation.completionBlock = { |
| 117 | + dispatchInQueueWhenPossible(callbackQueue) { |
| 118 | + guard callStore.clearIfMatches(call: operation) else { |
| 119 | + return |
| 120 | + } |
| 121 | + |
| 122 | + do { |
| 123 | + let value = try operation.extractNoCancellableResultData() |
| 124 | + callbackClosure(.success(value)) |
| 125 | + } catch { |
| 126 | + callbackClosure(.failure(error)) |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + callStore.store(call: operation) |
| 132 | + |
| 133 | + operationQueue.addOperations([operation], waitUntilFinished: false) |
| 134 | +} |
0 commit comments