-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Preliminary Scala.js and ScalaNative support
- Loading branch information
1 parent
dd2c5db
commit 0f3c6c1
Showing
16 changed files
with
173 additions
and
43 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package rapid | ||
|
||
import scala.concurrent.ExecutionContext | ||
|
||
object Platform extends RapidPlatform { | ||
override def executionContext: ExecutionContext = org.scalajs.macrotaskexecutor.MacrotaskExecutor.Implicits.global | ||
|
||
override def createFiber[Return](task: Task[Return]): Fiber[Return] = new FutureFiber[Return](task) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import scala.language.implicitConversions | ||
|
||
package object rapid extends RapidPackage { | ||
} |
23 changes: 23 additions & 0 deletions
23
core/jvm-native/src/main/scala/rapid/FutureBlockableFiber.scala
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package rapid | ||
|
||
import scala.concurrent.duration.Duration | ||
import scala.concurrent.{Await, Future} | ||
import scala.util.{Failure, Success, Try} | ||
|
||
class FutureBlockableFiber[Return](val task: Task[Return]) extends BlockableFiber[Return] { | ||
private val future: Future[Return] = Future(task.sync())(Platform.executionContext) | ||
|
||
override protected def invoke(): Return = await() | ||
|
||
override def attempt(): Try[Return] = future.value match { | ||
case Some(value) => value | ||
case None => Try(Await.result(future, Duration.Inf)) | ||
} | ||
|
||
override def await(): Return = Await.result(future, Duration.Inf) | ||
|
||
override def await(duration: Duration): Option[Return] = Try(Await.result(future, duration)) match { | ||
case Success(value) => Some(value) | ||
case Failure(_) => None | ||
} | ||
} |
File renamed without changes.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package spec | ||
|
||
import org.scalatest.matchers.should.Matchers | ||
import org.scalatest.wordspec.AnyWordSpec | ||
import rapid.Task | ||
|
||
class FiberSpec extends AnyWordSpec with Matchers { | ||
"Fiber" should { | ||
"start and await a task" in { | ||
val task = Task { 5 * 5 } | ||
val fiber = task.start() | ||
fiber.await() shouldEqual 25 | ||
} | ||
"handle task failures in fibers" in { | ||
val task = Task { throw new RuntimeException("Failure") } | ||
val fiber = task.start() | ||
an[RuntimeException] should be thrownBy fiber.await() | ||
} | ||
} | ||
} |
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
3 changes: 2 additions & 1 deletion
3
...shared/src/test/scala/spec/TaskSpec.scala → ...native/src/test/scala/spec/TaskSpec.scala
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,9 @@ | ||
package rapid | ||
|
||
import scala.concurrent.ExecutionContext | ||
|
||
object Platform extends RapidPlatform { | ||
// override def createFiber[Return](task: Task[Return]): Fiber[Return] = new VirtualThreadFiber[Return](task) | ||
override def createFiber[Return](task: Task[Return]): Fiber[Return] = new FutureFiber[Return](task) | ||
override def executionContext: ExecutionContext = scala.concurrent.ExecutionContext.Implicits.global | ||
|
||
override def createFiber[Return](task: Task[Return]): Fiber[Return] = new VirtualThreadFiber[Return](task) | ||
} |
File renamed without changes.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package rapid | ||
|
||
import scala.concurrent.ExecutionContext | ||
|
||
object Platform extends RapidPlatform { | ||
override def executionContext: ExecutionContext = scala.concurrent.ExecutionContext.Implicits.global | ||
|
||
override def createFiber[Return](task: Task[Return]): Fiber[Return] = new FutureBlockableFiber[Return](task) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import scala.language.implicitConversions | ||
|
||
package object rapid extends RapidPackage { | ||
implicit def fiber2Blockable[Return](fiber: Fiber[Return]): BlockableFiber[Return] = | ||
fiber.asInstanceOf[BlockableFiber[Return]] | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,17 @@ | ||
package rapid | ||
|
||
import scala.concurrent.ExecutionContext.Implicits.global | ||
import scala.concurrent.{Await, Future} | ||
import scala.concurrent.duration.Duration | ||
import scala.util.Try | ||
import scala.concurrent.Future | ||
import scala.util.{Failure, Try} | ||
|
||
class FutureFiber[Return](val task: Task[Return]) extends BlockableFiber[Return] { | ||
private val future: Future[Return] = Future(task.sync()) | ||
class FutureFiber[Return](val task: Task[Return]) extends Fiber[Return] { | ||
private val future: Future[Return] = Future(task.sync())(Platform.executionContext) | ||
|
||
override protected def invoke(): Return = await() | ||
|
||
override def await(): Return = await(Duration.Inf).get | ||
override def attempt(): Try[Return] = future.value match { | ||
case Some(value) => value | ||
case None => Failure(new RuntimeException("Cannot wait")) | ||
} | ||
|
||
override def await(duration: Duration): Option[Return] = Try(Await.result(future, duration)).toOption | ||
override def await(): Return = attempt().get | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,9 @@ | ||
package rapid | ||
|
||
import scala.concurrent.ExecutionContext | ||
|
||
trait RapidPlatform { | ||
def executionContext: ExecutionContext | ||
|
||
def createFiber[Return](task: Task[Return]): Fiber[Return] | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package spec | ||
|
||
import org.scalatest.matchers.should.Matchers | ||
import org.scalatest.time.SpanSugar.convertIntToGrainOfTime | ||
import org.scalatest.wordspec.AnyWordSpec | ||
import rapid._ | ||
|
||
class BasicsSyncSpec extends AnyWordSpec with Matchers { | ||
"Basics sync" should { | ||
"handle a simple task" in { | ||
val i = Task { | ||
5 * 5 | ||
} | ||
i.sync() should be(25) | ||
} | ||
"handle a simple task mapping" in { | ||
val i = Task { | ||
5 * 5 | ||
} | ||
val s = i.map { v => | ||
s"Value: $v" | ||
} | ||
s.sync() should be("Value: 25") | ||
} | ||
"handle flat mapping" in { | ||
val task = (1 to 10).foldLeft(Task(0))((t, i) => t.flatMap { total => | ||
Task(total + i) | ||
}) | ||
val result = task.sync() | ||
result should be(55) | ||
} | ||
"throw an error and recover" in { | ||
val result = Task[String](throw new RuntimeException("Die Die Die")) | ||
.handleError { _ => | ||
Task.pure("Recovered") | ||
} | ||
.sync() | ||
result should be("Recovered") | ||
} | ||
"process a list of tasks to a task with a list" in { | ||
val list = List( | ||
Task("One"), Task("Two"), Task("Three") | ||
) | ||
list.tasks.sync() should be(List("One", "Two", "Three")) | ||
} | ||
} | ||
} |