Skip to content

Commit 0ce5c30

Browse files
authored
Task cleanup (#115)
* remove StartableCoroTask * remove CoroTaskWith * remove unnecessary task dispatch
1 parent c4f449b commit 0ce5c30

File tree

9 files changed

+70
-115
lines changed

9 files changed

+70
-115
lines changed

.vscode/launch.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"name": "HashLink (launch)",
9+
"request": "launch",
10+
"type": "hl",
11+
"cwd": "${workspaceFolder}/tests",
12+
}
13+
]
14+
}

src/hxcoro/Coro.hx

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,7 @@ class Coro {
5757
@:coroutine static public function scope<T>(lambda:NodeLambda<T>):T {
5858
return suspend(cont -> {
5959
final context = cont.context;
60-
final scope = new CoroTask(context, CoroTask.CoroScopeStrategy);
61-
scope.runNodeLambda(lambda);
60+
final scope = new CoroTaskWithLambda(context, lambda, CoroTask.CoroScopeStrategy);
6261
scope.awaitContinuation(cont);
6362
});
6463
}
@@ -72,8 +71,7 @@ class Coro {
7271
@:coroutine static public function supervisor<T>(lambda:NodeLambda<T>):T {
7372
return suspend(cont -> {
7473
final context = cont.context;
75-
final scope = new CoroTask(context, CoroTask.CoroSupervisorStrategy);
76-
scope.runNodeLambda(lambda);
74+
final scope = new CoroTaskWithLambda(context, lambda, CoroTask.CoroSupervisorStrategy);
7775
scope.awaitContinuation(cont);
7876
});
7977
}
@@ -97,12 +95,11 @@ class Coro {
9795
return suspend(cont -> {
9896

9997
final context = cont.context;
100-
final scope = new CoroTask(context, CoroTask.CoroScopeStrategy);
98+
final scope = new CoroTaskWithLambda(context, lambda, CoroTask.CoroScopeStrategy);
10199
final handle = context.scheduleFunction(ms, () -> {
102100
scope.cancel(new TimeoutException());
103101
});
104102

105-
scope.runNodeLambda(lambda);
106103
scope.awaitContinuation(new TimeoutContinuation(cont, handle));
107104
});
108105
}

src/hxcoro/run/ContextRun.hx

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import hxcoro.schedulers.ILoop;
66
import hxcoro.task.CoroTask;
77
import hxcoro.task.ICoroTask;
88
import hxcoro.task.NodeLambda;
9-
import hxcoro.task.StartableCoroTask;
109

1110
/**
1211
This class provides static extensions for `Context` to work with `Task` instances.
@@ -41,7 +40,7 @@ class ContextRun {
4140
if (dispatcher == null) {
4241
throw 'Cannot create a task without a Dispatcher element';
4342
}
44-
return new StartableCoroTask(context, lambda, CoroTask.CoroScopeStrategy);
43+
return new CoroTaskWithLambda(context, lambda, CoroTask.CoroScopeStrategy, Created);
4544
}
4645

4746
/**
@@ -56,9 +55,7 @@ class ContextRun {
5655
if (dispatcher == null) {
5756
throw 'Cannot launch a task without a Dispatcher element';
5857
}
59-
final task = new CoroTask(context, CoroTask.CoroScopeStrategy);
60-
task.runNodeLambda(lambda);
61-
return task;
58+
return new CoroTaskWithLambda(context, lambda, CoroTask.CoroScopeStrategy);
6259
}
6360

6461
/**

src/hxcoro/run/LoopRun.hx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ class LoopRun {
2323
events in such a way that the loop processes them.
2424
**/
2525
static public function runTask<T>(loop:ILoop, context:Context, lambda:NodeLambda<T>):ICoroTask<T> {
26-
final task = new CoroTask(context, CoroTask.CoroScopeStrategy);
27-
task.runNodeLambda(lambda);
26+
final task = new CoroTaskWithLambda(context, lambda, CoroTask.CoroScopeStrategy);
2827
awaitTaskCompletion(loop, task);
2928
return task;
3029
}

src/hxcoro/task/CoroBaseTask.hx

Lines changed: 15 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,22 @@
11
package hxcoro.task;
22

3-
import hxcoro.continuations.FunctionContinuation;
4-
import hxcoro.concurrent.AtomicState;
5-
import hxcoro.concurrent.AtomicObject;
6-
import hxcoro.concurrent.BackOff;
7-
import hxcoro.elements.NonCancellable;
8-
import hxcoro.task.CoroTask;
9-
import hxcoro.task.node.INodeStrategy;
10-
import hxcoro.task.ICoroTask;
11-
import hxcoro.task.AbstractTask;
12-
import hxcoro.task.ICoroNode;
133
import haxe.Exception;
14-
import haxe.exceptions.CancellationException;
154
import haxe.coro.IContinuation;
5+
import haxe.coro.cancellation.CancellationToken;
166
import haxe.coro.context.Context;
17-
import haxe.coro.context.Key;
187
import haxe.coro.context.IElement;
19-
import haxe.coro.dispatchers.Dispatcher;
20-
import haxe.coro.cancellation.CancellationToken;
8+
import haxe.coro.context.Key;
9+
import haxe.exceptions.CancellationException;
10+
import hxcoro.concurrent.AtomicObject;
2111
import hxcoro.concurrent.ThreadSafeCallbacks;
12+
import hxcoro.continuations.FunctionContinuation;
13+
import hxcoro.elements.NonCancellable;
14+
import hxcoro.task.AbstractTask;
15+
import hxcoro.task.CoroTask;
16+
import hxcoro.task.ICoroNode;
17+
import hxcoro.task.ICoroTask;
18+
import hxcoro.task.node.INodeStrategy;
2219

23-
private class CoroTaskWith<T> implements ICoroNodeWith {
24-
public var context(get, null):Context;
25-
26-
final task:CoroBaseTask<T>;
27-
28-
public function new(context:Context, task:CoroBaseTask<T>) {
29-
this.context = context;
30-
this.task = task;
31-
}
32-
33-
inline function get_context() {
34-
return context;
35-
}
36-
37-
public function async<T>(lambda:NodeLambda<T>):ICoroTask<T> {
38-
final child = new CoroTaskWithLambda(context, lambda, CoroTask.CoroChildStrategy);
39-
context.get(Dispatcher).dispatch(child);
40-
return child;
41-
}
42-
43-
public function lazy<T>(lambda:NodeLambda<T>):IStartableCoroTask<T> {
44-
return new StartableCoroTask(context, lambda, CoroTask.CoroChildStrategy);
45-
}
46-
47-
public function with(...elements:IElement<Any>) {
48-
return task.with(...elements);
49-
}
50-
51-
public function without(...keys:Key<Any>) {
52-
return task.without(...keys);
53-
}
54-
}
5520
class TaskContinuationManager extends ThreadSafeCallbacks<IContinuation<Any>, IContinuation<Any>, IContinuation<Any>> {
5621
public function new(task:CoroBaseTask<Any>) {
5722
super(handle -> handle.resume(task.get(), task.getError()));
@@ -110,30 +75,28 @@ abstract class CoroBaseTask<T> extends AbstractTask implements ICoroNode impleme
11075
method is called. This occurrs automatically once this task has finished execution.
11176
**/
11277
public function lazy<T>(lambda:NodeLambda<T>):IStartableCoroTask<T> {
113-
return new StartableCoroTask(context, lambda, CoroTask.CoroChildStrategy);
78+
return new CoroTaskWithLambda(context, lambda, CoroTask.CoroChildStrategy, Created);
11479
}
11580

11681
/**
11782
Creates a child task to execute `lambda` and starts it automatically.
11883
**/
11984
public function async<T>(lambda:NodeLambda<T>):ICoroTask<T> {
120-
final child = new CoroTaskWithLambda<T>(context, lambda, CoroTask.CoroChildStrategy);
121-
context.get(Dispatcher).dispatch(child);
122-
return child;
85+
return new CoroTaskWithLambda<T>(context, lambda, CoroTask.CoroChildStrategy);
12386
}
12487

12588
/**
12689
Returns a copy of this tasks' `Context` with `elements` added, which can be used to start child tasks.
12790
**/
12891
public function with(...elements:IElement<Any>) {
129-
return new CoroTaskWith(context.clone().with(...elements), this);
92+
return context.with(...elements);
13093
}
13194

13295
/**
13396
Returns a copy of this tasks' `Context` where all `keys` are unset, which can be used to start child tasks.
13497
**/
13598
public function without(...keys:Key<Any>) {
136-
return new CoroTaskWith(context.clone().without(...keys), this);
99+
return context.without(...keys);
137100
}
138101

139102
/**

src/hxcoro/task/CoroTask.hx

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
package hxcoro.task;
22

3+
import haxe.Exception;
4+
import haxe.coro.IContinuation;
5+
import haxe.coro.context.Context;
6+
import hxcoro.task.AbstractTask;
7+
import hxcoro.task.ICoroTask;
38
import hxcoro.task.node.CoroChildStrategy;
49
import hxcoro.task.node.CoroScopeStrategy;
510
import hxcoro.task.node.CoroSupervisorStrategy;
611
import hxcoro.task.node.INodeStrategy;
7-
import hxcoro.task.AbstractTask;
8-
import haxe.coro.IContinuation;
9-
import haxe.coro.context.Context;
10-
import haxe.coro.dispatchers.IDispatchObject;
11-
import haxe.Exception;
1212

1313
@:using(CoroTask.ResumeStatusTools)
1414
private enum abstract ResumeStatus(Int) to Int {
@@ -38,19 +38,6 @@ class CoroTask<T> extends CoroBaseTask<T> implements IContinuation<T> {
3838

3939
public function doStart() {}
4040

41-
public function runNodeLambda(lambda:NodeLambda<T>) {
42-
start();
43-
final result = lambda(this, this);
44-
@:nullSafety(Off) switch result.state {
45-
case Pending:
46-
return;
47-
case Returned:
48-
this.succeedSync(result.result);
49-
case Thrown:
50-
this.failSync(result.error);
51-
}
52-
}
53-
5441
/**
5542
Resumes the task with the provided `result` and `error`.
5643
**/
@@ -76,18 +63,30 @@ class CoroTask<T> extends CoroBaseTask<T> implements IContinuation<T> {
7663
#end
7764
}
7865

79-
class CoroTaskWithLambda<T> extends CoroTask<T> implements IDispatchObject {
66+
class CoroTaskWithLambda<T> extends CoroTask<T> implements IStartableCoroTask<T> {
8067
final lambda:NodeLambda<T>;
8168

8269
/**
8370
Creates a new task using the provided `context` in order to execute `lambda`.
8471
**/
85-
public function new(context:Context, lambda:NodeLambda<T>, nodeStrategy:INodeStrategy) {
86-
super(context, nodeStrategy);
72+
public function new(context:Context, lambda:NodeLambda<T>, nodeStrategy:INodeStrategy, initialState:TaskState = Running) {
8773
this.lambda = lambda;
74+
super(context, nodeStrategy, initialState);
8875
}
8976

90-
public function onDispatch() {
91-
runNodeLambda(lambda);
77+
/**
78+
Starts executing this task's `lambda`. Has no effect if the task is already active or has completed.
79+
**/
80+
override public function doStart() {
81+
super.doStart();
82+
final result = lambda(this, this);
83+
@:nullSafety(Off) switch result.state {
84+
case Pending:
85+
return;
86+
case Returned:
87+
this.succeedSync(result.result);
88+
case Thrown:
89+
this.failSync(result.error);
90+
}
9291
}
9392
}

src/hxcoro/task/ICoroNode.hx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ interface ICoroNodeWith {
1010
var context(get, null):Context;
1111
function async<T>(lambda:NodeLambda<T>):ICoroTask<T>;
1212
function lazy<T>(lambda:NodeLambda<T>):IStartableCoroTask<T>;
13-
function with(...elements:IElement<Any>):ICoroNodeWith;
14-
function without(...keys:Key<Any>):ICoroNodeWith;
13+
function with(...elements:IElement<Any>):Context;
14+
function without(...keys:Key<Any>):Context;
1515
}
1616

1717
interface ICoroNode extends ICoroNodeWith {

src/hxcoro/task/StartableCoroTask.hx

Lines changed: 0 additions & 25 deletions
This file was deleted.

src/hxcoro/util/Convenience.hx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ import haxe.coro.dispatchers.Dispatcher;
1010
import haxe.coro.dispatchers.IDispatchObject;
1111
import haxe.exceptions.CancellationException;
1212
import hxcoro.continuations.FunctionContinuation;
13+
import hxcoro.task.CoroTask;
14+
import hxcoro.task.ICoroTask;
15+
import hxcoro.task.NodeLambda;
1316

1417
private class FunctionDispatchObject implements IDispatchObject {
1518
final func : ()->Void;
@@ -125,6 +128,14 @@ class ContextConvenience {
125128
static public inline function scheduleFunction(context:Context, ms:Int64, func:() -> Void) {
126129
return context.get(Dispatcher).scheduler.schedule(ms, new FunctionContinuation(context, (_, _) -> func()));
127130
}
131+
132+
static public function async<T>(context:Context, lambda:NodeLambda<T>):ICoroTask<T> {
133+
return new CoroTaskWithLambda<T>(context, lambda, CoroTask.CoroChildStrategy);
134+
}
135+
136+
static public function lazy<T>(context:Context, lambda:NodeLambda<T>):IStartableCoroTask<T> {
137+
return new CoroTaskWithLambda(context, lambda, CoroTask.CoroChildStrategy, Created);
138+
}
128139
}
129140

130141
class OtherConvenience {

0 commit comments

Comments
 (0)