From e5917e6c3ed2572c4b3100bd14f080d56fe44a68 Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Tue, 9 Apr 2024 14:39:23 +0800 Subject: [PATCH] 1 --- async_task.go | 39 ++++++++++++++++++++++++++++++++------- 1 file changed, 32 insertions(+), 7 deletions(-) diff --git a/async_task.go b/async_task.go index b60699a..3662970 100644 --- a/async_task.go +++ b/async_task.go @@ -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) {