feat: Refactors / moves into individual files, wip.

This commit is contained in:
2024-05-30 19:52:15 -04:00
parent 11ddfb04c3
commit bd0f90894f
6 changed files with 202 additions and 104 deletions

View File

@@ -4,13 +4,7 @@ import OSLog
public enum OnFailAction<State, Action> {
case fail(prefix: String? = nil, log: ((String) -> Void)? = nil)
case handle((inout State, Error) -> Void)
@available(macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0, *)
@inlinable
static func fail(prefix: String? = nil, logger: Logger) -> Self {
.fail(prefix: prefix, log: { logger.error("\($0)") })
}
case operation((inout State, Error) -> Effect<Action>)
@usableFromInline
func callAsFunction(state: inout State, error: Error) -> Effect<Action> {
@@ -21,10 +15,45 @@ public enum OnFailAction<State, Action> {
} else {
return .fail(error: error, log: log)
}
case let .handle(handler):
handler(&state, error)
return .none
case let .operation(handler):
return handler(&state, error)
}
}
@available(macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0, *)
@inlinable
static func fail(prefix: String? = nil, logger: Logger) -> Self {
.fail(prefix: prefix, log: { logger.error("\($0)") })
}
@inlinable
public static func set(
_ operation: @escaping @Sendable (inout State, Error) -> Void
) -> Self {
.operation(
SetAction.operation(operation).callAsFunction(state:value:)
)
}
@inlinable
public static func set(
_ operation: @escaping @Sendable (inout State, Error) -> Effect<Action>
) -> Self {
.operation(
SetAction.operation(f: operation).callAsFunction(state:value:)
)
}
@inlinable
public static func set(
keyPath: WritableKeyPath<State, Error?>,
effect: Effect<Action> = .none
) -> Self {
.operation(
SetAction.optionalKeyPath(
keyPath,
effect: effect
).callAsFunction(state:value:)
)
}
}