From 56ce335d299feecf1c253b0febcfd4fb40a9f28d Mon Sep 17 00:00:00 2001 From: David Roman <2538074+davdroman@users.noreply.github.com> Date: Sun, 4 Jun 2023 16:25:46 +0100 Subject: [PATCH] wip --- Sources/IntrospectionSelector.swift | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/Sources/IntrospectionSelector.swift b/Sources/IntrospectionSelector.swift index ea691aa..d6b9938 100644 --- a/Sources/IntrospectionSelector.swift +++ b/Sources/IntrospectionSelector.swift @@ -1,8 +1,10 @@ @_spi(Internals) public struct IntrospectionSelector { - 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) public static func from(_ entryType: Entry.Type, selector: @escaping (Entry) -> Target?) -> Self { @@ -43,8 +45,11 @@ public struct IntrospectionSelector { } } - init(_ selector: @escaping (IntrospectionPlatformViewController, IntrospectionScope, IntrospectionAnchorID) -> Target?) { - self.selector = selector + init( + _ selector: @escaping (IntrospectionPlatformViewController, IntrospectionScope, IntrospectionAnchorID) -> Target? + ) { + self.receiver = { selector($0, .receiver, $1) } + self.ancestor = { selector($0, .ancestor, $1) } } func callAsFunction( @@ -52,6 +57,18 @@ public struct IntrospectionSelector { _ scope: IntrospectionScope, _ anchorID: IntrospectionAnchorID ) -> 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 } }