File tree Expand file tree Collapse file tree 3 files changed +370
-9
lines changed
main/java/com/segment/analytics/kotlin/core/platform
test/kotlin/com/segment/analytics/kotlin/core/platform Expand file tree Collapse file tree 3 files changed +370
-9
lines changed Original file line number Diff line number Diff line change @@ -12,17 +12,20 @@ import kotlin.reflect.KClass
12
12
// threads to read the list but when a modification is made the modifier is given a new copy of
13
13
// list and that becomes the new version of the list.
14
14
// 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 () ) {
16
16
17
17
fun add (plugin : Plugin ) {
18
18
plugins.add(plugin)
19
19
}
20
20
21
21
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))
23
26
}
24
27
25
- fun execute (event : BaseEvent ): BaseEvent ? {
28
+ fun execute (event : BaseEvent ): BaseEvent ? {
26
29
var result: BaseEvent ? = event
27
30
28
31
plugins.forEach { plugin ->
@@ -61,7 +64,7 @@ internal class Mediator(internal val plugins: CopyOnWriteArrayList<Plugin>) {
61
64
return null
62
65
}
63
66
64
- fun <T : Plugin > findAll (pluginClass : KClass <T >): List <T > {
67
+ fun <T : Plugin > findAll (pluginClass : KClass <T >): List <T > {
65
68
return plugins.filter { pluginClass.isInstance(it) } as List <T >
66
69
}
67
70
}
Original file line number Diff line number Diff line change @@ -12,11 +12,11 @@ import kotlin.reflect.KClass
12
12
// Before -> Enrichment -> Destination -> After
13
13
internal class Timeline {
14
14
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 ()
20
20
)
21
21
lateinit var analytics: Analytics
22
22
You can’t perform that action at this time.
0 commit comments