@@ -20,68 +20,70 @@ public class Caravel: NSObject, UIWebViewDelegate {
2020 /**
2121 * Default Bus
2222 */
23- private static var _default : Caravel ?
24- private static var _buses : [ Caravel ] = [ Caravel] ( )
23+ private static var defaultBus : Caravel ?
24+ private static var buses : [ Caravel ] = [ Caravel] ( )
2525
26- private var _name : String
26+ private var secretName : String
2727
2828 /**
2929 * Bus subscribers
3030 */
31- private lazy var _subscribers : [ Subscriber ] = [ Subscriber] ( )
31+ private lazy var subscribers : [ Subscriber ] = [ Subscriber] ( )
3232
3333
3434 /**
3535 * Denotes if the bus has received the init event from JS
3636 */
37- private var _isInitialized : Bool
37+ private var isInitialized : Bool
3838
3939 // Multithreading locks
40- private static var _defaultInitLock = NSObject ( )
41- private static var _namedBusInitLock = NSObject ( )
40+ private static var defaultInitLock = NSObject ( )
41+ private static var namedBusInitLock = NSObject ( )
4242
4343 /**
4444 * Pending initialization subscribers
4545 */
46- private lazy var _initializers : [ ( Caravel ) -> Void ] = [ ]
46+ private lazy var initializers : [ ( Caravel ) -> Void ] = [ ]
4747 // Initializers are temporary saved in order to prevent them to be garbage
4848 // collected
49- private lazy var _onGoingInitializers : [ Int : ( ( Caravel ) -> Void ) ] = [ : ]
50- private lazy var _onGoingInitializersId = 0
49+ private lazy var onGoingInitializers : [ Int : ( ( Caravel ) -> Void ) ] = [ : ]
50+ private lazy var onGoingInitializersId = 0
5151
52- private var _webView : UIWebView
52+ private var webView : UIWebView
5353
5454 private init ( name: String , webView: UIWebView ) {
55- self . _name = name
56- self . _isInitialized = false
57- self . _webView = webView
55+ self . secretName = name
56+ self . isInitialized = false
57+ self . webView = webView
5858
5959 super. init ( )
6060
61- UIWebViewDelegateMediator . subscribe ( self . _webView , subscriber: self )
61+ UIWebViewDelegateMediator . subscribe ( self . webView , subscriber: self )
6262 }
6363
6464 /**
6565 * Sends event to JS
6666 */
67- private func _post( eventName: String , eventData: AnyObject ? , type: SupportedType ? ) {
68- var toRun : String ?
69- var data : String ?
70-
71- if let d: AnyObject = eventData {
72- data = DataSerializer . serialize ( d, type: type!)
73- } else {
74- data = " null "
75- }
76-
77- if _name == " default " {
78- toRun = " Caravel.getDefault().raise( \" \( eventName) \" , \( data!) ) "
79- } else {
80- toRun = " Caravel.get( \" \( _name) \" ).raise( \" \( eventName) \" , \( data!) ) "
81- }
82-
83- dispatch_async ( dispatch_get_main_queue ( ) ) {
84- self . _webView. stringByEvaluatingJavaScriptFromString ( toRun!)
67+ private func post( eventName: String , eventData: AnyObject ? , type: SupportedType ? ) {
68+ dispatch_async ( dispatch_get_global_queue ( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0 ) ) {
69+ var toRun : String ?
70+ var data : String ?
71+
72+ if let d: AnyObject = eventData {
73+ data = DataSerializer . serialize ( d, type: type!)
74+ } else {
75+ data = " null "
76+ }
77+
78+ if self . secretName == Caravel . DEFAULT_BUS_NAME {
79+ toRun = " Caravel.getDefault().raise( \" \( eventName) \" , \( data!) ) "
80+ } else {
81+ toRun = " Caravel.get( \" \( self . secretName) \" ).raise( \" \( eventName) \" , \( data!) ) "
82+ }
83+
84+ dispatch_async ( dispatch_get_main_queue ( ) ) {
85+ self . webView. stringByEvaluatingJavaScriptFromString ( toRun!)
86+ }
8587 }
8688 }
8789
@@ -90,26 +92,26 @@ public class Caravel: NSObject, UIWebViewDelegate {
9092 * Caravel has to watch this new component again
9193 */
9294 internal func setWebView( webView: UIWebView ) {
93- if webView. hash == _webView . hash {
95+ if webView. hash == self . webView . hash {
9496 return
9597 }
9698 // whenReady() should be triggered only after a CaravelInit event
9799 // has been raised (aka wait for JS before calling whenReady)
98- _isInitialized = false
99- _webView = webView
100- UIWebViewDelegateMediator . subscribe ( _webView , subscriber: self )
100+ self . isInitialized = false
101+ self . webView = webView
102+ UIWebViewDelegateMediator . subscribe ( self . webView , subscriber: self )
101103 }
102104
103105 internal func synchronized( action: ( ) -> Void ) {
104- let lock = ( _name == Caravel . DEFAULT_BUS_NAME) ? Caravel . _defaultInitLock : Caravel . _namedBusInitLock
106+ let lock = ( self . secretName == Caravel . DEFAULT_BUS_NAME) ? Caravel . defaultInitLock : Caravel . namedBusInitLock
105107
106108 objc_sync_enter ( lock)
107109 action ( )
108110 objc_sync_exit ( lock)
109111 }
110112
111113 public var name : String {
112- return _name
114+ return self . secretName
113115 }
114116
115117 /**
@@ -126,25 +128,25 @@ public class Caravel: NSObject, UIWebViewDelegate {
126128
127129 // All buses are notified about that incoming event. Then, they need to investigate first if they
128130 // are potential receivers
129- if _name == args. busName {
131+ if self . secretName == args. busName {
130132 if args. eventName == " CaravelInit " { // Reserved event name. Triggers whenReady
131- if !_isInitialized {
132- synchronized ( ) {
133- if !self . _isInitialized {
134- self . _isInitialized = true
133+ if !self . isInitialized {
134+ self . synchronized {
135+ if !self . isInitialized {
136+ self . isInitialized = true
135137
136- for i in self . _initializers {
137- let index = self . _onGoingInitializersId
138+ for i in self . initializers {
139+ let index = self . onGoingInitializersId
138140
139- self . _onGoingInitializers [ index] = i
140- self . _onGoingInitializersId ++
141+ self . onGoingInitializers [ index] = i
142+ self . onGoingInitializersId ++
141143
142144 dispatch_async ( dispatch_get_main_queue ( ) ) {
143145 i ( self )
144- self . _onGoingInitializers . removeValueForKey ( index)
146+ self . onGoingInitializers . removeValueForKey ( index)
145147 }
146148 }
147- self . _initializers = Array < ( Caravel ) -> Void > ( )
149+ self . initializers = Array < ( Caravel ) -> Void > ( )
148150 }
149151 }
150152 }
@@ -155,7 +157,7 @@ public class Caravel: NSObject, UIWebViewDelegate {
155157 eventData = DataSerializer . deserialize ( d)
156158 }
157159
158- for s in _subscribers {
160+ for s in self . subscribers {
159161 if s. name == args. eventName {
160162 dispatch_async ( dispatch_get_main_queue ( ) ) {
161163 s. callback ( args. eventName, eventData)
@@ -179,18 +181,18 @@ public class Caravel: NSObject, UIWebViewDelegate {
179181 * Returns the current bus when its JS counterpart is ready
180182 */
181183 public func whenReady( callback: ( Caravel ) -> Void ) {
182- if _isInitialized {
184+ if self . isInitialized {
183185 dispatch_async ( dispatch_get_main_queue ( ) ) {
184186 callback ( self )
185187 }
186188 } else {
187- synchronized ( ) {
188- if self . _isInitialized {
189+ self . synchronized {
190+ if self . self . isInitialized {
189191 dispatch_async ( dispatch_get_main_queue ( ) ) {
190192 callback ( self )
191193 }
192194 } else {
193- self . _initializers . append ( callback)
195+ self . initializers . append ( callback)
194196 }
195197 }
196198 }
@@ -200,71 +202,71 @@ public class Caravel: NSObject, UIWebViewDelegate {
200202 * Posts event without any argument
201203 */
202204 public func post( eventName: String ) {
203- _post ( eventName, eventData: nil , type: nil )
205+ self . post ( eventName, eventData: nil , type: nil )
204206 }
205207
206208 /**
207209 * Posts event with an extra int
208210 */
209211 public func post( eventName: String , anInt: Int ) {
210- _post ( eventName, eventData: anInt, type: . Int)
212+ self . post ( eventName, eventData: anInt, type: . Int)
211213 }
212214
213215 /**
214216 * Posts event with an extra bool
215217 */
216218 public func post( eventName: String , aBool: Bool ) {
217- _post ( eventName, eventData: aBool, type: . Bool)
219+ self . post ( eventName, eventData: aBool, type: . Bool)
218220 }
219221
220222 /**
221223 * Posts event with an extra double
222224 */
223225 public func post( eventName: String , aDouble: Double ) {
224- _post ( eventName, eventData: aDouble, type: . Double)
226+ self . post ( eventName, eventData: aDouble, type: . Double)
225227 }
226228
227229 /**
228230 * Posts event with an extra float
229231 */
230232 public func post( eventName: String , aFloat: Float ) {
231- _post ( eventName, eventData: aFloat, type: . Float)
233+ self . post ( eventName, eventData: aFloat, type: . Float)
232234 }
233235
234236 /**
235237 * Posts event with an extra string
236238 */
237239 public func post( eventName: String , aString: String ) {
238- _post ( eventName, eventData: aString, type: . String)
240+ self . post ( eventName, eventData: aString, type: . String)
239241 }
240242
241243 /**
242244 * Posts event with an extra array
243245 */
244246 public func post( eventName: String , anArray: NSArray ) {
245- _post ( eventName, eventData: anArray, type: . Array)
247+ self . post ( eventName, eventData: anArray, type: . Array)
246248 }
247249
248250 /**
249251 * Posts event with an extra dictionary
250252 */
251253 public func post( eventName: String , aDictionary: NSDictionary ) {
252- _post ( eventName, eventData: aDictionary, type: . Dictionary)
254+ self . post ( eventName, eventData: aDictionary, type: . Dictionary)
253255 }
254256
255257 /**
256258 * Subscribes to provided event. Callback is run with the event's name and extra data
257259 */
258260 public func register( eventName: String , callback: ( String , AnyObject ? ) -> Void ) {
259- _subscribers . append ( Subscriber ( name: eventName, callback: callback) )
261+ self . subscribers . append ( Subscriber ( name: eventName, callback: callback) )
260262 }
261263
262264 /**
263265 * Returns the default bus
264266 */
265267 public static func getDefault( webView: UIWebView ) -> Caravel {
266268 let getExisting = { ( ) -> Caravel ? in
267- if let b = Caravel . _default {
269+ if let b = Caravel . defaultBus {
268270 b. setWebView ( webView)
269271 return b
270272 } else {
@@ -276,14 +278,14 @@ public class Caravel: NSObject, UIWebViewDelegate {
276278 return bus
277279 } else {
278280 // setWebView must be run within a synchronized block
279- objc_sync_enter ( Caravel . _defaultInitLock )
281+ objc_sync_enter ( Caravel . defaultInitLock )
280282 if let bus = getExisting ( ) {
281- objc_sync_exit ( Caravel . _defaultInitLock )
283+ objc_sync_exit ( Caravel . defaultInitLock )
282284 return bus
283285 } else {
284- _default = Caravel ( name: Caravel . DEFAULT_BUS_NAME, webView: webView)
285- objc_sync_exit ( Caravel . _defaultInitLock )
286- return _default !
286+ self . defaultBus = Caravel ( name: Caravel . DEFAULT_BUS_NAME, webView: webView)
287+ objc_sync_exit ( Caravel . defaultInitLock )
288+ return self . defaultBus !
287289 }
288290 }
289291 }
@@ -296,7 +298,7 @@ public class Caravel: NSObject, UIWebViewDelegate {
296298 return getDefault ( webView)
297299 } else {
298300 let getExisting = { ( ) -> Caravel ? in
299- for b in self . _buses {
301+ for b in self . buses {
300302 if b. name == name {
301303 b. setWebView ( webView)
302304 return b
@@ -310,14 +312,14 @@ public class Caravel: NSObject, UIWebViewDelegate {
310312 return bus
311313 } else {
312314 // setWebView must be run within a synchronized block
313- objc_sync_enter ( Caravel . _namedBusInitLock )
315+ objc_sync_enter ( Caravel . namedBusInitLock )
314316 if let bus = getExisting ( ) {
315- objc_sync_exit ( Caravel . _namedBusInitLock )
317+ objc_sync_exit ( Caravel . namedBusInitLock )
316318 return bus
317319 } else {
318320 let newBus = Caravel ( name: name, webView: webView)
319- _buses . append ( newBus)
320- objc_sync_exit ( Caravel . _namedBusInitLock )
321+ self . buses . append ( newBus)
322+ objc_sync_exit ( Caravel . namedBusInitLock )
321323 return newBus
322324 }
323325 }
0 commit comments