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: AnyDefinition {
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 methods: [String : AnyMethod]
24   let constants: [String : Any?]
25   let eventListeners: [EventListener]
26   let viewManager: ViewManagerDefinition?
27 
28   /**
29    Names of the events that the module can send to JavaScript.
30    */
31   let eventNames: [String]
32 
33   /**
34    Initializer that is called by the `ModuleDefinitionBuilder` results builder.
35    */
36   init(definitions: [AnyDefinition]) {
37     self.name = definitions
38       .compactMap { $0 as? ModuleNameDefinition }
39       .last?
40       .name ?? ""
41 
42     self.methods = definitions
43       .compactMap { $0 as? AnyMethod }
44       .reduce(into: [String : AnyMethod]()) { dict, method in
45         dict[method.name] = method
46       }
47 
48     self.constants = definitions
49       .compactMap { $0 as? ConstantsDefinition }
50       .reduce(into: [String : Any?]()) { dict, definition in
51         dict.merge(definition.constants) { $1 }
52       }
53 
54     self.eventListeners = definitions.compactMap { $0 as? EventListener }
55 
56     self.viewManager = definitions
57       .compactMap { $0 as? ViewManagerDefinition }
58       .last
59 
60     self.eventNames = Array(
61       definitions
62         .compactMap { ($0 as? EventsDefinition)?.names }
63         .joined()
64     )
65   }
66 
67   /**
68    Sets the module type that the definition is associated with. We can't pass this in the initializer
69    as it's called by the results builder that doesn't have access to the type.
70    */
71   func withType(_ type: AnyModule.Type) -> Self {
72     self.type = type
73 
74     // Use the type name if the name is not in the definition or was defined empty.
75     if name.isEmpty {
76       name = String(describing: type)
77     }
78     return self
79   }
80 }
81 
82 /**
83  Module's name definition. Returned by `name()` in module's definition.
84  */
85 internal struct ModuleNameDefinition: AnyDefinition {
86   let name: String
87 }
88 
89 /**
90  A definition for module's constants. Returned by `constants(() -> SomeType)` in module's definition.
91  */
92 internal struct ConstantsDefinition: AnyDefinition {
93   let constants: [String : Any?]
94 }
95 
96 /**
97  A definition for module's events that can be sent to JavaScript.
98  */
99 internal struct EventsDefinition: AnyDefinition {
100   let names: [String]
101 }
102