Last active
December 23, 2022 07:21
-
-
Save onevcat/3c8f7c4e8c96f288854688cf34111636 to your computer and use it in GitHub Desktop.
An auto-weak delegate for handle modern delegate pattern.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
/// A class that keeps a weakly reference for `self` when implementing `onXXX` behaviors. | |
/// Instead of remembering to keep `self` as weak in a stored closure: | |
/// | |
/// ```swift | |
/// // MyClass.swift | |
/// var onDone: (() -> Void)? | |
/// func done() { | |
/// onDone?() | |
/// } | |
/// | |
/// // ViewController.swift | |
/// var obj: MyClass? | |
/// | |
/// func doSomething() { | |
/// obj = MyClass() | |
/// obj!.onDone = { [weak self] in | |
/// self?.reportDone() | |
/// } | |
/// } | |
/// ``` | |
/// | |
/// You can create a `Delegate` and observe on `self`. Now, there is no retain cycle inside: | |
/// | |
/// ```swift | |
/// // MyClass.swift | |
/// let onDone = Delegate<(), Void>() | |
/// func done() { | |
/// onDone.call() | |
/// } | |
/// | |
/// // ViewController.swift | |
/// var obj: MyClass? | |
/// | |
/// func doSomething() { | |
/// obj = MyClass() | |
/// obj!.onDone.delegate(on: self) { (self, _) | |
/// // `self` here is shadowed and does not keep a strong ref. | |
/// // So you can release both `MyClass` instance and `ViewController` instance. | |
/// self.reportDone() | |
/// } | |
/// } | |
/// ``` | |
/// | |
public class Delegate<Input, Output> { | |
public init() {} | |
private var block: ((Input) -> Output?)? | |
public func delegate<T: AnyObject>(on target: T, block: ((T, Input) -> Output)?) { | |
self.block = { [weak target] input in | |
guard let target = target else { return nil } | |
return block?(target, input) | |
} | |
} | |
public func call(_ input: Input) -> Output? { | |
return block?(input) | |
} | |
public func callAsFunction(_ input: Input) -> Output? { | |
return call(input) | |
} | |
} | |
extension Delegate where Input == Void { | |
public func call() -> Output? { | |
return call(()) | |
} | |
public func callAsFunction() -> Output? { | |
return call() | |
} | |
} | |
extension Delegate where Input == Void, Output: OptionalProtocol { | |
public func call() -> Output { | |
return call(()) | |
} | |
public func callAsFunction() -> Output { | |
return call() | |
} | |
} | |
extension Delegate where Output: OptionalProtocol { | |
public func call(_ input: Input) -> Output { | |
if let result = block?(input) { | |
return result | |
} else { | |
return Output.createNil | |
} | |
} | |
public func callAsFunction(_ input: Input) -> Output { | |
return call(input) | |
} | |
} | |
public protocol OptionalProtocol { | |
static var createNil: Self { get } | |
} | |
extension Optional : OptionalProtocol { | |
public static var createNil: Optional<Wrapped> { | |
return nil | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment