gitee.com/sy_183/go-common@v1.0.5-0.20231205030221-958cfe129b47/lifecycle/task/task.go (about) 1 package task 2 3 type Task interface { 4 Do(interrupter chan struct{}) (interrupted bool) 5 } 6 7 type nopTask struct{} 8 9 func Nop() Task { 10 return nopTask{} 11 } 12 13 func (t nopTask) Do(interrupter chan struct{}) (interrupted bool) { return false } 14 15 type interruptedTaskFunc func(interrupter chan struct{}) (interrupted bool) 16 17 func Interrupted(fn func(interrupter chan struct{}) (interrupted bool)) Task { 18 if fn == nil { 19 return Nop() 20 } 21 return interruptedTaskFunc(fn) 22 } 23 24 func (f interruptedTaskFunc) Do(interrupter chan struct{}) (interrupted bool) { return f(interrupter) } 25 26 type uninterruptedTaskFunc func() 27 28 func Func(fn func()) Task { 29 if fn == nil { 30 return Nop() 31 } 32 return uninterruptedTaskFunc(fn) 33 } 34 35 func (f uninterruptedTaskFunc) Do(interrupter chan struct{}) (interrupted bool) { 36 f() 37 return false 38 }