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