Skip to content

Instantly share code, notes, and snippets.

@IanKeen
Created July 31, 2022 01:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save IanKeen/5a42d1d00feb5092c8fa6b0cf63ecdc4 to your computer and use it in GitHub Desktop.
Save IanKeen/5a42d1d00feb5092c8fa6b0cf63ecdc4 to your computer and use it in GitHub Desktop.
PropertyWrapper: @invalidating backport
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