Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hotfix: infinity-loop in @Published property #195

Merged
merged 1 commit into from
Jun 20, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 40 additions & 2 deletions Sources/Core/PrettyDescriber.swift
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,6 @@ struct PrettyDescriber {
let typeName = String(describing: target.self)

let propertyWrappers: [(String, String)] = [
("Published", "currentValue"),
("StateObject", "wrappedValue"),
("ObservedObject", "wrappedValue"),
("EnvironmentObject", "_store"),
Expand All @@ -209,7 +208,7 @@ struct PrettyDescriber {
for (type, key) in propertyWrappers {
if typeName.hasPrefix("\(type)<"), let value = lookup(key, from: target) {
if debug {
// e.g. `@Published(42)`
// e.g. `@State(42)`
return "@\(type)(\(__string(value)))"
} else {
return __string(value)
Expand All @@ -227,6 +226,22 @@ struct PrettyDescriber {
return formatter.objectString(typeName: "@\(name)", fields: objectFields(target, debug: debug))
}

//
// @Published
//
// Note:
// Direct lookups because some data structures infinity-loop with circular references.
//
if typeName.hasPrefix("Published") {
let value = lookup(keyPath: ["storage", "publisher", "subject", "currentValue"], from: target)
if debug {
// e.g. `@Published(42)`
return "@Published\(__string(value)))"
} else {
return __string(value)
}
}

//
// @Namespace
//
Expand Down Expand Up @@ -472,6 +487,29 @@ struct PrettyDescriber {
return nil
}

private func lookup(keyPath: [String], from: Any) -> Any? {
var target: Any = from

for key in keyPath {
if let value = lookup(key: key, from: target) {
target = value
} else {
return nil
}
}

return target
}

private func lookup(key: String, from target: Any) -> Any? {
for child in Mirror(reflecting: target).children {
if let label = child.label, label == key {
return child.value
}
}
return nil
}

private func handleError(_ f: () throws -> String) -> String {
do {
return try f()
Expand Down