Skip to content

Commit bc25e20

Browse files
committed
Cleaned up adding and removing from array syntax
1 parent 0f9d111 commit bc25e20

File tree

4 files changed

+20
-38
lines changed

4 files changed

+20
-38
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ class Push(
9595

9696
// If a previous hook for this status already exists. Just append the new hook. If not, then
9797
// create a new array of hooks if no previous hook is associated with status
98-
receiveHooks[status] = receiveHooks[status]?.copyAndAdd(callback) ?: arrayListOf(callback)
98+
receiveHooks[status] = receiveHooks[status]?.plus(callback) ?: arrayListOf(callback)
9999

100100
return this
101101
}

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

+5-14
Original file line numberDiff line numberDiff line change
@@ -46,22 +46,22 @@ internal class StateChangeCallbacks {
4646

4747
/** Safely adds an onOpen callback */
4848
fun onOpen(callback: () -> Unit) {
49-
this.open = this.open.copyAndAdd(callback)
49+
this.open = this.open + callback
5050
}
5151

5252
/** Safely adds an onClose callback */
5353
fun onClose(callback: () -> Unit) {
54-
this.close = this.close.copyAndAdd(callback)
54+
this.close = this.close + callback
5555
}
5656

5757
/** Safely adds an onError callback */
5858
fun onError(callback: (Throwable, Response?) -> Unit) {
59-
this.error = this.error.copyAndAdd(callback)
59+
this.error = this.error + callback
6060
}
6161

6262
/** Safely adds an onMessage callback */
6363
fun onMessage(callback: (Message) -> Unit) {
64-
this.message = this.message.copyAndAdd(callback)
64+
this.message = this.message + callback
6565
}
6666

6767
/** Clears all stored callbacks */
@@ -73,15 +73,6 @@ internal class StateChangeCallbacks {
7373
}
7474
}
7575

76-
/** Converts the List to a MutableList, adds the value, and then returns as a read-only List */
77-
fun <T> List<T>.copyAndAdd(value: T): List<T> {
78-
val temp = this.toMutableList()
79-
temp.add(value)
80-
81-
return temp
82-
}
83-
84-
8576
/** RFC 6455: indicates a normal closure */
8677
const val WS_CLOSE_NORMAL = 1000
8778

@@ -304,7 +295,7 @@ class Socket(
304295

305296
fun channel(topic: String, params: Payload = mapOf()): Channel {
306297
val channel = Channel(topic, params, this)
307-
this.channels = this.channels.copyAndAdd(channel)
298+
this.channels = this.channels + channel
308299

309300
return channel
310301
}

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

+14-15
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import org.junit.jupiter.api.Nested
1919
import org.junit.jupiter.api.Test
2020
import org.mockito.Mock
2121
import org.mockito.MockitoAnnotations
22-
import org.phoenixframework.utilities.copyAndRemove
2322
import java.net.URL
2423
import java.util.concurrent.TimeUnit
2524

@@ -756,8 +755,8 @@ class SocketTest {
756755
val spy = spy(channel)
757756

758757
// Use the spy instance instead of the Channel instance
759-
socket.channels = socket.channels.copyAndRemove(channel)
760-
socket.channels = socket.channels.copyAndAdd(spy)
758+
socket.channels = socket.channels.minus(channel)
759+
socket.channels = socket.channels.plus(spy)
761760

762761
spy.join()
763762
assertThat(spy.state).isEqualTo(Channel.State.JOINING)
@@ -772,8 +771,8 @@ class SocketTest {
772771
val spy = spy(channel)
773772

774773
// Use the spy instance instead of the Channel instance
775-
socket.channels = socket.channels.copyAndRemove(channel)
776-
socket.channels = socket.channels.copyAndAdd(spy)
774+
socket.channels = socket.channels.minus(channel)
775+
socket.channels = socket.channels.plus(spy)
777776

778777
spy.join().trigger("ok", emptyMap())
779778

@@ -789,8 +788,8 @@ class SocketTest {
789788
val spy = spy(channel)
790789

791790
// Use the spy instance instead of the Channel instance
792-
socket.channels = socket.channels.copyAndRemove(channel)
793-
socket.channels = socket.channels.copyAndAdd(spy)
791+
socket.channels = socket.channels.minus(channel)
792+
socket.channels = socket.channels.plus(spy)
794793

795794
spy.join().trigger("ok", emptyMap())
796795
spy.leave()
@@ -828,8 +827,8 @@ class SocketTest {
828827
val spy = spy(channel)
829828

830829
// Use the spy instance instead of the Channel instance
831-
socket.channels = socket.channels.copyAndRemove(channel)
832-
socket.channels = socket.channels.copyAndAdd(spy)
830+
socket.channels = socket.channels.minus(channel)
831+
socket.channels = socket.channels.plus(spy)
833832

834833
spy.join()
835834
assertThat(spy.state).isEqualTo(Channel.State.JOINING)
@@ -844,8 +843,8 @@ class SocketTest {
844843
val spy = spy(channel)
845844

846845
// Use the spy instance instead of the Channel instance
847-
socket.channels = socket.channels.copyAndRemove(channel)
848-
socket.channels = socket.channels.copyAndAdd(spy)
846+
socket.channels = socket.channels.minus(channel)
847+
socket.channels = socket.channels.plus(spy)
849848

850849
spy.join().trigger("ok", emptyMap())
851850

@@ -861,8 +860,8 @@ class SocketTest {
861860
val spy = spy(channel)
862861

863862
// Use the spy instance instead of the Channel instance
864-
socket.channels = socket.channels.copyAndRemove(channel)
865-
socket.channels = socket.channels.copyAndAdd(spy)
863+
socket.channels = socket.channels.minus(channel)
864+
socket.channels = socket.channels.plus(spy)
866865

867866
spy.join().trigger("ok", emptyMap())
868867
spy.leave()
@@ -886,8 +885,8 @@ class SocketTest {
886885
val otherChannel = mock<Channel>()
887886
whenever(otherChannel.isMember(any())).thenReturn(false)
888887

889-
socket.channels = socket.channels.copyAndAdd(targetChannel)
890-
socket.channels = socket.channels.copyAndRemove(otherChannel)
888+
socket.channels = socket.channels.plus(targetChannel)
889+
socket.channels = socket.channels.minus(otherChannel)
891890

892891
val rawMessage =
893892
"{\"topic\":\"topic\",\"event\":\"event\",\"payload\":{\"one\":\"two\"},\"status\":\"ok\"}"

src/test/kotlin/org/phoenixframework/utilities/TestUtilities.kt

-8
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,4 @@ import org.phoenixframework.Channel
55

66
fun Channel.getBindings(event: String): List<Binding> {
77
return bindings.toList().filter { it.event == event }
8-
}
9-
10-
/** Converts the List to a MutableList, removes the value, and then returns as a read-only List */
11-
fun <T> List<T>.copyAndRemove(value: T): List<T> {
12-
val temp = this.toMutableList()
13-
temp.remove(value)
14-
15-
return temp
168
}

0 commit comments

Comments
 (0)