|
| 1 | +// swift-format-ignore: AllPublicDeclarationsHaveDocumentation |
| 2 | +/// A result builder for composing multiple atom effects into a single effect. |
| 3 | +/// |
| 4 | +/// ## Example |
| 5 | +/// ```swift |
| 6 | +/// @AtomEffectBuilder |
| 7 | +/// func effect(context: CurrentContext) -> some AtomEffect { |
| 8 | +/// UpdateEffect { |
| 9 | +/// print("Updated") |
| 10 | +/// } |
| 11 | +/// |
| 12 | +/// ReleaseEffect { |
| 13 | +/// print("Released") |
| 14 | +/// } |
| 15 | +/// |
| 16 | +/// CustomEffect() |
| 17 | +/// } |
| 18 | +/// ``` |
| 19 | +@MainActor |
| 20 | +@resultBuilder |
| 21 | +public enum AtomEffectBuilder { |
| 22 | + public static func buildBlock<Effect: AtomEffect>(_ effect: Effect) -> Effect { |
| 23 | + effect |
| 24 | + } |
| 25 | + |
| 26 | + public static func buildBlock<each Effect: AtomEffect>(_ effect: repeat each Effect) -> some AtomEffect { |
| 27 | + if #available(iOS 17, macOS 14, tvOS 17, watchOS 10, *) { |
| 28 | + return BlockEffect(repeat each effect) |
| 29 | + } |
| 30 | + else { |
| 31 | + return BlockBCEffect(repeat each effect) |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + public static func buildIf<Effect: AtomEffect>(_ effect: Effect?) -> some AtomEffect { |
| 36 | + IfEffect(effect) |
| 37 | + } |
| 38 | + |
| 39 | + public static func buildEither<TrueEffect: AtomEffect, FalseEffect: AtomEffect>( |
| 40 | + first: TrueEffect |
| 41 | + ) -> ConditionalEffect<TrueEffect, FalseEffect> { |
| 42 | + ConditionalEffect(storage: .trueEffect(first)) |
| 43 | + } |
| 44 | + |
| 45 | + public static func buildEither<TrueEffect: AtomEffect, FalseEffect: AtomEffect>( |
| 46 | + second: FalseEffect |
| 47 | + ) -> ConditionalEffect<TrueEffect, FalseEffect> { |
| 48 | + ConditionalEffect(storage: .falseEffect(second)) |
| 49 | + } |
| 50 | + |
| 51 | + public static func buildLimitedAvailability(_ effect: any AtomEffect) -> some AtomEffect { |
| 52 | + LimitedAvailabilityEffect(effect) |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +public extension AtomEffectBuilder { |
| 57 | + struct ConditionalEffect<TrueEffect: AtomEffect, FalseEffect: AtomEffect>: AtomEffect { |
| 58 | + internal enum Storage { |
| 59 | + case trueEffect(TrueEffect) |
| 60 | + case falseEffect(FalseEffect) |
| 61 | + } |
| 62 | + |
| 63 | + private let storage: Storage |
| 64 | + |
| 65 | + internal init(storage: Storage) { |
| 66 | + self.storage = storage |
| 67 | + } |
| 68 | + |
| 69 | + public func initializing(context: Context) { |
| 70 | + switch storage { |
| 71 | + case .trueEffect(let trueEffect): |
| 72 | + trueEffect.initializing(context: context) |
| 73 | + |
| 74 | + case .falseEffect(let falseEffect): |
| 75 | + falseEffect.initializing(context: context) |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + public func initialized(context: Context) { |
| 80 | + switch storage { |
| 81 | + case .trueEffect(let trueEffect): |
| 82 | + trueEffect.initialized(context: context) |
| 83 | + |
| 84 | + case .falseEffect(let falseEffect): |
| 85 | + falseEffect.initialized(context: context) |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + public func updated(context: Context) { |
| 90 | + switch storage { |
| 91 | + case .trueEffect(let trueEffect): |
| 92 | + trueEffect.updated(context: context) |
| 93 | + |
| 94 | + case .falseEffect(let falseEffect): |
| 95 | + falseEffect.updated(context: context) |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + public func released(context: Context) { |
| 100 | + switch storage { |
| 101 | + case .trueEffect(let trueEffect): |
| 102 | + trueEffect.released(context: context) |
| 103 | + case .falseEffect(let falseEffect): |
| 104 | + falseEffect.released(context: context) |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +private extension AtomEffectBuilder { |
| 111 | + @available(iOS 17, macOS 14, tvOS 17, watchOS 10, *) |
| 112 | + struct BlockEffect<each Effect: AtomEffect>: AtomEffect { |
| 113 | + private let effect: (repeat each Effect) |
| 114 | + |
| 115 | + init(_ effect: repeat each Effect) { |
| 116 | + self.effect = (repeat each effect) |
| 117 | + } |
| 118 | + |
| 119 | + func initializing(context: Context) { |
| 120 | + repeat (each effect).initializing(context: context) |
| 121 | + } |
| 122 | + |
| 123 | + func initialized(context: Context) { |
| 124 | + repeat (each effect).initialized(context: context) |
| 125 | + } |
| 126 | + |
| 127 | + func updated(context: Context) { |
| 128 | + repeat (each effect).updated(context: context) |
| 129 | + } |
| 130 | + |
| 131 | + func released(context: Context) { |
| 132 | + repeat (each effect).released(context: context) |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + struct BlockBCEffect: AtomEffect { |
| 137 | + private let _initializing: @MainActor (Context) -> Void |
| 138 | + private let _initialized: @MainActor (Context) -> Void |
| 139 | + private let _updated: @MainActor (Context) -> Void |
| 140 | + private let _released: @MainActor (Context) -> Void |
| 141 | + |
| 142 | + init<each Effect: AtomEffect>(_ effect: repeat each Effect) { |
| 143 | + _initializing = { context in |
| 144 | + repeat (each effect).initializing(context: context) |
| 145 | + } |
| 146 | + _initialized = { context in |
| 147 | + repeat (each effect).initialized(context: context) |
| 148 | + } |
| 149 | + _updated = { context in |
| 150 | + repeat (each effect).updated(context: context) |
| 151 | + } |
| 152 | + _released = { context in |
| 153 | + repeat (each effect).released(context: context) |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + func initializing(context: Context) { |
| 158 | + _initializing(context) |
| 159 | + } |
| 160 | + |
| 161 | + func initialized(context: Context) { |
| 162 | + _initialized(context) |
| 163 | + } |
| 164 | + |
| 165 | + func updated(context: Context) { |
| 166 | + _updated(context) |
| 167 | + } |
| 168 | + |
| 169 | + func released(context: Context) { |
| 170 | + _released(context) |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + struct IfEffect<Effect: AtomEffect>: AtomEffect { |
| 175 | + private let effect: Effect? |
| 176 | + |
| 177 | + init(_ effect: Effect?) { |
| 178 | + self.effect = effect |
| 179 | + } |
| 180 | + |
| 181 | + func initializing(context: Context) { |
| 182 | + effect?.initializing(context: context) |
| 183 | + } |
| 184 | + |
| 185 | + func initialized(context: Context) { |
| 186 | + effect?.initialized(context: context) |
| 187 | + } |
| 188 | + |
| 189 | + func updated(context: Context) { |
| 190 | + effect?.updated(context: context) |
| 191 | + } |
| 192 | + |
| 193 | + func released(context: Context) { |
| 194 | + effect?.released(context: context) |
| 195 | + } |
| 196 | + } |
| 197 | + |
| 198 | + struct LimitedAvailabilityEffect: AtomEffect { |
| 199 | + private let effect: any AtomEffect |
| 200 | + |
| 201 | + init(_ effect: any AtomEffect) { |
| 202 | + self.effect = effect |
| 203 | + } |
| 204 | + |
| 205 | + func initializing(context: Context) { |
| 206 | + effect.initializing(context: context) |
| 207 | + } |
| 208 | + |
| 209 | + func initialized(context: Context) { |
| 210 | + effect.initialized(context: context) |
| 211 | + } |
| 212 | + |
| 213 | + func updated(context: Context) { |
| 214 | + effect.updated(context: context) |
| 215 | + } |
| 216 | + |
| 217 | + func released(context: Context) { |
| 218 | + effect.released(context: context) |
| 219 | + } |
| 220 | + } |
| 221 | +} |
0 commit comments