Skip to content

Commit d16ce13

Browse files
committed
Cleaning up and formatting code
1 parent b1f814f commit d16ce13

File tree

3 files changed

+56
-45
lines changed

3 files changed

+56
-45
lines changed

src/main/kotlin/org/phoenixframework/Defaults.kt

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,21 +38,22 @@ object Defaults {
3838

3939
/** Default reconnect algorithm for the socket */
4040
val reconnectSteppedBackOff: (Int) -> Long = { tries ->
41-
if (tries > 9) 5_000 else listOf(10L, 50L, 100L, 150L, 200L, 250L, 500L, 1_000L, 2_000L)[tries - 1]
41+
if (tries > 9) 5_000 else listOf(
42+
10L, 50L, 100L, 150L, 200L, 250L, 500L, 1_000L, 2_000L
43+
)[tries - 1]
4244
}
4345

4446
/** Default rejoin algorithm for individual channels */
4547
val rejoinSteppedBackOff: (Int) -> Long = { tries ->
4648
if (tries > 3) 10_000 else listOf(1_000L, 2_000L, 5_000L)[tries - 1]
4749
}
4850

49-
5051
/** The default Gson configuration to use when parsing messages */
5152
val gson: Gson
5253
get() = GsonBuilder()
53-
.setLenient()
54-
.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
55-
.create()
54+
.setLenient()
55+
.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
56+
.create()
5657

5758
/**
5859
* Takes an endpoint and a params closure given by the User and constructs a URL that
@@ -62,7 +63,10 @@ object Defaults {
6263
*
6364
* @throws IllegalArgumentException if [endpoint] is not a valid URL endpoint.
6465
*/
65-
internal fun buildEndpointUrl(endpoint: String, paramsClosure: PayloadClosure?): URL {
66+
internal fun buildEndpointUrl(
67+
endpoint: String,
68+
paramsClosure: PayloadClosure
69+
): URL {
6670
var mutableUrl = endpoint
6771
// Silently replace web socket URLs with HTTP URLs.
6872
if (endpoint.regionMatches(0, "ws:", 0, 3, ignoreCase = true)) {
@@ -72,8 +76,9 @@ object Defaults {
7276
}
7377

7478
// If there are query params, append them now
75-
var httpUrl = HttpUrl.parse(mutableUrl) ?: throw IllegalArgumentException("invalid url: $endpoint")
76-
paramsClosure?.invoke()?.let {
79+
var httpUrl =
80+
HttpUrl.parse(mutableUrl) ?: throw IllegalArgumentException("invalid url: $endpoint")
81+
paramsClosure.invoke()?.let {
7782
val httpBuilder = httpUrl.newBuilder()
7883
it.forEach { (key, value) ->
7984
httpBuilder.addQueryParameter(key, value.toString())
@@ -85,6 +90,4 @@ object Defaults {
8590
// Store the URL that will be used to establish a connection
8691
return httpUrl.url()
8792
}
88-
89-
9093
}

src/main/kotlin/org/phoenixframework/Socket.kt

Lines changed: 42 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -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
*/
113124
class 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
}

src/test/kotlin/org/phoenixframework/SocketTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class SocketTest {
4848
internal fun `sets defaults`() {
4949
val socket = Socket("wss://localhost:4000/socket")
5050

51-
assertThat(socket.paramsClosure).isNull()
51+
assertThat(socket.paramsClosure.invoke()).isNull()
5252
assertThat(socket.channels).isEmpty()
5353
assertThat(socket.sendBuffer).isEmpty()
5454
assertThat(socket.ref).isEqualTo(0)

0 commit comments

Comments
 (0)