Skip to content

Add DelegateForwarding module with DelegateProxy - #17

Merged
davdroman merged 8 commits into
mainfrom
feat/delegate-proxy
Nov 9, 2026
Merged

davdroman merged 8 commits into
mainfrom
feat/delegate-proxy

Conversation

@davdroman

Copy link
Copy Markdown
Owner

DelegateProxy<Delegate> takes an object's delegate slot and forwards every call it doesn't implement to the delegate it replaced.

@MainActor
final class DismissInterceptor: DelegateProxy<any UIAdaptivePresentationControllerDelegate>,
    UIAdaptivePresentationControllerDelegate
{
    var isDismissible = true

    func presentationControllerShouldDismiss(_ controller: UIPresentationController) -> Bool {
        isDismissible && forwardee?.presentationControllerShouldDismiss?(controller) ?? true
    }
}

interceptor.install(on: presentationController, at: \.delegate)

Reinstalling on a slot the proxy already holds is a no-op. Releasing the proxy gives the slot back unless it has been given another delegate since. If the replaced delegate goes away while the proxy is installed, calls the owner still makes to it are dropped instead of crashing on an unrecognized selector. Delegates a framework calls on its own threads, such as XMLParserDelegate, work too.

@davdroman
davdroman merged commit 2c3831d into main Nov 9, 2026
14 checks passed
@davdroman
davdroman deleted the feat/delegate-proxy branch November 9, 2026 19:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant