|
| 1 | +// |
| 2 | +// AnyReactor.swift |
| 3 | +// GoodReactor |
| 4 | +// |
| 5 | +// Created by Filip Šašala on 24/09/2025. |
| 6 | +// |
| 7 | + |
| 8 | +import Observation |
| 9 | + |
| 10 | +@available(iOS 17.0, macOS 14.0, *) |
| 11 | +@MainActor @Observable @dynamicMemberLookup public final class AnyReactor<WrappedAction: Sendable, WrappedMutation: Sendable, WrappedDestination: Sendable, WrappedState>: Reactor { |
| 12 | + |
| 13 | + // MARK: - Type aliases |
| 14 | + |
| 15 | + public typealias Action = WrappedAction |
| 16 | + public typealias Mutation = WrappedMutation |
| 17 | + public typealias Destination = WrappedDestination |
| 18 | + public typealias State = WrappedState |
| 19 | + |
| 20 | + // MARK: - Forwarders |
| 21 | + |
| 22 | + private let _getState: () -> State |
| 23 | + private let _setState: (State) -> () |
| 24 | + private let _initialStateBuilder: () -> State |
| 25 | + private let _sendAction: (Action) -> () |
| 26 | + private let _sendActionAsync: (Action) async -> () |
| 27 | + private let _getDestination: () -> Destination? |
| 28 | + private let _sendDestination: (Destination?) -> () |
| 29 | + private let _reduce: (inout State, Event<WrappedAction, WrappedMutation, WrappedDestination>) -> () |
| 30 | + private let _transform: () -> () |
| 31 | + |
| 32 | + // MARK: - Initialization |
| 33 | + |
| 34 | + public init<R: Reactor>(_ base: R) where |
| 35 | + R.Action == Action, |
| 36 | + R.Mutation == Mutation, |
| 37 | + R.Destination == Destination, |
| 38 | + R.State == State |
| 39 | + { |
| 40 | + self._getState = { base.state } |
| 41 | + self._setState = { base.state = $0 } |
| 42 | + self._initialStateBuilder = { base.makeInitialState() } |
| 43 | + self._sendAction = { base.send(action: $0) } |
| 44 | + self._sendActionAsync = { await base.send(action: $0) } |
| 45 | + self._getDestination = { base.destination } |
| 46 | + self._sendDestination = { base.send(destination: $0) } |
| 47 | + self._reduce = { base.reduce(state: &$0, event: $1) } |
| 48 | + self._transform = { base.transform() } |
| 49 | + } |
| 50 | + |
| 51 | +} |
| 52 | + |
| 53 | +// MARK: - Dynamic member lookup |
| 54 | + |
| 55 | +@available(iOS 17.0, macOS 14.0, *) |
| 56 | +public extension AnyReactor { |
| 57 | + |
| 58 | + subscript<T>(dynamicMember keyPath: KeyPath<State, T>) -> T { |
| 59 | + _getState()[keyPath: keyPath] |
| 60 | + } |
| 61 | + |
| 62 | + subscript<T>(dynamicMember keyPath: ReferenceWritableKeyPath<State, T>) -> T { |
| 63 | + _getState()[keyPath: keyPath] |
| 64 | + } |
| 65 | + |
| 66 | +} |
| 67 | + |
| 68 | +// MARK: - Reactor |
| 69 | + |
| 70 | +@available(iOS 17.0, macOS 14.0, *) |
| 71 | +public extension AnyReactor { |
| 72 | + |
| 73 | + func makeInitialState() -> State { |
| 74 | + _initialStateBuilder() |
| 75 | + } |
| 76 | + |
| 77 | + func transform() { |
| 78 | + _transform() |
| 79 | + } |
| 80 | + |
| 81 | + func reduce(state: inout State, event: Event<WrappedAction, WrappedMutation, WrappedDestination>) { |
| 82 | + _reduce(&state, event) |
| 83 | + } |
| 84 | + |
| 85 | + var destination: Destination? { |
| 86 | + get { |
| 87 | + _getDestination() |
| 88 | + } |
| 89 | + set { |
| 90 | + _sendDestination(newValue) |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + var initialState: State { |
| 95 | + _initialStateBuilder() |
| 96 | + } |
| 97 | + |
| 98 | +} |
| 99 | + |
| 100 | +// MARK: - Mirror |
| 101 | + |
| 102 | +@available(iOS 17.0, macOS 14.0, *) |
| 103 | +public extension AnyReactor { |
| 104 | + |
| 105 | + func send(action: Action) { |
| 106 | + _sendAction(action) |
| 107 | + } |
| 108 | + |
| 109 | + func send(action: Action) async { |
| 110 | + await _sendActionAsync(action) |
| 111 | + } |
| 112 | + |
| 113 | + func send(destination: Destination?) { |
| 114 | + _sendDestination(destination) |
| 115 | + } |
| 116 | + |
| 117 | +} |
| 118 | + |
| 119 | +// MARK: - Eraser |
| 120 | + |
| 121 | +@available(iOS 17.0, macOS 14.0, *) |
| 122 | +public extension Reactor { |
| 123 | + |
| 124 | + func eraseToAnyReactor() -> AnyReactor<Action, Mutation, Destination, State> { |
| 125 | + AnyReactor(self) |
| 126 | + } |
| 127 | + |
| 128 | +} |
0 commit comments