Add custom coroutine context during new Context creation

This commit is contained in:
Alexander Nozik 2024-05-25 21:19:06 +03:00
parent 2cd1885420
commit 3de5691c84
2 changed files with 14 additions and 3 deletions

View File

@ -10,6 +10,7 @@ import space.kscience.dataforge.misc.ThreadSafe
import space.kscience.dataforge.names.Name import space.kscience.dataforge.names.Name
import space.kscience.dataforge.provider.Provider import space.kscience.dataforge.provider.Provider
import kotlin.coroutines.CoroutineContext import kotlin.coroutines.CoroutineContext
import kotlin.coroutines.EmptyCoroutineContext
/** /**
* The local environment for anything being done in DataForge framework. Contexts are organized into tree structure with [Global] at the top. * The local environment for anything being done in DataForge framework. Contexts are organized into tree structure with [Global] at the top.
@ -26,6 +27,7 @@ public open class Context internal constructor(
public val parent: Context?, public val parent: Context?,
plugins: Set<Plugin>, // set of unattached plugins plugins: Set<Plugin>, // set of unattached plugins
meta: Meta, meta: Meta,
coroutineContext: CoroutineContext = EmptyCoroutineContext,
) : Named, MetaRepr, Provider, CoroutineScope { ) : Named, MetaRepr, Provider, CoroutineScope {
/** /**
@ -65,7 +67,7 @@ public open class Context internal constructor(
override val coroutineContext: CoroutineContext by lazy { override val coroutineContext: CoroutineContext by lazy {
(parent ?: Global).coroutineContext.let { parenContext -> (parent ?: Global).coroutineContext.let { parenContext ->
parenContext + SupervisorJob(parenContext[Job]) parenContext + coroutineContext + SupervisorJob(parenContext[Job])
} }
} }

View File

@ -13,6 +13,8 @@ import space.kscience.dataforge.names.plus
import kotlin.collections.component1 import kotlin.collections.component1
import kotlin.collections.component2 import kotlin.collections.component2
import kotlin.collections.set import kotlin.collections.set
import kotlin.coroutines.CoroutineContext
import kotlin.coroutines.EmptyCoroutineContext
/** /**
* A convenience builder for context * A convenience builder for context
@ -59,6 +61,13 @@ public class ContextBuilder internal constructor(
plugin(DeFactoPluginFactory(plugin)) plugin(DeFactoPluginFactory(plugin))
} }
private var coroutineContext: CoroutineContext = EmptyCoroutineContext
public fun coroutineContext(coroutineContext: CoroutineContext) {
this.coroutineContext = coroutineContext
}
public fun build(): Context { public fun build(): Context {
val contextName = name ?: NameToken("@auto", hashCode().toUInt().toString(16)).asName() val contextName = name ?: NameToken("@auto", hashCode().toUInt().toString(16)).asName()
val plugins = HashMap<PluginTag, Plugin>() val plugins = HashMap<PluginTag, Plugin>()
@ -86,7 +95,7 @@ public class ContextBuilder internal constructor(
addPlugin(factory, meta) addPlugin(factory, meta)
} }
return Context(contextName, parent, plugins.values.toSet(), meta.seal()) return Context(contextName, parent, plugins.values.toSet(), meta.seal(), coroutineContext)
} }
} }