81 lines
1.3 KiB
Go
81 lines
1.3 KiB
Go
package f5
|
|
|
|
import (
|
|
)
|
|
|
|
type AsyncTask struct {
|
|
status int32
|
|
cb func(*AsyncTask)
|
|
succCb func(*AsyncTask)
|
|
failCb func(*AsyncTask)
|
|
exitCb func(*AsyncTask)
|
|
execTimes int64
|
|
}
|
|
|
|
func (this *AsyncTask) init(cb func(*AsyncTask)) *AsyncTask {
|
|
this.cb = cb
|
|
return this
|
|
}
|
|
|
|
func (this *AsyncTask) isRunning() 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)
|
|
}
|
|
if this.exitCb != nil {
|
|
this.exitCb(this)
|
|
}
|
|
}
|
|
|
|
func (this *AsyncTask) SetFail() {
|
|
if !this.isRunning() {
|
|
panic("task is not runing")
|
|
}
|
|
this.status = -1
|
|
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")
|
|
}
|
|
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 (this *AsyncTask) OnExit(cb func(*AsyncTask)) *AsyncTask {
|
|
this.exitCb = cb
|
|
return this
|
|
}
|
|
|
|
func NewAsyncTask(cb func(*AsyncTask)) *AsyncTask {
|
|
p := new(AsyncTask)
|
|
return p.init(cb).Continue()
|
|
}
|