1 // Copyright 2022-present 650 Industries. All rights reserved. 2 3 /** 4 An enum that identifies lifecycle method types. 5 */ 6 internal enum ViewLifecycleMethodType { 7 case didUpdateProps 8 } 9 10 /** 11 Type-erased protocol for all view lifecycle methods. 12 */ 13 internal protocol AnyViewLifecycleMethod: AnyDefinition { 14 /** 15 Type of the lifecycle method. 16 */ 17 var type: ViewLifecycleMethodType { get } 18 19 /** 20 Calls the lifecycle method for the given view. 21 */ callAsFunctionnull22 func callAsFunction(_ view: UIView) 23 } 24 25 /** 26 Element of the view definition that represents a lifecycle method, such as `OnViewDidUpdateProps`. 27 */ 28 public final class ViewLifecycleMethod<ViewType: UIView>: AnyViewLifecycleMethod { 29 /** 30 The actual closure that gets called when the view signals an event in view's lifecycle. 31 */ 32 let closure: (ViewType) -> Void 33 34 let type: ViewLifecycleMethodType 35 36 init(type: ViewLifecycleMethodType, closure: @escaping (ViewType) -> Void) { 37 self.type = type 38 self.closure = closure 39 } 40 41 func callAsFunction(_ view: UIView) { 42 guard let view = view as? ViewType else { 43 log.warn("Cannot call lifecycle method '\(type)', given view is not of type '\(ViewType.self)'") 44 return 45 } 46 closure(view) 47 } 48 } 49