-
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.
Fixes #7
- Loading branch information
Showing
8 changed files
with
196 additions
and
0 deletions.
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
js/src/main/scala/pl/metastack/metarx/AsyncScheduler.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,36 @@ | ||
package pl.metastack.metarx | ||
|
||
import scala.concurrent.duration._ | ||
|
||
import scala.scalajs.js | ||
|
||
class AsyncScheduler extends Scheduler { | ||
type Timeout = js.Dynamic | ||
type Interval = js.Dynamic | ||
|
||
def setTimeout(delayMillis: Long, r: Runnable): Timeout = { | ||
val lambda: js.Function = () => r.run() | ||
js.Dynamic.global.setTimeout(lambda, delayMillis) | ||
} | ||
|
||
def setInterval(intervalMillis: Long, r: Runnable): Interval = { | ||
val lambda: js.Function = () => r.run() | ||
js.Dynamic.global.setInterval(lambda, intervalMillis) | ||
} | ||
|
||
def clearTimeout(task: Timeout): Unit = | ||
js.Dynamic.global.clearTimeout(task) | ||
|
||
def clearInterval(task: Interval): Unit = | ||
js.Dynamic.global.clearInterval(task) | ||
|
||
def schedule(interval: FiniteDuration, r: Runnable): Cancelable = { | ||
val task = setInterval(interval.toMillis, r) | ||
Cancelable(clearInterval(task)) | ||
} | ||
|
||
def scheduleOnce(initialDelay: FiniteDuration, r: Runnable): Cancelable = { | ||
val task = setTimeout(initialDelay.toMillis, r) | ||
Cancelable(clearTimeout(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,5 @@ | ||
package pl.metastack.metarx | ||
|
||
object Platform { | ||
implicit lazy val DefaultScheduler: Scheduler = new AsyncScheduler | ||
} |
32 changes: 32 additions & 0 deletions
32
jvm/src/main/scala/pl/metastack/metarx/AsyncScheduler.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,32 @@ | ||
package pl.metastack.metarx | ||
|
||
import java.util.concurrent.{ScheduledExecutorService, TimeUnit} | ||
|
||
import scala.concurrent.duration._ | ||
import scala.concurrent.ExecutionContext | ||
|
||
class AsyncScheduler(s: ScheduledExecutorService, | ||
ec: ExecutionContext) extends Scheduler { | ||
def schedule(interval: FiniteDuration, r: Runnable): Cancelable = | ||
schedule(interval.length, interval.unit, r) | ||
|
||
def schedule(interval: Long, unit: TimeUnit, r: Runnable): Cancelable = { | ||
require(interval > 0) | ||
val initialDelay = interval | ||
val task = s.scheduleAtFixedRate(r, initialDelay, interval, unit) | ||
Cancelable(task.cancel(true)) | ||
} | ||
|
||
def scheduleOnce(initialDelay: FiniteDuration, r: Runnable): Cancelable = | ||
scheduleOnce(initialDelay.length, initialDelay.unit, r) | ||
|
||
def scheduleOnce(initialDelay: Long, unit: TimeUnit, r: Runnable): Cancelable = { | ||
if (initialDelay <= 0) { | ||
ec.execute(r) | ||
Cancelable() | ||
} else { | ||
val task = s.schedule(r, initialDelay, unit) | ||
Cancelable(task.cancel(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,11 @@ | ||
package pl.metastack.metarx | ||
|
||
import java.util.concurrent.Executors | ||
|
||
import scala.concurrent.ExecutionContext | ||
|
||
object Platform { | ||
implicit lazy val DefaultScheduler: Scheduler = new AsyncScheduler( | ||
Executors.newSingleThreadScheduledExecutor(), | ||
This comment has been minimized.
Sorry, something went wrong. |
||
ExecutionContext.Implicits.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
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,44 @@ | ||
package pl.metastack.metarx | ||
|
||
import scala.concurrent.duration._ | ||
|
||
/** | ||
* Inspired from Monifu's scheduling code. | ||
*/ | ||
trait Scheduler { | ||
def schedule(interval: FiniteDuration, r: Runnable): Cancelable | ||
def scheduleOnce(initialDelay: FiniteDuration, action: Runnable): Cancelable | ||
|
||
def schedule(interval: FiniteDuration)(action: => Unit): Cancelable = | ||
schedule(interval, new Runnable { | ||
def run(): Unit = action | ||
}) | ||
|
||
def scheduleOnce(initialDelay: FiniteDuration)(action: => Unit): Cancelable = | ||
scheduleOnce(initialDelay, new Runnable { | ||
def run(): Unit = action | ||
}) | ||
|
||
def currentTimeMillis(): Long = System.currentTimeMillis() | ||
} | ||
|
||
trait Cancelable { | ||
def cancel(): Boolean | ||
} | ||
|
||
object Cancelable { | ||
def apply(): Cancelable = apply({}) | ||
|
||
def apply(callback: => Unit): Cancelable = | ||
new Cancelable { | ||
var isCanceled = false | ||
|
||
def cancel(): Boolean = | ||
if (isCanceled) false | ||
else { | ||
isCanceled = true | ||
callback | ||
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
29 changes: 29 additions & 0 deletions
29
shared/src/test/scala/pl/metastack/metarx/SchedulerTest.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,29 @@ | ||
package pl.metastack.metarx | ||
|
||
import minitest.SimpleTestSuite | ||
|
||
import scala.collection.mutable.ArrayBuffer | ||
|
||
object SchedulerTest extends SimpleTestSuite { | ||
|
||
test("schedule()") { | ||
import scala.concurrent.duration._ | ||
val ch = Channel[Int]() | ||
|
||
val scheduler: Scheduler = Platform.DefaultScheduler | ||
var i = 0 | ||
val task = scheduler.schedule(500.millis) { | ||
ch := i | ||
i += 1 | ||
} | ||
|
||
val collected = ArrayBuffer.empty[Int] | ||
ch.attach(collected += _) | ||
|
||
scheduler.scheduleOnce(2100.millis) { | ||
assertEquals(collected, Seq(0, 1, 2, 3)) | ||
task.cancel() | ||
} | ||
} | ||
|
||
} |
What is the reasoning about hard coding the executor, rather than letting the user supply it using implicit?