-
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make some async api work on Scala.js and Native (#12)
* Move API only working on JVM to JVM specific code * Move shared JS/Native code to src-js-native * Use package private traits to add functionality This implements a different approach which is NOT binary compatible, but is source compatible and avoid duplicating code. The idea is to have mixins with different implementations for every object or trait that has functions existing on JVM only * Fix Js/Native and make ContextSimpleCompanionObject private
- Loading branch information
Showing
12 changed files
with
151 additions
and
82 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,3 @@ | ||
package castor.platform | ||
|
||
private [castor] trait Context |
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,5 @@ | ||
package castor.platform | ||
|
||
private [castor] trait ContextCompanionObject { | ||
private [castor] trait ContextSimpleCompanionObject | ||
} |
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,3 @@ | ||
package castor.platform | ||
|
||
private [castor] trait ContextImpl |
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,7 @@ | ||
package castor.platform | ||
|
||
import scala.concurrent.ExecutionContext | ||
|
||
private [castor] object Platform { | ||
def executionContext = ExecutionContext.global | ||
} |
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 castor.platform | ||
|
||
import castor._ | ||
|
||
private [castor] trait Context { | ||
def scheduleMsg[T](a: Actor[T], msg: T, time: java.time.Duration) | ||
(implicit fileName: sourcecode.FileName, | ||
line: sourcecode.Line): Unit | ||
} |
47 changes: 47 additions & 0 deletions
47
castor/src-jvm/castor/platform/ContextCompanionObject.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,47 @@ | ||
package castor.platform | ||
|
||
import castor._ | ||
|
||
import scala.concurrent.ExecutionContext | ||
|
||
import java.util.concurrent.{Executors, ThreadFactory} | ||
|
||
private [castor] trait ContextCompanionObject { | ||
/** | ||
* Castor actor context based on a fixed thread pool | ||
*/ | ||
def makeThreadPool(numThreads: Int, daemon: Boolean) = Executors.newFixedThreadPool( | ||
numThreads, | ||
new ThreadFactory { | ||
override def newThread(r: Runnable) = { | ||
val t = new Thread(r) | ||
t.setDaemon(daemon) | ||
t | ||
} | ||
} | ||
) | ||
class ThreadPool(numThreads: Int = Runtime.getRuntime().availableProcessors(), | ||
daemon: Boolean = true, | ||
logEx: Throwable => Unit = _.printStackTrace()) extends Context.Impl{ | ||
val threadPool = makeThreadPool(numThreads, daemon) | ||
|
||
val executionContext = ExecutionContext.fromExecutorService(threadPool) | ||
|
||
def reportFailure(cause: Throwable): Unit = logEx(cause) | ||
def shutdown() = threadPool.shutdownNow() | ||
} | ||
|
||
/** | ||
* [[castor.Context.ThreadPool]] used for testing; tracks scheduling and completion of | ||
* tasks and futures, so we can support a `.waitForInactivity()` method to wait | ||
* until the system is quiescient | ||
*/ | ||
class TestThreadPool(numThreads: Int = Runtime.getRuntime().availableProcessors(), | ||
daemon: Boolean = true, | ||
logEx: Throwable => Unit = _.printStackTrace()) | ||
extends ThreadPool(numThreads, daemon, logEx) with Context.TestBase | ||
|
||
private [castor] trait ContextSimpleCompanionObject { | ||
lazy val threadPool = makeThreadPool(Runtime.getRuntime().availableProcessors(), true) | ||
} | ||
} |
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,34 @@ | ||
package castor.platform | ||
|
||
import java.util.concurrent.{Executors, ThreadFactory, TimeUnit} | ||
|
||
import castor._ | ||
|
||
private [castor] trait ContextImpl extends Context { this: Context.Impl => | ||
lazy val scheduler = Executors.newSingleThreadScheduledExecutor( | ||
new ThreadFactory { | ||
def newThread(r: Runnable): Thread = { | ||
val t = new Thread(r, "ActorContext-Scheduler-Thread") | ||
t.setDaemon(true) | ||
t | ||
} | ||
} | ||
) | ||
|
||
def scheduleMsg[T](a: Actor[T], | ||
msg: T, delay: java.time.Duration) | ||
(implicit fileName: sourcecode.FileName, | ||
line: sourcecode.Line) = { | ||
val token = reportSchedule(a, msg, fileName, line) | ||
scheduler.schedule[Unit]( | ||
new java.util.concurrent.Callable[Unit] { | ||
def call(): Unit = { | ||
a.send(msg)(fileName, line) | ||
reportComplete(token) | ||
} | ||
}, | ||
delay.toMillis, | ||
TimeUnit.MILLISECONDS | ||
) | ||
} | ||
} |
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,11 @@ | ||
package castor.platform | ||
|
||
import castor._ | ||
|
||
import scala.concurrent.ExecutionContext | ||
|
||
import java.util.concurrent.{Executors, ThreadFactory, TimeUnit} | ||
|
||
private [castor] object Platform { | ||
def executionContext = ExecutionContext.fromExecutorService(Context.Simple.threadPool) | ||
} |
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,11 @@ | ||
package castor | ||
|
||
trait Actor[T]{ | ||
def send(t: T) | ||
(implicit fileName: sourcecode.FileName, | ||
line: sourcecode.Line): Unit | ||
|
||
def sendAsync(f: scala.concurrent.Future[T]) | ||
(implicit fileName: sourcecode.FileName, | ||
line: sourcecode.Line): Unit | ||
} |
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