-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update for KyoApp in JS env, allow JS's KyoApp to use multiple runs
- Loading branch information
Showing
6 changed files
with
105 additions
and
25 deletions.
There are no files selected for viewing
30 changes: 30 additions & 0 deletions
30
kyo-core/js/src/main/scala/kyo/KyoAppPlatformSpecific.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,30 @@ | ||
package kyo | ||
|
||
import kyo.Maybe.Absent | ||
import kyo.Maybe.Present | ||
|
||
abstract class KyoAppPlatformSpecific extends KyoApp.Base[Async & Resource & Abort[Throwable]]: | ||
|
||
private var maybePreviousAsync: Maybe[Unit < (Async & Abort[Throwable])] = Absent | ||
|
||
final override protected def run[A: Flat](v: => A < (Async & Resource & Abort[Throwable]))(using Frame): Unit = | ||
import AllowUnsafe.embrace.danger | ||
val currentAsync: Unit < (Async & Abort[Throwable]) = | ||
Async.timeout(timeout)(handle(v)).map(value => printResult(Result.success(value))) | ||
maybePreviousAsync = maybePreviousAsync match | ||
case Absent => Present(currentAsync) | ||
case Present(previousAsync) => Present(previousAsync.flatMap(_ => currentAsync)) | ||
if maybeProc.isEmpty then | ||
maybeProc = Present(() => | ||
maybePreviousAsync match | ||
case Absent => () | ||
case Present(previousAsync) => | ||
val racedAsyncIO = Clock.repeatWithDelay(1.hours)(()).map { fiber => | ||
Async.race(Seq(fiber.get, previousAsync)) | ||
} | ||
val _ = IO.Unsafe.run(Async.run(racedAsyncIO)).eval | ||
) | ||
end if | ||
end run | ||
|
||
end KyoAppPlatformSpecific |
22 changes: 22 additions & 0 deletions
22
kyo-core/jvm/src/main/scala/kyo/KyoAppPlatformSpecific.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,22 @@ | ||
package kyo | ||
|
||
import scala.collection.mutable.ListBuffer | ||
|
||
abstract class KyoAppPlatformSpecific extends KyoApp.Base[Async & Resource & Abort[Throwable]]: | ||
|
||
private val procListBuffer = new ListBuffer[() => Unit] | ||
|
||
final override protected def run[A: Flat](v: => A < (Async & Resource & Abort[Throwable]))(using Frame): Unit = | ||
import AllowUnsafe.embrace.danger | ||
procListBuffer += (() => | ||
val result = IO.Unsafe.run(Abort.run(Async.runAndBlock(timeout)(handle(v)))).eval | ||
printResult(result) | ||
) | ||
if maybeProc.isEmpty then | ||
maybeProc = Present(() => | ||
for proc <- procListBuffer do proc() | ||
) | ||
end if | ||
end run | ||
|
||
end KyoAppPlatformSpecific |
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
7 changes: 0 additions & 7 deletions
7
kyo-scheduler/js/src/main/scala/java/util/concurrent/ThreadFactory.scala
This file was deleted.
Oops, something went wrong.
10 changes: 6 additions & 4 deletions
10
kyo-scheduler/js/src/main/scala/kyo/scheduler/util/Threads.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 |
---|---|---|
@@ -1,13 +1,15 @@ | ||
package kyo.scheduler.util | ||
|
||
import java.lang.Thread | ||
import java.util.concurrent.ThreadFactory | ||
import java.util.concurrent.ThreadFactoryStub | ||
|
||
object Threads { | ||
|
||
def apply(name: String): ThreadFactory = | ||
ThreadFactoryStub.get | ||
def apply(name: String): ThreadFactory = apply(name, _ => Thread.currentThread()) | ||
|
||
def apply(name: String, create: Runnable => Thread): ThreadFactory = | ||
ThreadFactoryStub.get | ||
new ThreadFactory { | ||
override def newThread(runnable: Runnable): Thread = create(runnable) | ||
} | ||
|
||
} |