Skip to content

Commit

Permalink
Merge pull request #440 from JetBrains/ext-protocol-usov
Browse files Browse the repository at this point in the history
Delegate creating extensions to parent protocol
  • Loading branch information
Iliya-usov authored Oct 3, 2023
2 parents 921c5a5 + a86da16 commit 941e40e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ class Protocol internal constructor(
override val scheduler: IScheduler,
override val wire: IWire, //to initialize field with circular dependencies
override val lifetime: Lifetime,
serializationCtx: SerializationCtx? = null,
parentContexts: ProtocolContexts? = null,
parentExtCreated: ISignal<ExtCreationInfoEx>? = null,
private val parentProtocol: Protocol?,
parentExtConfirmation: RdSignal<ExtCreationInfo>? = null,
vararg initialContexts: RdContext<*>
) : IRdDynamic, IProtocol {
Expand All @@ -43,7 +41,7 @@ class Protocol internal constructor(
scheduler: IScheduler,
wire: IWire, //to initialize field with circular dependencies
lifetime: Lifetime,
vararg initialContexts: RdContext<*>) : this(name, serializers, identity, scheduler, wire, lifetime, null, null, null, null, *initialContexts)
vararg initialContexts: RdContext<*>) : this(name, serializers, identity, scheduler, wire, lifetime, null, null, *initialContexts)

override val location: RName = RName(name)
override val outOfSyncModels: ViewableSet<RdExtBase> = ViewableSet(SynchronizedSet())
Expand All @@ -57,11 +55,11 @@ class Protocol internal constructor(
}

override val protocol: IProtocol get() = this
override val serializationContext: SerializationCtx = serializationCtx ?: SerializationCtx(serializers, mapOf("Protocol" to InternRoot<Any>().also {
override val serializationContext: SerializationCtx = parentProtocol?.serializationContext ?: SerializationCtx(serializers, mapOf("Protocol" to InternRoot<Any>().also {
it.rdid = RdId.Null.mix("ProtocolInternRoot")
}))

override val contexts: ProtocolContexts = parentContexts ?: ProtocolContexts(serializationContext)
override val contexts: ProtocolContexts = parentProtocol?.contexts ?: ProtocolContexts(serializationContext)

override val extCreated: ISignal<ExtCreationInfoEx>

Expand All @@ -73,15 +71,15 @@ class Protocol internal constructor(
init {
wire.setupContexts(contexts)

if(serializationCtx == null) {
if(parentProtocol?.serializationContext == null) {
serializationContext.internRoots.getValue("Protocol").bindTopLevel(lifetime, this, "ProtocolInternRoot")
}

initialContexts.forEach {
contexts.registerContext(it)
}

if (parentContexts == null) {
if (parentProtocol?.contexts == null) {
contexts.also {
it.rdid = RdId.Null.mix("ProtocolContextHandler")
AllowBindingCookie.allowBind {
Expand All @@ -90,7 +88,7 @@ class Protocol internal constructor(
}
}

extCreated = parentExtCreated ?: Signal()
extCreated = parentProtocol?.extCreated ?: Signal()
extConfirmation = parentExtConfirmation ?: createExtSignal().also { signal ->
val protocolScheduler = scheduler
signal.scheduler = (protocolScheduler as? ISchedulerWithBackground)?.backgroundScheduler ?: protocolScheduler
Expand Down Expand Up @@ -119,6 +117,10 @@ class Protocol internal constructor(
}

override fun <T: RdExtBase> getOrCreateExtension(clazz: KClass<T>, create: () -> T): T {
parentProtocol?.let {
return it.getOrCreateExtension(clazz, create)
}

Sync.lock(extensions) {
val res = extensions[clazz] ?: run {
val newExtension = create()
Expand All @@ -132,7 +134,11 @@ class Protocol internal constructor(
}
}

override fun <T: RdExtBase> tryGetExtension(clazz: KClass<T>): T? {
override fun <T : RdExtBase> tryGetExtension(clazz: KClass<T>): T? {
parentProtocol?.let {
return it.tryGetExtension(clazz)
}

Sync.lock(extensions) {
val res = extensions[clazz] ?: return null
return castExtension(res, clazz)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ abstract class RdExtBase : RdReactiveBase() {
}

override fun init(lifetime: Lifetime, parentProtocol: IProtocol, ctx: SerializationCtx) {
parentProtocol as Protocol
Protocol.initializationLogger.traceMe { "binding" }

val parentWire = parentProtocol.wire

serializersOwner.register(parentProtocol.serializers)
val serializationCtx = serializationContext ?: return

extWire.realWire = parentWire
lifetime.bracketIfAlive({
Expand All @@ -77,7 +77,7 @@ abstract class RdExtBase : RdReactiveBase() {
}

val signal = createExtSignal()
val proto = Protocol(parentProtocol.name, parentProtocol.serializers, parentProtocol.identity, scheduler, extWire, lifetime, serializationCtx, parentProtocol.contexts, parentProtocol.extCreated, signal).also {
val proto = Protocol(parentProtocol.name, parentProtocol.serializers, parentProtocol.identity, scheduler, extWire, lifetime, parentProtocol, signal).also {
it.outOfSyncModels.flowInto(lifetime, parentProtocol.outOfSyncModels) { model -> model }
}

Expand All @@ -99,7 +99,7 @@ abstract class RdExtBase : RdReactiveBase() {
}

Signal.nonPriorityAdviseSection {
(parentProtocol as Protocol).submitExtCreated(info)
parentProtocol.submitExtCreated(info)
}
}

Expand Down

0 comments on commit 941e40e

Please sign in to comment.