Skip to content

Commit 9367027

Browse files
committed
Fix #2: BusyWorker.doTask - incorrect behavior with code blocks.
Disallow use of code blocks in place of SimpleTask.
1 parent 72fb5b2 commit 9367027

File tree

3 files changed

+18
-14
lines changed

3 files changed

+18
-14
lines changed

ReadMe.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ It will also change the cursor in the root pane to busy.
112112
When task is done, the cursor will be changed back to default and root pane will enabled back.
113113

114114
```scala
115-
new BusyWorker("Simple Task", parentWindow).doTask {
115+
new BusyWorker("Simple Task", parentWindow).doTask { () =>
116116
Thread.sleep(1000)
117117
print(1 + 1)
118118
}

scalafx-extras-demos/src/main/scala/org/scalafx/extras/BusyWorkerDemo.scala

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,14 +86,14 @@ object BusyWorkerDemo extends JFXApp {
8686
maxWidth = Double.MaxValue
8787
},
8888
new Button("Task with simple progress indicator") {
89-
onAction = () => busyWorker.doTask("Task 2") {
89+
onAction = () => busyWorker.doTask("Task 2") { () =>
9090
println("Task 2")
9191
Thread.sleep(3000)
9292
}
9393
maxWidth = Double.MaxValue
9494
},
9595
new Button("Task failing with exception (on 7)") {
96-
onAction = () => busyWorker.doTask("Task 1")(
96+
onAction = () => busyWorker.doTask("Task 3")(
9797
new SimpleTask[String] {
9898
override def call(): String = {
9999
val maxItems = 10
@@ -120,6 +120,15 @@ object BusyWorkerDemo extends JFXApp {
120120
}
121121
)
122122
maxWidth = Double.MaxValue
123+
},
124+
new Button("Print execution thread") {
125+
onAction = () => busyWorker.doTask("Task 4") { () =>
126+
println("1: Thread '" + Thread.currentThread().getName + "'")
127+
println("2: Thread '" + Thread.currentThread().getName + "'")
128+
println("3: Thread '" + Thread.currentThread().getName + "'")
129+
println("4: Thread '" + Thread.currentThread().getName + "'")
130+
}
131+
maxWidth = Double.MaxValue
123132
}
124133
).map(_.delegate)
125134
}

scalafx-extras/src/main/scala/org/scalafx/extras/BusyWorker.scala

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,6 @@ import scala.runtime.NonLocalReturnControl
4343

4444
object BusyWorker {
4545

46-
//noinspection ConvertExpressionToSAM
47-
implicit def apply[R](op: => R): SimpleTask[R] = new SimpleTask[R] {
48-
def call(): R = op
49-
}
50-
5146
implicit def apply(nodes: Seq[Node]): Seq[jfxs.Node] = nodes.map(_.delegate)
5247

5348
/**
@@ -252,7 +247,7 @@ class BusyWorker private(val title: String,
252247
*
253248
* Example of running a task without waiting to complete, using a lambda
254249
* {{{
255-
* worker.doTask{
250+
* worker.doTask{ () =>
256251
* // Some workload code, does not produce value ot it is discard
257252
* Thread.sleep(1000)
258253
* print(1 + 1)
@@ -262,7 +257,7 @@ class BusyWorker private(val title: String,
262257
* Example of stating a task (with a lambda) and waiting till it finishes and returns a result
263258
* {{{
264259
* // This code will return before workload is completed
265-
* val future = worker.doTask{
260+
* val future = worker.doTask{ () =>
266261
* // Some workload code producing final value
267262
* Thread.sleep(1000)
268263
* 1 + 1
@@ -294,7 +289,7 @@ class BusyWorker private(val title: String,
294289
* @param task actions to perform, can be provided a as a lambda op: => R, see examples above.
295290
* @return `Future` that can be used to retrieve result produced the workload, if any.
296291
*/
297-
def doTask[R](implicit task: SimpleTask[R]): Future[R] = {
292+
def doTask[R](task: SimpleTask[R]): Future[R] = {
298293
doTask(title)(task)
299294
}
300295

@@ -305,7 +300,7 @@ class BusyWorker private(val title: String,
305300
*
306301
* Example of running a task without waiting to complete, using a lambda
307302
* {{{
308-
* worker.doTask("My Task") {
303+
* worker.doTask("My Task") { () =>
309304
* // Some workload code, does not produce value ot it is discard
310305
* Thread.sleep(1000)
311306
* print(1 + 1)
@@ -315,7 +310,7 @@ class BusyWorker private(val title: String,
315310
* Example of stating a task (with a lambda) and waiting till it finishes and returns a result
316311
* {{{
317312
* // This code will return before workload is completed
318-
* val future = worker.doTask("My Task") {
313+
* val future = worker.doTask("My Task") { () =>
319314
* // Some workload code producing final value
320315
* Thread.sleep(1000)
321316
* 1 + 1
@@ -348,7 +343,7 @@ class BusyWorker private(val title: String,
348343
* @param task actions to perform, can be provided a as a lambda op: => R, see examples above.
349344
* @return `Future` that can be used to retrieve result produced the workload, if any.
350345
*/
351-
def doTask[R](name: String)(implicit task: SimpleTask[R]): Future[R] = {
346+
def doTask[R](name: String)(task: SimpleTask[R]): Future[R] = {
352347
val jfxTask = new javafx.concurrent.Task[R] {
353348
override def call(): R = {
354349
task.call()

0 commit comments

Comments
 (0)