Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add features: createRoot, PromiseWrapper, and react lazy #587

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions core/src/main/scala/slinky/core/facade/React.scala
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ trait ReactRef[T] extends js.Object {
var current: T @uncheckedVariance = js.native
}

trait LazyResult[P] extends js.Object {
val default: FunctionalComponent[P]
}

@js.native
@JSImport("react", JSImport.Namespace, "React")
private[slinky] object ReactRaw extends js.Object {
Expand All @@ -79,6 +83,9 @@ private[slinky] object ReactRaw extends js.Object {

def memo(fn: js.Function, compare: js.UndefOr[js.Object]): js.Function = js.native

final def `lazy`[P](f: js.Function0[js.Promise[LazyResult[P]]]): FunctionalComponent[P] = js.native


@js.native
object Children extends js.Object {
def map(children: ReactChildren, transformer: js.Function1[ReactElement, ReactElement]): ReactChildren = js.native
Expand Down
30 changes: 30 additions & 0 deletions core/src/main/scala/slinky/core/facade/Suspense.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ import slinky.core.{BuildingComponent, ExternalComponent, ExternalPropsWriterPro

import scala.scalajs.js
import scala.scalajs.js.|
import scala.concurrent.Future
import scala.scalajs.js
import scala.scalajs.js.JavaScriptException
import scala.scalajs.js.JSConverters._
import scala.util.{Failure, Success}
import concurrent.ExecutionContext.Implicits.global


object Suspense
extends ExternalComponent()(new Writer[Suspense.Props] {
Expand All @@ -16,3 +23,26 @@ object Suspense

def apply(fallback: ReactElement): BuildingComponent[Nothing, js.Object] = apply(Props(fallback))
}


object Suspended {

class PromiseWrap[T](suspend: Future[T]) extends js.Object {
private val pending = suspend.toJSPromise
private var data: Option[T] = None
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this shouldn't be private as I'm finding often non-presentational logic may want to check the existence of data without triggering unnecessary exceptions to be thrown

private var throwable: Option[Throwable] = None

suspend.onComplete{
case Success(result) => data = Some(result)
case Failure(error) => throwable = Some(error)
}

def read(): T = (data, throwable) match {
case (Some(data), _) => data
case (None, Some(error)) => throw error
case _ => throw JavaScriptException(pending)
}

}
}

11 changes: 11 additions & 0 deletions web/src/main/scala/slinky/web/ReactDOM.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import org.scalajs.dom.Element

import scala.scalajs.js
import scala.scalajs.js.annotation.JSImport
import org.scalajs.dom.Element

@js.native
@JSImport("react-dom", JSImport.Namespace, "ReactDOM")
Expand Down Expand Up @@ -35,3 +36,13 @@ object ReactDOMServer extends js.Object {
def renderToNodeStream(element: ReactElement): js.Object = js.native
def renderToStaticNodeStream(element: ReactElement): js.Object = js.native
}

trait ReactRoot extends js.Object {
def render(component: ReactElement): ReactInstance
}

@js.native
@JSImport("react-dom/client", JSImport.Namespace, "ReactDOM")
object ReactDOMClient extends js.Object {
def createRoot(target: Element): ReactRoot = js.native
}