1 import UIKit
2 
3 /**
4  Extends all modules with the functions used to build a module definition.
5  Unfortunately they need to be scoped here, but hopefully this proposal
6  https://github.com/apple/swift-evolution/blob/main/proposals/0289-result-builders.md#builder-scoped-name-lookup
7  will be implemented in the future.
8  */
9 extension AnyModule {
10   // MARK: - Module name
11 
12   /**
13    Sets the name of the module that is exported to the JavaScript world.
14    */
15   @available(*, deprecated, renamed: "Name")
16   public func name(_ name: String) -> AnyDefinition {
17     return ModuleNameDefinition(name: name)
18   }
19 
20   // MARK: - Module's lifecycle
21 
22   /**
23    Creates module's lifecycle listener that is called right after module initialization.
24    */
25   @available(*, deprecated, renamed: "OnCreate")
26   public func onCreate(_ closure: @escaping () -> Void) -> AnyDefinition {
27     return EventListener(.moduleCreate, closure)
28   }
29 
30   /**
31    Creates module's lifecycle listener that is called when the module is about to be deallocated.
32    */
33   @available(*, deprecated, renamed: "OnDestroy")
34   public func onDestroy(_ closure: @escaping () -> Void) -> AnyDefinition {
35     return EventListener(.moduleDestroy, closure)
36   }
37 
38   /**
39    Creates module's lifecycle listener that is called when the app context owning the module is about to be deallocated.
40    */
41   @available(*, deprecated, renamed: "OnAppContextDestroys")
42   public func onAppContextDestroys(_ closure: @escaping () -> Void) -> AnyDefinition {
43     return EventListener(.appContextDestroys, closure)
44   }
45 
46   /**
47    Creates a listener that is called when the app is about to enter the foreground mode.
48    */
49   @available(*, deprecated, renamed: "OnAppEntersBackground")
50   public func onAppEntersForeground(_ closure: @escaping () -> Void) -> AnyDefinition {
51     return EventListener(.appEntersForeground, closure)
52   }
53 
54   /**
55    Creates a listener that is called when the app becomes active again.
56    */
57   @available(*, deprecated, renamed: "OnAppBecomesActive")
58   public func onAppBecomesActive(_ closure: @escaping () -> Void) -> AnyDefinition {
59     return EventListener(.appBecomesActive, closure)
60   }
61 
62   /**
63    Creates a listener that is called when the app enters the background mode.
64    */
65   @available(*, deprecated, renamed: "OnAppEntersBackground")
66   public func onAppEntersBackground(_ closure: @escaping () -> Void) -> AnyDefinition {
67     return EventListener(.appEntersBackground, closure)
68   }
69 
70   // MARK: - View Manager
71 
72   /**
73    Creates the view manager definition that scopes other view-related definitions.
74    */
75   @available(*, deprecated, renamed: "ViewManager")
76   public func viewManager(@ViewManagerDefinitionBuilder _ closure: @escaping () -> ViewManagerDefinition) -> AnyDefinition {
77     return closure()
78   }
79 }
80 
81 // MARK: - Module name
82 
83 /**
84  Sets the name of the module that is exported to the JavaScript world.
85  */
86 public func Name(_ name: String) -> AnyDefinition {
87   return ModuleNameDefinition(name: name)
88 }
89 
90 // MARK: - Module's lifecycle
91 
92 /**
93  Creates module's lifecycle listener that is called right after module initialization.
94  */
95 public func OnCreate(_ closure: @escaping () -> Void) -> AnyDefinition {
96   return EventListener(.moduleCreate, closure)
97 }
98 
99 /**
100  Creates module's lifecycle listener that is called when the module is about to be deallocated.
101  */
102 public func OnDestroy(_ closure: @escaping () -> Void) -> AnyDefinition {
103   return EventListener(.moduleDestroy, closure)
104 }
105 
106 /**
107  Creates module's lifecycle listener that is called when the app context owning the module is about to be deallocated.
108  */
109 public func OnAppContextDestroys(_ closure: @escaping () -> Void) -> AnyDefinition {
110   return EventListener(.appContextDestroys, closure)
111 }
112 
113 /**
114  Creates a listener that is called when the app is about to enter the foreground mode.
115  */
116 public func OnAppEntersForeground(_ closure: @escaping () -> Void) -> AnyDefinition {
117   return EventListener(.appEntersForeground, closure)
118 }
119 
120 /**
121  Creates a listener that is called when the app becomes active again.
122  */
123 public func OnAppBecomesActive(_ closure: @escaping () -> Void) -> AnyDefinition {
124   return EventListener(.appBecomesActive, closure)
125 }
126 
127 /**
128  Creates a listener that is called when the app enters the background mode.
129  */
130 public func OnAppEntersBackground(_ closure: @escaping () -> Void) -> AnyDefinition {
131   return EventListener(.appEntersBackground, closure)
132 }
133 
134 // MARK: - View Manager
135 
136 /**
137  Creates the view manager definition that scopes other view-related definitions.
138  */
139 public func ViewManager(@ViewManagerDefinitionBuilder _ closure: @escaping () -> ViewManagerDefinition) -> AnyDefinition {
140   return closure()
141 }
142