Skip to content

Commit

Permalink
Add Loggable protocol for state and dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
0xLeif committed Sep 19, 2024
1 parent 3b73abe commit 691b5de
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 5 deletions.
6 changes: 5 additions & 1 deletion Sources/AppState/Application/Application+internal.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ extension Application {
static var cacheDescription: String {
shared.cache.allValues
.map { key, value in
"\t- \(value)"
if let value = value as? Loggable {
"\t- \(value.logValue)"
} else {
"\t- \(value)"
}
}
.sorted(by: <)
.joined(separator: "\n")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ extension Application {
}

/// `Dependency` struct encapsulates dependencies used throughout the app.
public struct Dependency<Value: Sendable>: CustomStringConvertible, Sendable {
public struct Dependency<Value: Sendable>: Sendable, Loggable {
/// The dependency value.
var value: Value

Expand All @@ -28,7 +28,7 @@ extension Application {
self.scope = scope
}

public var description: String {
public var logValue: String {
"Dependency<\(Value.self)>(\(value)) (\(scope.key))"
}
}
Expand Down
4 changes: 4 additions & 0 deletions Sources/AppState/Application/Types/Helper/Loggable.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
protocol Loggable {
@MainActor
var logValue: String { get }
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Foundation

extension Application {
/// `State` encapsulates the value within the application's scope and allows any changes to be propagated throughout the scoped area.
public struct State<Value: Sendable>: MutableApplicationState, Sendable {
public struct State<Value: Sendable>: Sendable, MutableApplicationState, Loggable {
/// Values that are available in the cache.
enum StateType {
case state
Expand Down Expand Up @@ -98,7 +98,7 @@ extension Application {
}

@MainActor
public var description: String {
public var logValue: String {
switch type {
case .state: return "State<\(Value.self)>(\(value)) (\(scope.key))"
case .stored: return "StoredState<\(Value.self)>(\(value)) (\(scope.key))"
Expand Down

0 comments on commit 691b5de

Please sign in to comment.