@@ -45,22 +45,34 @@ internal class StateChangeCallbacks {
4545    private  set
4646
4747  /* * Safely adds an onOpen callback */ 
48-   fun  onOpen (ref :  String , callback :  () ->  Unit ) {
48+   fun  onOpen (
49+     ref :  String ,
50+     callback :  () ->  Unit 
51+   ) {
4952    this .open =  this .open +  Pair (ref, callback)
5053  }
5154
5255  /* * Safely adds an onClose callback */ 
53-   fun  onClose (ref :  String , callback :  () ->  Unit ) {
56+   fun  onClose (
57+     ref :  String ,
58+     callback :  () ->  Unit 
59+   ) {
5460    this .close =  this .close +  Pair (ref, callback)
5561  }
5662
5763  /* * Safely adds an onError callback */ 
58-   fun  onError (ref :  String , callback :  (Throwable , Response ? ) ->  Unit ) {
64+   fun  onError (
65+     ref :  String ,
66+     callback :  (Throwable , Response ? ) ->  Unit 
67+   ) {
5968    this .error =  this .error +  Pair (ref, callback)
6069  }
6170
6271  /* * Safely adds an onMessage callback */ 
63-   fun  onMessage (ref :  String , callback :  (Message ) ->  Unit ) {
72+   fun  onMessage (
73+     ref :  String ,
74+     callback :  (Message ) ->  Unit 
75+   ) {
6476    this .message =  this .message +  Pair (ref, callback)
6577  }
6678
@@ -96,7 +108,6 @@ typealias PayloadClosure = () -> Payload?
96108 * Connects to a Phoenix Server 
97109 */  
98110
99- 
100111/* *
101112 * A [Socket] which connects to a Phoenix Server. Takes a closure to allow for changing parameters 
102113 * to be sent to the server when connecting. 
@@ -112,7 +123,7 @@ typealias PayloadClosure = () -> Payload?
112123 */  
113124class  Socket (
114125  url :  String ,
115-   paramsClosure :  PayloadClosure ? ,
126+   val   paramsClosure :  PayloadClosure ,
116127  private  val  gson :  Gson  = Defaults .gson,
117128  private  val  client :  OkHttpClient  = OkHttpClient .Builder ().build()
118129) {
@@ -132,12 +143,6 @@ class Socket(
132143  var  endpointUrl:  URL 
133144    private  set
134145
135-   /* *
136-    * A closure that returns the optional params to pass when connecting. Must 
137-    * be set when initializing the Socket. These will be appended to the URL. 
138-    */  
139-   val  paramsClosure:  PayloadClosure ?  =  paramsClosure
140- 
141146  /* * Timeout to use when opening a connection */ 
142147  var  timeout:  Long  =  Defaults .TIMEOUT 
143148
@@ -229,7 +234,7 @@ class Socket(
229234    params:  Payload ?  =  null ,
230235    gson:  Gson  =  Defaults .gson,
231236    client:  OkHttpClient  =  OkHttpClient .Builder ().build()
232-   ):  this (url, params?. let  { { it }   }, gson, client)
237+   )  :  this (url, { params  }, gson, client)
233238
234239  init  {
235240    var  mutableUrl =  url
@@ -245,7 +250,6 @@ class Socket(
245250      mutableUrl + =  " websocket" 
246251    }
247252
248- 
249253    //  Store the endpoint before changing the protocol
250254    this .endpoint =  mutableUrl
251255
@@ -255,12 +259,12 @@ class Socket(
255259
256260    //  Create reconnect timer
257261    this .reconnectTimer =  TimeoutTimer (
258-          dispatchQueue =  dispatchQueue,
259-          timerCalculation =  reconnectAfterMs,
260-          callback =  {
261-            this .logItems(" Socket attempting to reconnect" 
262-            this .teardown { this .connect() }
263-          })
262+       dispatchQueue =  dispatchQueue,
263+       timerCalculation =  reconnectAfterMs,
264+       callback =  {
265+         this .logItems(" Socket attempting to reconnect" 
266+         this .teardown { this .connect() }
267+       })
264268  }
265269
266270  // ------------------------------------------------------------------------------
@@ -289,8 +293,7 @@ class Socket(
289293    this .closeWasClean =  false 
290294
291295    //  Build the new endpointUrl with the params closure. The payload returned
292-     //  from the closure could have changed after the socket attempts to reconnect,
293-     //  i.e. and authToken.
296+     //  from the closure could be different such as a changing authToken.
294297    this .endpointUrl =  Defaults .buildEndpointUrl(this .endpoint, this .paramsClosure)
295298
296299    //  Now create the connection transport and attempt to connect
@@ -313,7 +316,6 @@ class Socket(
313316    //  Reset any reconnects and teardown the socket connection
314317    this .reconnectTimer.reset()
315318    this .teardown(code, reason, callback)
316- 
317319  }
318320
319321  fun  onOpen (callback :  (() ->  Unit )): String  {
@@ -336,7 +338,10 @@ class Socket(
336338    this .stateChangeCallbacks.release()
337339  }
338340
339-   fun  channel (topic :  String , params :  Payload  = mapOf()): Channel  {
341+   fun  channel (
342+     topic :  String ,
343+     params :  Payload  = mapOf()
344+   ): Channel  {
340345    val  channel =  Channel (topic, params, this )
341346    this .channels =  this .channels +  channel
342347
@@ -350,7 +355,7 @@ class Socket(
350355    //  removed instead of calling .remove() on the list, thus returning a new list
351356    //  that does not contain the channel that was removed.
352357    this .channels =  channels
353-          .filter { it.joinRef !=  channel.joinRef }
358+       .filter { it.joinRef !=  channel.joinRef }
354359  }
355360
356361  /* *
@@ -481,7 +486,7 @@ class Socket(
481486    val  period =  heartbeatIntervalMs
482487
483488    heartbeatTask = 
484-          dispatchQueue.queueAtFixedRate(delay, period, TimeUnit .MILLISECONDS ) { sendHeartbeat() }
489+       dispatchQueue.queueAtFixedRate(delay, period, TimeUnit .MILLISECONDS ) { sendHeartbeat() }
485490  }
486491
487492  internal  fun  sendHeartbeat () {
@@ -503,10 +508,11 @@ class Socket(
503508    //  The last heartbeat was acknowledged by the server. Send another one
504509    this .pendingHeartbeatRef =  this .makeRef()
505510    this .push(
506-         topic =  " phoenix" 
507-         event =  Channel .Event .HEARTBEAT .value,
508-         payload =  mapOf (),
509-         ref =  pendingHeartbeatRef)
511+       topic =  " phoenix" 
512+       event =  Channel .Event .HEARTBEAT .value,
513+       payload =  mapOf (),
514+       ref =  pendingHeartbeatRef
515+     )
510516  }
511517
512518  private  fun  abnormalClose (reason :  String ) {
@@ -570,14 +576,17 @@ class Socket(
570576
571577    //  Dispatch the message to all channels that belong to the topic
572578    this .channels
573-          .filter { it.isMember(message) }
574-          .forEach { it.trigger(message) }
579+       .filter { it.isMember(message) }
580+       .forEach { it.trigger(message) }
575581
576582    //  Inform all onMessage callbacks of the message
577583    this .stateChangeCallbacks.message.forEach { it.second.invoke(message) }
578584  }
579585
580-   internal  fun  onConnectionError (t :  Throwable , response :  Response ? ) {
586+   internal  fun  onConnectionError (
587+     t :  Throwable ,
588+     response :  Response ? 
589+   ) {
581590    this .logItems(" Transport: error $t " 
582591
583592    //  Send an error to all channels
@@ -586,5 +595,4 @@ class Socket(
586595    //  Inform any state callbacks of the error
587596    this .stateChangeCallbacks.error.forEach { it.second.invoke(t, response) }
588597  }
589- 
590598}
0 commit comments