Skip to content

Commit

Permalink
fix non-static transaction lookup
Browse files Browse the repository at this point in the history
  • Loading branch information
rmgk committed Mar 2, 2024
1 parent ff87a1a commit 67a19c1
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 23 deletions.
15 changes: 3 additions & 12 deletions Modules/Reactives/shared/src/main/scala/reactives/core/Core.scala
Original file line number Diff line number Diff line change
Expand Up @@ -356,16 +356,8 @@ trait Transaction[State[_]] {
Tracing.observe(Tracing.Drop(source, sink))
}
}
object Transaction {
inline def summon[State[_]]: Transaction[State] = scala.compiletime.summonFrom {
case tx: Transaction[State] => tx
case at: AdmissionTicket[State] => at.tx
case st: StaticTicket[State] => st.tx
}
}

/** Scheduler that defines the basic data-types available to the user and creates turns for propagation handling.
*/
/** Scheduler that defines the basic data-types available to the user and creates turns for propagation handling. */
@implicitNotFound(msg = "Could not find an implicit scheduler. Did you forget an import?")
trait Scheduler[S[_]] {

Expand All @@ -387,20 +379,19 @@ object Scheduler {
given defaultScheduler: Scheduler[Interface.State] = Interface.default
}


trait SchedulerImpl[State[_], Tx <: Transaction[State]] extends Scheduler[State] {
override def dynamicScope: DynamicScopeImpl[State, Tx] = new DynamicScopeImpl[State, Tx](this)
}


/** Provides the capability to look up transactions in the dynamic scope. */
trait DynamicScope[State[_]] {
private[reactives] def dynamicTransaction[T](f: Transaction[State] => T): T
def maybeTransaction: Option[Transaction[State]]
}

object DynamicScope {
class DynamicScopeImpl[State[_], Tx <: Transaction[State]](scheduler: SchedulerImpl[State, Tx]) extends DynamicScope[State] {
class DynamicScopeImpl[State[_], Tx <: Transaction[State]](scheduler: SchedulerImpl[State, Tx])
extends DynamicScope[State] {

final private[reactives] def dynamicTransaction[T](f: Transaction[State] => T): T = {
_currentTransaction.value match {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ import reactives.operator.Interface

case class TransactionScope[State[_]](static: Option[Transaction[State]])
object TransactionScope extends LowPrioTransactionScope {
given static[State[_]](using tx: Transaction[State]): TransactionScope[State] = TransactionScope(Some(tx))
inline given search[State[_]](using tx: Transaction[State]): TransactionScope[State] = static(tx)
inline given search[State[_]](using at: AdmissionTicket[State]): TransactionScope[State] = static(at.tx)
inline given Search[State[_]](using st: StaticTicket[State]): TransactionScope[State] = static(st.tx)

def static[State[_]](transaction: Transaction[State]): TransactionScope[State] = TransactionScope(Some(transaction))

// non inline give variant for macro
def fromTicket[State[_]](ticket: StaticTicket[State]): TransactionScope[State] = TransactionScope(Some(ticket.tx))
}
trait LowPrioTransactionScope {
Expand Down Expand Up @@ -50,27 +56,25 @@ trait PlanTransactionScope[State[_]] {

object PlanTransactionScope {

implicit def fromScheduler[State[_]](scheduler: Scheduler[State]): DynamicTransactionLookup[State] = DynamicTransactionLookup(scheduler)
implicit def fromScheduler[State[_]](scheduler: Scheduler[State]): DynamicTransactionLookup[State] =
DynamicTransactionLookup(scheduler)

case class StaticInTransaction[State[_]](tx: Transaction[State], scheduler: Scheduler[State])
extends PlanTransactionScope[State] {
override def planTransaction(inintialWrites: ReSource.of[State]*)(admissionPhase: AdmissionTicket[State] => Unit)
: Unit =
override def planTransaction(inintialWrites: ReSource.of[State]*)(admission: AdmissionTicket[State] => Unit): Unit =
tx.observe { () =>
scheduler.forceNewTransaction(inintialWrites*)(admissionPhase)
scheduler.forceNewTransaction(inintialWrites*)(admission)
}
}

case class DynamicTransactionLookup[State[_]](scheduler: Scheduler[State])
extends PlanTransactionScope[State] {
override def planTransaction(inintialWrites: ReSource.of[State]*)(admissionPhase: AdmissionTicket[State] => Unit)
: Unit =
case class DynamicTransactionLookup[State[_]](scheduler: Scheduler[State]) extends PlanTransactionScope[State] {
override def planTransaction(inintialWrites: ReSource.of[State]*)(admission: AdmissionTicket[State] => Unit): Unit =
scheduler.dynamicScope.maybeTransaction match
case Some(tx) => tx.observe { () =>
scheduler.forceNewTransaction(inintialWrites*)(admissionPhase)
scheduler.forceNewTransaction(inintialWrites*)(admission)
}
case None =>
scheduler.forceNewTransaction(inintialWrites*)(admissionPhase)
scheduler.forceNewTransaction(inintialWrites*)(admission)
}

inline given search(using ts: TransactionScope[Interface.State]): PlanTransactionScope[Interface.State] =
Expand Down

0 comments on commit 67a19c1

Please sign in to comment.