-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwork.go
41 lines (34 loc) · 1.03 KB
/
work.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package tree
import "context"
type Work struct {
ctx context.Context
owner *Task
}
// Spawn returns a child task running with the given work
func (w *Work) Spawn(handler WorkHandler, opts ...Option) *Task {
options := w.owner.childOptions()
options.Apply(WithWork(handler))
options.Apply(opts...)
newTask := NewTask(WithOptions(options))
w.Attach(newTask)
newTask.Start(w.ctx)
return newTask
}
// Attach adds a task as a child of the executing task
func (w *Work) Attach(newTask *Task) *Task {
w.owner.attach(w.ctx, newTask)
return newTask
}
// WaitChild blocks until a child terminates
func (w *Work) WaitChild() (*Task, error) {
return w.owner.awaitAnySub(w.ctx)
}
// TerminateEarly requests early termination of the current work causing the context of the current work to cancelled.
// Regularly to terminate the current work early, just use a regular go return from the work handler.
func (w *Work) TerminateEarly() {
w.owner.Terminate()
}
// Task returns the task executing the work
func (w *Work) Task() *Task {
return w.owner
}