Created
May 9, 2022 16:38
-
-
Save oscar-broman/0b0a7a5d31f9624c41565e48f3bd268b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package react | |
import scala.annotation.implicitNotFound | |
import scala.util.NotGiven | |
object Domain { | |
sealed trait Observable[T] { | |
def observe(op: => T): Unit | |
} | |
} | |
class Domain { | |
import Domain._ | |
@implicitNotFound("This method can only be used within a react context") | |
class Context { | |
@implicitNotFound("Signals can only be subscribed to inside dependency nodes") | |
class DependencyNodeContext | |
class LeafNodeContext | |
class NoContext | |
} | |
given this.type = this | |
given Context = new Context | |
sealed trait Signal[T] extends Observable[T] { | |
def observe(op: => T): Unit = ??? | |
def apply()(using | |
ctx: Context, | |
@implicitNotFound("This method can only be used within a react context") | |
using: NotGiven[ctx.NoContext] | |
)(using ev: ctx.DependencyNodeContext): T | |
def now(using | |
ctx: Context, | |
@implicitNotFound("This method can only be used within a react context") | |
using: NotGiven[ctx.NoContext] | |
)(using | |
@implicitNotFound("Signals may only be read from inside dependency nodes or leaf nodes") | |
ev: ctx.DependencyNodeContext | ctx.LeafNodeContext | |
): T | |
} | |
class Var[T](initial: T)(using | |
ctx: Context, | |
@implicitNotFound("This method can only be used within a react context") | |
using: NotGiven[ctx.NoContext] | |
)(using | |
@implicitNotFound("Signals may not be created inside leaf nodes") | |
ev1: NotGiven[ctx.LeafNodeContext], | |
@implicitNotFound("Signals may not be created inside dependency nodes") | |
ev2: NotGiven[ctx.DependencyNodeContext], | |
) extends Signal[T] { | |
def apply()(using | |
ctx: Context, | |
@implicitNotFound("This method can only be used within a react context") | |
using: NotGiven[ctx.NoContext] | |
)(using ctx.DependencyNodeContext): T = ??? | |
def now(using | |
ctx: Context, | |
@implicitNotFound("This method can only be used within a react context") | |
using: NotGiven[ctx.NoContext] | |
)(using | |
@implicitNotFound("Signals may only be read from inside dependency nodes or leaf nodes") | |
ev: ctx.DependencyNodeContext | ctx.LeafNodeContext | |
): T = ??? | |
} | |
class Observer[T](signal: Signal[T], op: T => Unit) { | |
def dispose(): Unit = ??? | |
} | |
class StrictSignal[T](expr: => T) extends Signal[T] { | |
def apply()(using | |
ctx: Context, | |
@implicitNotFound("This method can only be used within a react context") | |
using: NotGiven[ctx.NoContext] | |
)(using ctx.DependencyNodeContext): T = ??? | |
def now(using | |
ctx: Context, | |
@implicitNotFound("This method can only be used within a react context") | |
using: NotGiven[ctx.NoContext] | |
)(using | |
@implicitNotFound("Signals may only be read from inside dependency nodes or leaf nodes") | |
ev: ctx.DependencyNodeContext | ctx.LeafNodeContext | |
): T = ??? | |
} | |
def Strict[T](using | |
ctx: Context, | |
@implicitNotFound("This method can only be used within a react context") | |
using: NotGiven[ctx.NoContext] | |
)(using | |
@implicitNotFound("Signals may not be created inside leaf nodes or dependency nodes") | |
ev: NotGiven[ctx.DependencyNodeContext | ctx.LeafNodeContext] | |
)(expr: ctx.DependencyNodeContext ?=> T): StrictSignal[T] = { | |
given depCtx: ctx.DependencyNodeContext = ??? | |
new StrictSignal(expr) | |
} | |
def observe[T](using | |
ctx: Context, | |
@implicitNotFound("This method can only be used within a react context") | |
ev1: NotGiven[ctx.NoContext], | |
@implicitNotFound("Observers can not be created inside dependency nodes") | |
ev2: NotGiven[ctx.DependencyNodeContext] | |
)(signal: Signal[T])(op: ctx.LeafNodeContext ?=> T => Unit): Observer[T] = { | |
given leafCtx: ctx.LeafNodeContext = ??? | |
Observer(signal, op) | |
} | |
def doLater(using | |
ctx: Context, | |
@implicitNotFound("This method can only be used within a react context") | |
using: NotGiven[ctx.NoContext] | |
)(op: ctx.LeafNodeContext ?=> Unit): Unit = { | |
given leafCtx: ctx.LeafNodeContext = ??? | |
op | |
} | |
def schedule(op: Context ?=> Unit)(using | |
ctx: Context, | |
@implicitNotFound("This method can only be used within a react context") | |
using: NotGiven[ctx.NoContext] | |
): Unit = { | |
??? | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment