Skip to content

Commit

Permalink
backpaddle a bit on separation of searches to make macro more systematic
Browse files Browse the repository at this point in the history
  • Loading branch information
rmgk committed Mar 2, 2024
1 parent 862c80e commit 8f4b8e2
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import reactives.operator.Interface
import reactives.structure.RExceptions.ObservedException
import reactives.structure.{Observe, Pulse}
import reactives.operator.*
import reactives.operator.Interface.State

import scala.annotation.targetName
import scala.scalajs.js
Expand Down Expand Up @@ -48,7 +47,7 @@ class Tags(val addDebuggingIds: Boolean) {
}

extension (input: Input)
def inputEntered(using creationTicket: CreationTicket[State], scheduler: PlanTransactionScope[State]): Event[String] = {
def inputEntered(using creationTicket: CreationTicket[Interface.State], scheduler: PlanTransactionScope[Interface.State]): Event[String] = {
val handler: Event.CBR[KeyboardEvent, Unit] = Event.fromCallback(input.onkeyup = Event.handle(_))

handler.event
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
package reactives.core

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))
def fromTicket[State[_]](ticket: StaticTicket[State]): TransactionScope[State] = TransactionScope(Some(ticket.tx))
}
trait LowPrioTransactionScope {
given dynamicTransactionScope[State[_]]: TransactionScope[State] = TransactionScope(None)
}

trait CreationScope[State[_]] {
def embedCreation[T](f: Transaction[State] => T): T

Expand All @@ -18,6 +29,7 @@ trait CreationScope[State[_]] {
embedCreation(_.initializer.createSource(intv)(instantiateReactive))
}
}

object CreationScope {

case class StaticCreationScope[State[_]](tx: Transaction[State]) extends CreationScope[State] {
Expand All @@ -27,15 +39,9 @@ object CreationScope {
override def embedCreation[T](f: Transaction[State] => T): T = ds.dynamicTransaction(f)
}

def makeDynamicIndirectionForMacroReplacement[State[_]](ds: DynamicScope[State]): DynamicCreationScope[State] =
new DynamicCreationScope(ds)
def makeFromTicket[State[_]](tick: StaticTicket[State]): StaticCreationScope[State] = new StaticCreationScope(tick.tx)

inline given search[State[_]]: CreationScope[State] = scala.compiletime.summonFrom {
case tx: Transaction[State] => StaticCreationScope(tx)
case ds: DynamicScope[State] => makeDynamicIndirectionForMacroReplacement(ds)
case sched: Scheduler[State] => makeDynamicIndirectionForMacroReplacement(sched.dynamicScope)
}
inline given search(using ts: TransactionScope[Interface.State]): CreationScope[Interface.State] = ts.static match
case None => DynamicCreationScope(Interface.default.dynamicScope)
case Some(tx) => StaticCreationScope(tx)
}

trait PlanTransactionScope[State[_]] {
Expand Down Expand Up @@ -64,16 +70,7 @@ object PlanTransactionScope {
scheduler.forceNewTransaction(inintialWrites*)(admissionPhase)
}

inline given summon[State[_]]: PlanTransactionScope[State] = scala.compiletime.summonFrom {
case scheduler: Scheduler[State] =>
scala.compiletime.summonFrom {
case tx: Transaction[State] =>
StaticInTransaction(tx, scheduler)
case ds: DynamicScope[State] =>
DynamicTransactionLookup(ds, scheduler)
case _ =>
DynamicTransactionLookup(scheduler.dynamicScope, scheduler)
}

}
inline given search(using ts: TransactionScope[Interface.State]): PlanTransactionScope[Interface.State] = ts.static match
case None => DynamicTransactionLookup(Interface.default.dynamicScope, Interface.default)
case Some(tx) => StaticInTransaction(tx, Interface.default)
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class RESubscriber[T](evt: Evt[T], fac: Scheduler[State]) extends Subscriber[T]
thrw match
case ex: Exception =>
import fac.dynamicScope
PlanTransactionScope.summon.planTransaction(evt) { implicit turn => evt.admitPulse(Pulse.Exceptional(ex)) }
PlanTransactionScope.search.planTransaction(evt) { implicit turn => evt.admitPulse(Pulse.Exceptional(ex)) }
case other => throw other
}
override def onSubscribe(s: Subscription): Unit =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package reactives.macros

import reactives.core.{CreationScope, Transaction}
import reactives.core.{CreationScope, Transaction, TransactionScope}

import scala.quoted.*

Expand Down Expand Up @@ -101,8 +101,8 @@ object MacroLegos {

override def transformTerm(tree: quotes.reflect.Term)(owner: quotes.reflect.Symbol): quotes.reflect.Term = {
tree match
case Apply(TypeApply(Ident("makeDynamicIndirectionForMacroReplacement"), ta), _) =>
Apply(TypeApply(Ident(TermRef(TypeRepr.of[CreationScope.type], "makeFromTicket")), ta), List(ticket))
case TypeApply(Ident("dynamicTransactionScope"), ta) =>
Apply(TypeApply(Ident(TermRef(TypeRepr.of[TransactionScope.type], "fromTicket")), ta), List(ticket))
case other =>
super.transformTerm(tree)(owner)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ trait Interface {

/** @group internal */
given implicitScheduler: Scheduler[State] = scheduler

/** @group internal */
given implicitSCope: DynamicScope[State] = scheduler.dynamicScope

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ class ObserveAfterCommit extends munit.FunSuite {

assertEquals(v1.now, 1)

callback()
intercept[IllegalStateException](callback())

assertEquals(v1.now, 2)
assertEquals(v1.now, 1)

}

Expand Down

0 comments on commit 8f4b8e2

Please sign in to comment.