This commit is contained in:
aozhiwei 2024-04-09 14:39:23 +08:00
parent 0e32fe0327
commit e5917e6c3e

View File

@ -14,6 +14,8 @@ type AsyncTask struct {
cb func(*AsyncTask)
succCb func(*AsyncTask)
failCb func(*AsyncTask)
exitCb func(*AsyncTask)
preExecTimes int64
execTimes int64
lockKeys map[string]*taskLock
}
@ -46,9 +48,13 @@ func (this *AsyncTask) SetSucc() {
panic("task is not runing")
}
this.status = 1
this.ClearLocks()
if this.succCb != nil {
this.succCb(this)
}
if this.exitCb != nil {
this.exitCb(this)
}
}
func (this *AsyncTask) SetFail() {
@ -56,15 +62,22 @@ func (this *AsyncTask) SetFail() {
panic("task is not runing")
}
this.status = -1
this.ClearLocks()
if this.failCb != nil {
this.failCb(this)
}
if this.exitCb != nil {
this.exitCb(this)
}
}
func (this *AsyncTask) Continue() *AsyncTask {
if !this.IsRunning() {
panic("task is not runing")
}
if len(this.lockKeys) > 0 {
panic("lockTask not support continue")
}
GetApp().RegisterMainThreadCb(
func () {
this.cb(this)
@ -78,27 +91,39 @@ func (this *AsyncTask) checkDo() *AsyncTask {
panic("task is not runing")
}
if this.allIsReady() {
GetApp().RegisterMainThreadCb(
func () {
this.cb(this)
this.execTimes += 1
})
if this.preExecTimes <= 0 {
this.preExecTimes++
if this.preExecTimes > 1 {
panic("locktask preExectimes > 0")
}
GetApp().RegisterMainThreadCb(
func () {
this.cb(this)
if this.execTimes > 0 {
panic("locktask only run once")
}
this.execTimes += 1
})
}
}
return this
}
func (this *AsyncTask) OnSucc(cb func(*AsyncTask)) *AsyncTask {
this.ClearLocks()
this.succCb = cb
return this
}
func (this *AsyncTask) OnFail(cb func(*AsyncTask)) *AsyncTask {
this.ClearLocks()
this.failCb = cb
return this
}
func (this *AsyncTask) OnExit(cb func(*AsyncTask)) *AsyncTask {
this.exitCb = cb
return this
}
func (this *AsyncTask) allIsReady() bool {
var allReady = true
for _, lock := range(this.lockKeys) {