Skip to content

Commit 0779c7a

Browse files
eyal-lezmydsrees
authored andcommitted
Kotlinify and simplify a little bit the code (#23)
* Remove useless public modifier * Set PhxMessage to a data class * Improve PhxMessage documentation * Simplify code * Add empty line at end of file to be POSIX compliant
1 parent e7db7fa commit 0779c7a

File tree

8 files changed

+43
-52
lines changed

8 files changed

+43
-52
lines changed

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ class PhxChannel(
136136
* @param timeout: Overrides the default timeout
137137
* @return Push which receive hooks can be applied to
138138
*/
139-
public fun join(joinParams: Payload? = null, timeout: Long? = null): PhxPush {
139+
fun join(joinParams: Payload? = null, timeout: Long? = null): PhxPush {
140140
if (joinedOnce) {
141141
throw IllegalStateException("Tried to join channel multiple times. `join()` can only be called once per channel")
142142
}
@@ -156,7 +156,7 @@ class PhxChannel(
156156
* @param callback: Callback to be informed when the channel closes
157157
* @return the ref counter of the subscription
158158
*/
159-
public fun onClose(callback: (msg: PhxMessage) -> Unit): Int {
159+
fun onClose(callback: (msg: PhxMessage) -> Unit): Int {
160160
return this.on(PhxEvent.CLOSE, callback)
161161
}
162162

@@ -166,14 +166,14 @@ class PhxChannel(
166166
* @param callback: Callback to be informed when the channel errors
167167
* @return the ref counter of the subscription
168168
*/
169-
public fun onError(callback: (msg: PhxMessage) -> Unit): Int {
169+
fun onError(callback: (msg: PhxMessage) -> Unit): Int {
170170
return this.on(PhxEvent.ERROR, callback)
171171
}
172172

173173
/**
174174
* Convenience method to take the Channel.Event enum. Same as channel.on(string)
175175
*/
176-
public fun on(event: PhxChannel.PhxEvent, callback: (PhxMessage) -> Unit): Int {
176+
fun on(event: PhxChannel.PhxEvent, callback: (PhxMessage) -> Unit): Int {
177177
return this.on(event.value, callback)
178178
}
179179

@@ -195,7 +195,7 @@ class PhxChannel(
195195
* @param callback: Receives payload of the event
196196
* @return: The subscriptions ref counter
197197
*/
198-
public fun on(event: String, callback: (PhxMessage) -> Unit): Int {
198+
fun on(event: String, callback: (PhxMessage) -> Unit): Int {
199199
val ref = bindingRef
200200
this.bindingRef = ref + 1
201201

@@ -220,7 +220,7 @@ class PhxChannel(
220220
* @param event: Event to unsubscribe from
221221
* @param ref: Optional. Ref counter returned when subscribed to event
222222
*/
223-
public fun off(event: String, ref: Int? = null) {
223+
fun off(event: String, ref: Int? = null) {
224224
// Remove any subscriptions that match the given event and ref ID. If no ref
225225
// ID is given, then remove all subscriptions for an event.
226226
if (ref != null) {
@@ -238,7 +238,7 @@ class PhxChannel(
238238
* @param timeout: Optional timeout. Default will be used
239239
* @return [PhxPush] that can be hooked into
240240
*/
241-
public fun push(event: String, payload: Payload, timeout: Long = DEFAULT_TIMEOUT): PhxPush {
241+
fun push(event: String, payload: Payload, timeout: Long = DEFAULT_TIMEOUT): PhxPush {
242242
if (!joinedOnce) {
243243
// If the Channel has not been joined, throw an exception
244244
throw RuntimeException("Tried to push $event to $topic before joining. Use channel.join() before pushing events")
@@ -269,7 +269,7 @@ class PhxChannel(
269269
*
270270
* @param timeout: Optional timeout. Default will be used
271271
*/
272-
public fun leave(timeout: Long = DEFAULT_TIMEOUT): PhxPush {
272+
fun leave(timeout: Long = DEFAULT_TIMEOUT): PhxPush {
273273
this.state = PhxState.LEAVING
274274

275275
val onClose: ((PhxMessage) -> Unit) = {
@@ -297,7 +297,7 @@ class PhxChannel(
297297
* @param callback: Callback which will receive the inbound message before
298298
* it is dispatched to other callbacks. Must return a Message object.
299299
*/
300-
public fun onMessage(callback: (message: PhxMessage) -> PhxMessage) {
300+
fun onMessage(callback: (message: PhxMessage) -> PhxMessage) {
301301
this.onMessage = callback
302302
}
303303

@@ -380,4 +380,4 @@ class PhxChannel(
380380
/** @return: True if the channel has requested to leave */
381381
val isLeaving: Boolean
382382
get() = state == PhxState.LEAVING
383-
}
383+
}

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

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,20 @@ package org.phoenixframework
22

33
import com.google.gson.annotations.SerializedName
44

5-
class PhxMessage(
5+
data class PhxMessage(
66
/** The unique string ref. Empty if not present */
77
@SerializedName("ref")
88
val ref: String = "",
99

10-
/** Property "topic" is never used */
10+
/** The message topic */
1111
@SerializedName("topic")
1212
val topic: String = "",
1313

14-
/** Property "topic" is never used */
14+
/** The message event name, for example "phx_join" or any other custom name */
1515
@SerializedName("event")
1616
val event: String = "",
1717

18-
/** Property "topic" is never used */
18+
/** The payload of the message */
1919
@SerializedName("payload")
2020
val payload: Payload = HashMap(),
2121

@@ -30,10 +30,4 @@ class PhxMessage(
3030
*/
3131
val status: String?
3232
get() = payload["status"] as? String
33-
34-
35-
override fun toString(): String {
36-
return "Message(ref='$ref', joinRef=$joinRef, topic='$topic', event='$event', payload=$payload)"
37-
}
38-
39-
}
33+
}

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,7 @@ class PhxPush(
138138
* in milliseconds is reached.
139139
*/
140140
fun startTimeout() {
141-
this.timeoutTimer?.let {
142-
it.cancel()
143-
}
141+
this.timeoutTimer?.cancel()
144142

145143
val ref = this.channel.socket.makeRef()
146144
this.ref = ref
@@ -191,4 +189,4 @@ class PhxPush(
191189
this.channel.trigger(message)
192190
}
193191
}
194-
}
192+
}

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

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -33,28 +33,28 @@ open class PhxSocket(
3333
// Public Attributes
3434
//------------------------------------------------------------------------------
3535
/** Timeout to use when opening connections */
36-
public var timeout: Long = DEFAULT_TIMEOUT
36+
var timeout: Long = DEFAULT_TIMEOUT
3737

3838
/** Interval between sending a heartbeat */
39-
public var heartbeatIntervalMs: Long = DEFAULT_HEARTBEAT
39+
var heartbeatIntervalMs: Long = DEFAULT_HEARTBEAT
4040

4141
/** Interval between socket reconnect attempts */
42-
public var reconnectAfterMs: ((tries: Int) -> Long) = closure@{
42+
var reconnectAfterMs: ((tries: Int) -> Long) = closure@{
4343
return@closure if (it >= 3) 100000 else longArrayOf(1000, 2000, 5000)[it]
4444
}
4545

4646
/** Hook for custom logging into the client */
47-
public var logger: ((msg: String) -> Unit)? = null
47+
var logger: ((msg: String) -> Unit)? = null
4848

4949
/** Disable sending Heartbeats by setting to true */
50-
public var skipHeartbeat: Boolean = false
50+
var skipHeartbeat: Boolean = false
5151

5252
/**
5353
* Socket will attempt to reconnect if the Socket was closed. Will not
5454
* reconnect if the Socket errored (e.g. connection refused.) Default
5555
* is set to true
5656
*/
57-
public var autoReconnect: Boolean = true
57+
var autoReconnect: Boolean = true
5858

5959

6060
//------------------------------------------------------------------------------
@@ -140,13 +140,13 @@ open class PhxSocket(
140140
// Public
141141
//------------------------------------------------------------------------------
142142
/** True if the Socket is currently connected */
143-
public val isConnected: Boolean
143+
val isConnected: Boolean
144144
get() = connection != null
145145

146146
/**
147147
* Disconnects the Socket
148148
*/
149-
public fun disconnect() {
149+
fun disconnect() {
150150
connection?.close(1000, null)
151151
connection = null
152152

@@ -157,7 +157,7 @@ open class PhxSocket(
157157
* will be sent through the connection. If the Socket is already connected,
158158
* then this call will be ignored.
159159
*/
160-
public fun connect() {
160+
fun connect() {
161161
// Do not attempt to reconnect if already connected
162162
if (isConnected) return
163163
connection = client.newWebSocket(request, this)
@@ -173,7 +173,7 @@ open class PhxSocket(
173173
*
174174
* @param callback: Callback to register
175175
*/
176-
public fun onOpen(callback: () -> Unit) {
176+
fun onOpen(callback: () -> Unit) {
177177
this.onOpenCallbacks.add(callback)
178178
}
179179

@@ -188,7 +188,7 @@ open class PhxSocket(
188188
*
189189
* @param callback: Callback to register
190190
*/
191-
public fun onClose(callback: () -> Unit) {
191+
fun onClose(callback: () -> Unit) {
192192
this.onCloseCallbacks.add(callback)
193193
}
194194

@@ -202,7 +202,7 @@ open class PhxSocket(
202202
*
203203
* @param callback: Callback to register
204204
*/
205-
public fun onError(callback: (Throwable?, Response?) -> Unit) {
205+
fun onError(callback: (Throwable?, Response?) -> Unit) {
206206
this.onErrorCallbacks.add(callback)
207207
}
208208

@@ -216,7 +216,7 @@ open class PhxSocket(
216216
*
217217
* @param callback: Callback to register
218218
*/
219-
public fun onMessage(callback: (PhxMessage) -> Unit) {
219+
fun onMessage(callback: (PhxMessage) -> Unit) {
220220
this.onMessageCallbacks.add(callback)
221221
}
222222

@@ -226,7 +226,7 @@ open class PhxSocket(
226226
* call this method when you are finished when the Socket in order to release
227227
* any references held by the socket.
228228
*/
229-
public fun removeAllCallbacks() {
229+
fun removeAllCallbacks() {
230230
this.onOpenCallbacks.clear()
231231
this.onCloseCallbacks.clear()
232232
this.onErrorCallbacks.clear()
@@ -237,7 +237,7 @@ open class PhxSocket(
237237
* Removes the Channel from the socket. This does not cause the channel to inform
238238
* the server that it is leaving so you should call channel.leave() first.
239239
*/
240-
public fun remove(channel: PhxChannel) {
240+
fun remove(channel: PhxChannel) {
241241
this.channels = channels
242242
.filter { it.joinRef != channel.joinRef }
243243
.toMutableList()
@@ -249,7 +249,7 @@ open class PhxSocket(
249249
* Example:
250250
* val channel = socket.channel("rooms", params)
251251
*/
252-
public fun channel(topic: String, params: Payload? = null): PhxChannel {
252+
fun channel(topic: String, params: Payload? = null): PhxChannel {
253253
val channel = PhxChannel(topic, params ?: HashMap(), this)
254254
this.channels.add(channel)
255255
return channel
@@ -258,14 +258,14 @@ open class PhxSocket(
258258
/**
259259
* Sends data through the Socket
260260
*/
261-
public open fun push(topic: String,
262-
event: String,
263-
payload: Payload,
264-
ref: String? = null,
265-
joinRef: String? = null) {
261+
open fun push(topic: String,
262+
event: String,
263+
payload: Payload,
264+
ref: String? = null,
265+
joinRef: String? = null) {
266266

267267
val callback: (() -> Unit) = {
268-
var body: MutableMap<String, Any> = HashMap()
268+
val body: MutableMap<String, Any> = HashMap()
269269
body["topic"] = topic
270270
body["event"] = event
271271
body["payload"] = payload
@@ -294,7 +294,7 @@ open class PhxSocket(
294294
/**
295295
* @return the next message ref, accounting for overflows
296296
*/
297-
public open fun makeRef(): String {
297+
open fun makeRef(): String {
298298
val newRef = this.ref + 1
299299
this.ref = if (newRef == Int.MAX_VALUE) 0 else newRef
300300

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,4 @@ class PhxTimer(
4343
this.timer?.cancel()
4444
this.timer = null
4545
}
46-
}
46+
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,4 +150,3 @@ class PhxChannelTest {
150150
CompletableFuture.allOf(f1, f3).get(10, TimeUnit.SECONDS)
151151
}
152152
}
153-

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,4 @@ class PhxMessageTest {
1919
assertThat(message.status).isEqualTo("ok")
2020

2121
}
22-
}
22+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,4 @@ class PhxSocketTest {
3636
assertThat(PhxSocket("wss://localhost:4000/socket/websocket", spacesParams).endpoint.toString())
3737
.isEqualTo("https://localhost:4000/socket/websocket?user_id=1&token=abc%20123")
3838
}
39-
}
39+
}

0 commit comments

Comments
 (0)