Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,20 @@ import kotlin.reflect.KClass
// threads to read the list but when a modification is made the modifier is given a new copy of
// list and that becomes the new version of the list.
// More info: https://developer.android.com/reference/kotlin/java/util/concurrent/CopyOnWriteArrayList
internal class Mediator(internal val plugins: CopyOnWriteArrayList<Plugin>) {
internal class Mediator(internal var plugins: CopyOnWriteArrayList<Plugin> = CopyOnWriteArrayList()) {

fun add(plugin: Plugin) {
plugins.add(plugin)
}

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

fun execute(event: BaseEvent): BaseEvent? {
fun execute(event: BaseEvent): BaseEvent? {
var result: BaseEvent? = event

plugins.forEach { plugin ->
Expand Down Expand Up @@ -61,7 +64,7 @@ internal class Mediator(internal val plugins: CopyOnWriteArrayList<Plugin>) {
return null
}

fun <T : Plugin> findAll(pluginClass: KClass<T>): List<T> {
fun <T : Plugin> findAll(pluginClass: KClass<T>): List<T> {
return plugins.filter { pluginClass.isInstance(it) } as List<T>
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ import kotlin.reflect.KClass
// Before -> Enrichment -> Destination -> After
internal class Timeline {
internal val plugins: Map<Plugin.Type, Mediator> = mapOf(
Plugin.Type.Before to Mediator(CopyOnWriteArrayList()),
Plugin.Type.Enrichment to Mediator(CopyOnWriteArrayList()),
Plugin.Type.Destination to Mediator(CopyOnWriteArrayList()),
Plugin.Type.After to Mediator(CopyOnWriteArrayList()),
Plugin.Type.Utility to Mediator(CopyOnWriteArrayList())
Plugin.Type.Before to Mediator(),
Plugin.Type.Enrichment to Mediator(),
Plugin.Type.Destination to Mediator(),
Plugin.Type.After to Mediator(),
Plugin.Type.Utility to Mediator()
)
lateinit var analytics: Analytics

Expand Down
Loading