Created
July 31, 2022 01:36
-
-
Save IanKeen/5a42d1d00feb5092c8fa6b0cf63ecdc4 to your computer and use it in GitHub Desktop.
PropertyWrapper: @invalidating backport
This file contains 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
struct Invalidation { | |
static let display = Invalidation { $0.setNeedsDisplay() } | |
static let layout = Invalidation { $0.setNeedsLayout() } | |
let action: (UIView) -> Void | |
} | |
@propertyWrapper | |
struct Invalidating<Value> { | |
private let invalidations: [Invalidation] | |
var wrappedValue: Value | |
init(wrappedValue: Value, _ invalidations: Invalidation...) { | |
self.wrappedValue = wrappedValue | |
self.invalidations = invalidations | |
} | |
static subscript<Instance: UIView>( | |
_enclosingInstance instance: Instance, | |
wrapped wrappedKeyPath: ReferenceWritableKeyPath<Instance, Value>, | |
storage storageKeyPath: ReferenceWritableKeyPath<Instance, Self> | |
) -> Value { | |
get { instance[keyPath: storageKeyPath].wrappedValue } | |
set { | |
instance[keyPath: storageKeyPath].wrappedValue = newValue | |
instance[keyPath: storageKeyPath].invalidations.forEach { $0.action(instance) } | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment