Skip to content

Commit 95663f6

Browse files
authored
Merge pull request #99 from ccolorcat/feature/create-channel-with-payload-closure
Allows channel creation using PayloadClosure
2 parents 15369f9 + 52d5156 commit 95663f6

File tree

7 files changed

+76
-28
lines changed

7 files changed

+76
-28
lines changed

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

+10-4
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ data class Binding(
3838
*/
3939
class Channel(
4040
val topic: String,
41-
params: Payload,
41+
paramsClosure: PayloadClosure,
4242
internal val socket: Socket
4343
) {
4444

@@ -94,10 +94,10 @@ class Channel(
9494
internal var timeout: Long
9595

9696
/** Params passed in through constructions and provided to the JoinPush */
97-
var params: Payload = params
97+
var params: Payload
98+
get() = joinPush.payload
9899
set(value) {
99100
joinPush.payload = value
100-
field = value
101101
}
102102

103103
/** Set to true once the channel has attempted to join */
@@ -121,6 +121,12 @@ class Channel(
121121
*/
122122
internal var onMessage: (Message) -> Message = { it }
123123

124+
constructor(
125+
topic: String,
126+
params: Payload,
127+
socket: Socket
128+
) : this(topic, { params }, socket)
129+
124130
init {
125131
this.state = State.CLOSED
126132
this.bindings = ConcurrentLinkedQueue()
@@ -148,7 +154,7 @@ class Channel(
148154
this.joinPush = Push(
149155
channel = this,
150156
event = Event.JOIN.value,
151-
payload = params,
157+
payloadClosure = paramsClosure,
152158
timeout = timeout)
153159

154160
// Perform once the Channel has joined

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

+2-8
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,9 @@ import com.google.gson.FieldNamingPolicy
2626
import com.google.gson.Gson
2727
import com.google.gson.GsonBuilder
2828
import com.google.gson.JsonObject
29-
import com.google.gson.JsonParser
3029
import com.google.gson.reflect.TypeToken
31-
import okhttp3.HttpUrl
3230
import okhttp3.HttpUrl.Companion.toHttpUrlOrNull
33-
import org.phoenixframework.Defaults.gson
3431
import java.net.URL
35-
import javax.swing.text.html.HTML.Tag.P
3632

3733
object Defaults {
3834

@@ -156,10 +152,8 @@ object Defaults {
156152
httpBuilder.addQueryParameter("vsn", vsn)
157153

158154
// Append any additional query params
159-
paramsClosure.invoke()?.let {
160-
it.forEach { (key, value) ->
161-
httpBuilder.addQueryParameter(key, value.toString())
162-
}
155+
paramsClosure.invoke().forEach { (key, value) ->
156+
httpBuilder.addQueryParameter(key, value.toString())
163157
}
164158

165159
// Return the [URL] that will be used to establish a connection

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

+19-2
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ class Push(
3232
val channel: Channel,
3333
/** The event the Push is targeting */
3434
val event: String,
35-
/** The message to be sent */
36-
var payload: Payload = mapOf(),
35+
/** Closure that allows changing parameters sent during push */
36+
var payloadClosure: PayloadClosure,
3737
/** Duration before the message is considered timed out and failed to send */
3838
var timeout: Long = Defaults.TIMEOUT
3939
) {
@@ -56,6 +56,23 @@ class Push(
5656
/** The event that is associated with the reference ID of the Push */
5757
var refEvent: String? = null
5858

59+
var payload: Payload
60+
get() = payloadClosure.invoke()
61+
set(value) {
62+
payloadClosure = { value }
63+
}
64+
65+
constructor(
66+
/** The channel the Push is being sent through */
67+
channel: Channel,
68+
/** The event the Push is targeting */
69+
event: String,
70+
/** The message to be sent */
71+
payload: Payload = mapOf(),
72+
/** Duration before the message is considered timed out and failed to send */
73+
timeout: Long = Defaults.TIMEOUT
74+
) : this(channel, event, { payload }, timeout)
75+
5976
//------------------------------------------------------------------------------
6077
// Public
6178
//------------------------------------------------------------------------------

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

+9-4
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ const val WS_CLOSE_ABNORMAL = 1006
100100
/**
101101
* A closure that will return an optional Payload
102102
*/
103-
typealias PayloadClosure = () -> Payload?
103+
typealias PayloadClosure = () -> Payload
104104

105105
/** A closure that will encode a Map<String, Any> into a JSON String */
106106
typealias EncodeClosure = (Any) -> String
@@ -242,7 +242,7 @@ class Socket(
242242
*/
243243
constructor(
244244
url: String,
245-
params: Payload? = null,
245+
params: Payload = mapOf(),
246246
vsn: String = Defaults.VSN,
247247
encode: EncodeClosure = Defaults.encode,
248248
decode: DecodeClosure = Defaults.decode,
@@ -358,9 +358,14 @@ class Socket(
358358
fun channel(
359359
topic: String,
360360
params: Payload = mapOf()
361+
): Channel = this.channel(topic) { params }
362+
363+
fun channel(
364+
topic: String,
365+
paramsClosure: PayloadClosure
361366
): Channel {
362-
val channel = Channel(topic, params, this)
363-
this.channels = this.channels + channel
367+
val channel = Channel(topic, paramsClosure, this)
368+
this.channels += channel
364369

365370
return channel
366371
}

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

+26
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,32 @@ class ChannelTest {
149149
/* End JoinParams */
150150
}
151151

152+
153+
@Nested
154+
@DisplayName("join paramsClosure")
155+
inner class JoinParamsClosure {
156+
@Test
157+
internal fun `updating join params closure`() {
158+
val paramsClosure = { mapOf("value" to 1) }
159+
val change = mapOf("value" to 2)
160+
161+
channel = Channel("topic", paramsClosure, socket)
162+
val joinPush = channel.joinPush
163+
164+
assertThat(joinPush.channel).isEqualTo(channel)
165+
assertThat(joinPush.payload["value"]).isEqualTo(1)
166+
assertThat(joinPush.event).isEqualTo("phx_join")
167+
assertThat(joinPush.timeout).isEqualTo(10_000L)
168+
169+
channel.params = change
170+
assertThat(joinPush.channel).isEqualTo(channel)
171+
assertThat(joinPush.payload["value"]).isEqualTo(2)
172+
assertThat(channel.params["value"]).isEqualTo(2)
173+
assertThat(joinPush.event).isEqualTo("phx_join")
174+
assertThat(joinPush.timeout).isEqualTo(10_000L)
175+
}
176+
}
177+
152178
@Nested
153179
@DisplayName("join")
154180
inner class Join {

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

+9-9
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ class PresenceTest {
196196
@Test
197197
internal fun `onJoins new presences and onLeaves left presences`() {
198198
val newState = fixState
199-
var state = mutableMapOf(
199+
var state: MutableMap<String, MutableMap<String, List<Map<String, Any>>>> = mutableMapOf(
200200
"u4" to mutableMapOf("metas" to listOf(mapOf("id" to 4, "phx_ref" to "4"))))
201201

202202
val joined: PresenceDiff = mutableMapOf()
@@ -245,9 +245,9 @@ class PresenceTest {
245245

246246
@Test
247247
internal fun `onJoins only newly added metas`() {
248-
var state = mutableMapOf(
248+
var state: MutableMap<String, MutableMap<String, List<Map<String, Any>>>> = mutableMapOf(
249249
"u3" to mutableMapOf("metas" to listOf(mapOf("id" to 3, "phx_ref" to "3"))))
250-
val newState = mutableMapOf(
250+
val newState: MutableMap<String, MutableMap<String, List<Map<String, Any>>>> = mutableMapOf(
251251
"u3" to mutableMapOf("metas" to listOf(
252252
mapOf("id" to 3, "phx_ref" to "3"),
253253
mapOf("id" to 3, "phx_ref" to "3.new")
@@ -285,9 +285,9 @@ class PresenceTest {
285285

286286
@Test
287287
internal fun `onLeaves only newly removed metas`() {
288-
val newState = mutableMapOf(
288+
val newState: MutableMap<String, MutableMap<String, List<Map<String, Any>>>> = mutableMapOf(
289289
"u3" to mutableMapOf("metas" to listOf(mapOf("id" to 3, "phx_ref" to "3"))))
290-
var state = mutableMapOf(
290+
var state: MutableMap<String, MutableMap<String, List<Map<String, Any>>>> = mutableMapOf(
291291
"u3" to mutableMapOf("metas" to listOf(
292292
mapOf("id" to 3, "phx_ref" to "3"),
293293
mapOf("id" to 3, "phx_ref" to "3.left")
@@ -326,13 +326,13 @@ class PresenceTest {
326326

327327
@Test
328328
internal fun `syncs both joined and left metas`() {
329-
val newState = mutableMapOf(
329+
val newState: MutableMap<String, MutableMap<String, List<Map<String, Any>>>> = mutableMapOf(
330330
"u3" to mutableMapOf("metas" to listOf(
331331
mapOf("id" to 3, "phx_ref" to "3"),
332332
mapOf("id" to 3, "phx_ref" to "3.new")
333333
)))
334334

335-
var state = mutableMapOf(
335+
var state: MutableMap<String, MutableMap<String, List<Map<String, Any>>>> = mutableMapOf(
336336
"u3" to mutableMapOf("metas" to listOf(
337337
mapOf("id" to 3, "phx_ref" to "3"),
338338
mapOf("id" to 3, "phx_ref" to "3.left")
@@ -421,13 +421,13 @@ class PresenceTest {
421421

422422
@Test
423423
internal fun `removes meta while leaving key if other metas exist`() {
424-
var state = mutableMapOf(
424+
var state: MutableMap<String, MutableMap<String, List<Map<String, Any>>>> = mutableMapOf(
425425
"u1" to mutableMapOf("metas" to listOf(
426426
mapOf("id" to 1, "phx_ref" to "1"),
427427
mapOf("id" to 1, "phx_ref" to "1.2")
428428
)))
429429

430-
val leaves = mutableMapOf(
430+
val leaves: MutableMap<String, MutableMap<String, List<Map<String, Any>>>> = mutableMapOf(
431431
"u1" to mutableMapOf("metas" to listOf(
432432
mapOf("id" to 1, "phx_ref" to "1")
433433
)))

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

+1-1
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.invoke()).isNull()
51+
assertThat(socket.paramsClosure.invoke()).isEmpty()
5252
assertThat(socket.channels).isEmpty()
5353
assertThat(socket.sendBuffer).isEmpty()
5454
assertThat(socket.ref).isEqualTo(0)

0 commit comments

Comments
 (0)