1 2 /** 3 A protocol that must be implemented to be a part of module's definition and the module definition itself. 4 */ 5 public protocol AnyDefinition {} 6 7 /** 8 The definition of the module. It is used to define some parameters 9 of the module and what it exports to the JavaScript world. 10 See `ModuleDefinitionBuilder` for more details on how to create it. 11 */ 12 public final class ModuleDefinition: ObjectDefinition { 13 /** 14 The module's type associated with the definition. It's used to create the module instance. 15 */ 16 var type: AnyModule.Type? 17 18 /** 19 Name of the defined module. Falls back to the type name if not provided in the definition. 20 */ 21 var name: String 22 23 let eventListeners: [EventListener] 24 let viewManager: ViewManagerDefinition? 25 26 /** 27 Names of the events that the module can send to JavaScript. 28 */ 29 let eventNames: [String] 30 31 /** 32 Initializer that is called by the `ModuleDefinitionBuilder` results builder. 33 */ 34 override init(definitions: [AnyDefinition]) { 35 self.name = definitions 36 .compactMap { $0 as? ModuleNameDefinition } 37 .last? 38 .name ?? "" 39 40 self.eventListeners = definitions.compactMap { $0 as? EventListener } 41 42 self.viewManager = definitions 43 .compactMap { $0 as? ViewManagerDefinition } 44 .last 45 46 self.eventNames = Array( 47 definitions 48 .compactMap { ($0 as? EventsDefinition)?.names } 49 .joined() 50 ) 51 52 super.init(definitions: definitions) 53 } 54 55 /** 56 Sets the module type that the definition is associated with. We can't pass this in the initializer 57 as it's called by the results builder that doesn't have access to the type. 58 */ 59 func withType(_ type: AnyModule.Type) -> Self { 60 self.type = type 61 62 // Use the type name if the name is not in the definition or was defined empty. 63 if name.isEmpty { 64 name = String(describing: type) 65 } 66 return self 67 } 68 } 69 70 /** 71 Module's name definition. Returned by `name()` in module's definition. 72 */ 73 internal struct ModuleNameDefinition: AnyDefinition { 74 let name: String 75 } 76 77 /** 78 A definition for module's constants. Returned by `constants(() -> SomeType)` in module's definition. 79 */ 80 internal struct ConstantsDefinition: AnyDefinition { 81 let body: () -> [String: Any?] 82 } 83 84 /** 85 A definition for module's events that can be sent to JavaScript. 86 */ 87 internal struct EventsDefinition: AnyDefinition { 88 let names: [String] 89 } 90