This commit is contained in:
David Roman 2023-06-04 16:25:46 +01:00
parent 94ab0068fc
commit 56ce335d29
No known key found for this signature in database
GPG Key ID: 7058646EEFCB70A7
1 changed files with 22 additions and 5 deletions

View File

@ -1,8 +1,10 @@
@_spi(Internals) @_spi(Internals)
public struct IntrospectionSelector<Target: PlatformEntity> { public struct IntrospectionSelector<Target: PlatformEntity> {
private let selector: (IntrospectionPlatformViewController, IntrospectionScope, IntrospectionAnchorID) -> Target? private var receiver: (IntrospectionPlatformViewController, IntrospectionAnchorID) -> Target?
private var ancestor: (IntrospectionPlatformViewController, IntrospectionAnchorID) -> Target?
static var `default`: Self { .from(Target.self, selector: { $0 }) } @_spi(Internals)
public static var `default`: Self { .from(Target.self, selector: { $0 }) }
@_spi(Internals) @_spi(Internals)
public static func from<Entry: PlatformEntity>(_ entryType: Entry.Type, selector: @escaping (Entry) -> Target?) -> Self { public static func from<Entry: PlatformEntity>(_ entryType: Entry.Type, selector: @escaping (Entry) -> Target?) -> Self {
@ -43,8 +45,11 @@ public struct IntrospectionSelector<Target: PlatformEntity> {
} }
} }
init(_ selector: @escaping (IntrospectionPlatformViewController, IntrospectionScope, IntrospectionAnchorID) -> Target?) { init(
self.selector = selector _ selector: @escaping (IntrospectionPlatformViewController, IntrospectionScope, IntrospectionAnchorID) -> Target?
) {
self.receiver = { selector($0, .receiver, $1) }
self.ancestor = { selector($0, .ancestor, $1) }
} }
func callAsFunction( func callAsFunction(
@ -52,6 +57,18 @@ public struct IntrospectionSelector<Target: PlatformEntity> {
_ scope: IntrospectionScope, _ scope: IntrospectionScope,
_ anchorID: IntrospectionAnchorID _ anchorID: IntrospectionAnchorID
) -> Target? { ) -> Target? {
selector(controller, scope, anchorID) if
scope.contains(.receiver),
let target = receiver(controller, anchorID)
{
return target
}
if
scope.contains(.ancestor),
let target = ancestor(controller, anchorID)
{
return target
}
return nil
} }
} }