f5/async_task.go
aozhiwei 01ba77f7ae 1
2024-04-07 13:44:56 +08:00

108 lines
1.8 KiB
Go

package f5
import (
"q5"
)
type taskLock struct {
entry q5.ListHead
}
type AsyncTask struct {
status int32
cb func(*AsyncTask)
succCb func(*AsyncTask)
failCb func(*AsyncTask)
execTimes int64
lockKeys [][]string
}
type LockAsyncTask = AsyncTask
func (this *AsyncTask) init(cb func(*AsyncTask)) *AsyncTask {
this.cb = cb
return this
}
func (this *AsyncTask) GetExecTimes() int64 {
return this.execTimes
}
func (this *AsyncTask) IsRunning() bool {
return this.status == 0
}
func (this *AsyncTask) IsSucc() bool {
return this.status > 0
}
func (this *AsyncTask) IsFail() bool {
return this.status < 0
}
func (this *AsyncTask) SetSucc() {
if !this.IsRunning() {
panic("task is not runing")
}
this.status = 1
if this.succCb != nil {
this.succCb(this)
}
}
func (this *AsyncTask) SetFail() {
if !this.IsRunning() {
panic("task is not runing")
}
this.status = -1
if this.failCb != nil {
this.failCb(this)
}
}
func (this *AsyncTask) Continue() *AsyncTask {
if !this.IsRunning() {
panic("task is not runing")
}
GetApp().RegisterMainThreadCb(
func () {
this.cb(this)
this.execTimes += 1
})
return this
}
func (this *AsyncTask) do() *AsyncTask {
if !this.IsRunning() {
panic("task is not runing")
}
GetApp().RegisterMainThreadCb(
func () {
this.cb(this)
this.execTimes += 1
})
return this
}
func (this *AsyncTask) OnSucc(cb func(*AsyncTask)) *AsyncTask {
this.succCb = cb
return this
}
func (this *AsyncTask) OnFail(cb func(*AsyncTask)) *AsyncTask {
this.failCb = cb
return this
}
func NewAsyncTask(cb func(*AsyncTask)) *AsyncTask {
p := new(AsyncTask)
p.lockKeys = [][]string{}
return p.init(cb).Continue()
}
func NewLockAsyncTask(keys [][]string, cb func(*LockAsyncTask)) *LockAsyncTask {
p := new(AsyncTask)
p.lockKeys = [][]string{}
return p.init(cb).Continue()
}