Skip to content

Commit ac21599

Browse files
committed
feat(task): accept func() or func(t *Internal)
1 parent c7c9bd2 commit ac21599

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

task/task.go

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,8 @@ func (i *Internal) CancelableCtx() context.Context {
171171
}
172172

173173
// New creates new instances of Task.
174-
func New(fn func(t *Internal)) *Task {
174+
// Accepts `func()` or `func(t *Internal)`
175+
func New(fn interface{}) *Task {
175176
// Spin up some channels
176177
done := make(chan struct{}, 1)
177178
stopper := make(chan struct{}, 1)
@@ -203,12 +204,17 @@ func New(fn func(t *Internal)) *Task {
203204
})
204205

205206
// Execute the task
206-
fn(&Internal{
207-
Resolver: tiResolver,
208-
Rejector: tiRejector,
209-
Stopper: t.Stopper,
210-
done: &done,
211-
})
207+
switch v := fn.(type) {
208+
case func():
209+
v()
210+
case func(t *Internal):
211+
v(&Internal{
212+
Resolver: tiResolver,
213+
Rejector: tiRejector,
214+
Stopper: t.Stopper,
215+
done: &done,
216+
})
217+
}
212218

213219
// Read the result in a non blocking manner. Keep in mind not every task
214220
// will actually resolve or reject anything, the simple fact that it is

0 commit comments

Comments
 (0)