Skip to content

Commit 68cebe6

Browse files
authored
Add Mediator test and clean it up a bit. (#143)
1 parent dcc140b commit 68cebe6

File tree

3 files changed

+370
-9
lines changed

3 files changed

+370
-9
lines changed

core/src/main/java/com/segment/analytics/kotlin/core/platform/Mediator.kt

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,20 @@ import kotlin.reflect.KClass
1212
// threads to read the list but when a modification is made the modifier is given a new copy of
1313
// list and that becomes the new version of the list.
1414
// More info: https://developer.android.com/reference/kotlin/java/util/concurrent/CopyOnWriteArrayList
15-
internal class Mediator(internal val plugins: CopyOnWriteArrayList<Plugin>) {
15+
internal class Mediator(internal var plugins: CopyOnWriteArrayList<Plugin> = CopyOnWriteArrayList()) {
1616

1717
fun add(plugin: Plugin) {
1818
plugins.add(plugin)
1919
}
2020

2121
fun remove(plugin: Plugin) {
22-
plugins.removeAll { it === plugin } // remove only if reference is the same
22+
// Note: We want to use this form of removeAll() function that takes a collection and
23+
// and NOT the removeAll {} that takes a code block as that will get wrapped by MutableList
24+
// and use a stateful iterator that will break when run from multiple threads.
25+
plugins.removeAll(setOf(plugin))
2326
}
2427

25-
fun execute(event: BaseEvent): BaseEvent? {
28+
fun execute(event: BaseEvent): BaseEvent? {
2629
var result: BaseEvent? = event
2730

2831
plugins.forEach { plugin ->
@@ -61,7 +64,7 @@ internal class Mediator(internal val plugins: CopyOnWriteArrayList<Plugin>) {
6164
return null
6265
}
6366

64-
fun <T : Plugin> findAll(pluginClass: KClass<T>): List<T> {
67+
fun <T : Plugin> findAll(pluginClass: KClass<T>): List<T> {
6568
return plugins.filter { pluginClass.isInstance(it) } as List<T>
6669
}
6770
}

core/src/main/java/com/segment/analytics/kotlin/core/platform/Timeline.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ import kotlin.reflect.KClass
1212
// Before -> Enrichment -> Destination -> After
1313
internal class Timeline {
1414
internal val plugins: Map<Plugin.Type, Mediator> = mapOf(
15-
Plugin.Type.Before to Mediator(CopyOnWriteArrayList()),
16-
Plugin.Type.Enrichment to Mediator(CopyOnWriteArrayList()),
17-
Plugin.Type.Destination to Mediator(CopyOnWriteArrayList()),
18-
Plugin.Type.After to Mediator(CopyOnWriteArrayList()),
19-
Plugin.Type.Utility to Mediator(CopyOnWriteArrayList())
15+
Plugin.Type.Before to Mediator(),
16+
Plugin.Type.Enrichment to Mediator(),
17+
Plugin.Type.Destination to Mediator(),
18+
Plugin.Type.After to Mediator(),
19+
Plugin.Type.Utility to Mediator()
2020
)
2121
lateinit var analytics: Analytics
2222

0 commit comments

Comments
 (0)