|
| 1 | +// |
| 2 | +// Copyright © 2018 Iterable. All rights reserved. |
| 3 | +// |
| 4 | + |
| 5 | +import Foundation |
| 6 | + |
| 7 | +enum IterableError: Error { |
| 8 | + case general(description: String) |
| 9 | +} |
| 10 | + |
| 11 | +extension IterableError: LocalizedError { |
| 12 | + var errorDescription: String? { |
| 13 | + switch self { |
| 14 | + case let .general(description): |
| 15 | + return NSLocalizedString(description, comment: "error description") |
| 16 | + } |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +// This has only two public methods |
| 21 | +// either there is a success with result |
| 22 | +// or there is a failure with error |
| 23 | +// There is no way to set value a result in this class. |
| 24 | +class Pending<Value, Failure> where Failure: Error { |
| 25 | + fileprivate var successCallbacks = [(Value) -> Void]() |
| 26 | + fileprivate var errorCallbacks = [(Failure) -> Void]() |
| 27 | + |
| 28 | + public func onCompletion(receiveValue: @escaping ((Value) -> Void), receiveError: ( (Failure) -> Void)? = nil) { |
| 29 | + successCallbacks.append(receiveValue) |
| 30 | + |
| 31 | + // if a successful result already exists (from constructor), report it |
| 32 | + if case let Result.success(value)? = result { |
| 33 | + successCallbacks.forEach { $0(value) } |
| 34 | + } |
| 35 | + |
| 36 | + if let receiveError = receiveError { |
| 37 | + errorCallbacks.append(receiveError) |
| 38 | + |
| 39 | + // if a failed result already exists (from constructor), report it |
| 40 | + if case let Result.failure(error)? = result { |
| 41 | + errorCallbacks.forEach { $0(error) } |
| 42 | + } |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + @discardableResult public func onSuccess(block: @escaping ((Value) -> Void)) -> Pending<Value, Failure> { |
| 47 | + successCallbacks.append(block) |
| 48 | + |
| 49 | + // if a successful result already exists (from constructor), report it |
| 50 | + if case let Result.success(value)? = result { |
| 51 | + successCallbacks.forEach { $0(value) } |
| 52 | + } |
| 53 | + |
| 54 | + return self |
| 55 | + } |
| 56 | + |
| 57 | + @discardableResult public func onError(block: @escaping ((Failure) -> Void)) -> Pending<Value, Failure> { |
| 58 | + errorCallbacks.append(block) |
| 59 | + |
| 60 | + // if a failed result already exists (from constructor), report it |
| 61 | + if case let Result.failure(error)? = result { |
| 62 | + errorCallbacks.forEach { $0(error) } |
| 63 | + } |
| 64 | + |
| 65 | + return self |
| 66 | + } |
| 67 | + |
| 68 | + public func isResolved() -> Bool { |
| 69 | + result != nil |
| 70 | + } |
| 71 | + |
| 72 | + public func wait() { |
| 73 | +// ITBDebug() |
| 74 | + guard !isResolved() else { |
| 75 | +// ITBDebug("isResolved") |
| 76 | + return |
| 77 | + } |
| 78 | + |
| 79 | +// ITBDebug("waiting....") |
| 80 | + Thread.sleep(forTimeInterval: 0.1) |
| 81 | + wait() |
| 82 | + } |
| 83 | + |
| 84 | + fileprivate var result: Result<Value, Failure>? { |
| 85 | + // Observe whenever a result is assigned, and report it |
| 86 | + didSet { result.map(report) } |
| 87 | + } |
| 88 | + |
| 89 | + // Report success or error based on result |
| 90 | + private func report(result: Result<Value, Failure>) { |
| 91 | + switch result { |
| 92 | + case let .success(value): |
| 93 | + successCallbacks.forEach { $0(value) } |
| 94 | + case let .failure(error): |
| 95 | + errorCallbacks.forEach { $0(error) } |
| 96 | + } |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +extension Pending { |
| 101 | + func flatMap<NewValue>(_ closure: @escaping (Value) -> Pending<NewValue, Failure>) -> Pending<NewValue, Failure> { |
| 102 | + let fulfill = Fulfill<NewValue, Failure>() |
| 103 | + |
| 104 | + onSuccess { value in |
| 105 | + let pending = closure(value) |
| 106 | + |
| 107 | + pending.onSuccess { futureValue in |
| 108 | + fulfill.resolve(with: futureValue) |
| 109 | + } |
| 110 | + |
| 111 | + pending.onError { futureError in |
| 112 | + fulfill.reject(with: futureError) |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + onError { error in |
| 117 | + fulfill.reject(with: error) |
| 118 | + } |
| 119 | + |
| 120 | + return fulfill |
| 121 | + } |
| 122 | + |
| 123 | + func map<NewValue>(_ closure: @escaping (Value) -> NewValue) -> Pending<NewValue, Failure> { |
| 124 | + let fulfill = Fulfill<NewValue, Failure>() |
| 125 | + |
| 126 | + onSuccess { value in |
| 127 | + let nextValue = closure(value) |
| 128 | + fulfill.resolve(with: nextValue) |
| 129 | + } |
| 130 | + |
| 131 | + onError { error in |
| 132 | + fulfill.reject(with: error) |
| 133 | + } |
| 134 | + |
| 135 | + return fulfill |
| 136 | + } |
| 137 | + |
| 138 | + func mapFailure<NewFailure>(_ closure: @escaping (Failure) -> NewFailure) -> Pending<Value, NewFailure> { |
| 139 | + let fulfill = Fulfill<Value, NewFailure>() |
| 140 | + |
| 141 | + onSuccess { value in |
| 142 | + fulfill.resolve(with: value) |
| 143 | + } |
| 144 | + |
| 145 | + onError { error in |
| 146 | + let nextError = closure(error) |
| 147 | + fulfill.reject(with: nextError) |
| 148 | + } |
| 149 | + |
| 150 | + return fulfill |
| 151 | + } |
| 152 | + |
| 153 | + func replaceError(with defaultForError: Value) -> Pending<Value, Failure> { |
| 154 | + let fulfill = Fulfill<Value, Failure>() |
| 155 | + |
| 156 | + onSuccess { value in |
| 157 | + fulfill.resolve(with: value) |
| 158 | + } |
| 159 | + |
| 160 | + onError { _ in |
| 161 | + fulfill.resolve(with: defaultForError) |
| 162 | + } |
| 163 | + |
| 164 | + return fulfill |
| 165 | + } |
| 166 | +} |
| 167 | + |
| 168 | +// This class takes the responsibility of setting value for Pending |
| 169 | +class Fulfill<Value, Failure>: Pending<Value, Failure> where Failure: Error { |
| 170 | + public init(value: Value? = nil) { |
| 171 | +// ITBDebug() |
| 172 | + super.init() |
| 173 | + if let value = value { |
| 174 | + result = Result.success(value) |
| 175 | + } else { |
| 176 | + result = nil |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + public init(error: Failure) { |
| 181 | +// ITBDebug() |
| 182 | + super.init() |
| 183 | + result = Result.failure(error) |
| 184 | + } |
| 185 | + |
| 186 | + deinit { |
| 187 | +// ITBDebug() |
| 188 | + } |
| 189 | + |
| 190 | + public func resolve(with value: Value) { |
| 191 | + result = .success(value) |
| 192 | + } |
| 193 | + |
| 194 | + public func reject(with error: Failure) { |
| 195 | + result = .failure(error) |
| 196 | + } |
| 197 | +} |
0 commit comments