Skip to content

Commit

Permalink
Create a safe iterator at notify new state (#2)
Browse files Browse the repository at this point in the history
* Create a safe iterator at notify new state

* Created test
  • Loading branch information
Daniel Sánchez Ceinos authored Feb 19, 2020
1 parent 3eb4b23 commit 03116c3
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
7 changes: 2 additions & 5 deletions masmini-common/src/main/java/masmini/Store.kt
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@ abstract class Store<S> : Closeable {

@Suppress("UNCHECKED_CAST")
open fun initialState(): S {
val type = (javaClass.genericSuperclass as ParameterizedType).actualTypeArguments[0]
as Class<S>
val type = (javaClass.genericSuperclass as ParameterizedType).actualTypeArguments[0] as Class<S>
try {
val constructor = type.getDeclaredConstructor()
constructor.isAccessible = true
Expand All @@ -85,9 +84,7 @@ abstract class Store<S> : Closeable {
//State mutation should to happen on UI thread
if (newState != _state) {
_state = newState
listeners.forEach {
it(newState)
}
listeners.toList().forEach { it(newState) }
}
}

Expand Down
11 changes: 11 additions & 0 deletions masmini-common/src/test/kotlin/masmini/StoreTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package masmini

import org.amshove.kluent.`should be equal to`
import org.junit.Test
import java.io.Closeable

class StoreTest {

Expand Down Expand Up @@ -44,4 +45,14 @@ class StoreTest {
store.updateState("abc")
state `should be equal to` ""
}

@Test
fun `close listener doesn't throw concurrent exception`() {
val store = SampleStore()
store.subscribe(hotStart = false) {}

var subscription: Closeable? = null
subscription = store.subscribe(hotStart = false) { subscription?.close() }
store.updateState("abc")
}
}

0 comments on commit 03116c3

Please sign in to comment.