1
This commit is contained in:
parent
003e5247c7
commit
b6f20e6837
76
async_task.go
Normal file
76
async_task.go
Normal file
@ -0,0 +1,76 @@
|
|||||||
|
package f5
|
||||||
|
|
||||||
|
type AsyncTask struct {
|
||||||
|
status int32
|
||||||
|
cb func(*AsyncTask)
|
||||||
|
succCb func(*AsyncTask)
|
||||||
|
failCb func(*AsyncTask)
|
||||||
|
execTimes int64
|
||||||
|
}
|
||||||
|
|
||||||
|
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) 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 {
|
||||||
|
return new(AsyncTask).init(cb).Continue()
|
||||||
|
}
|
36
task.go
36
task.go
@ -1,36 +0,0 @@
|
|||||||
package f5
|
|
||||||
|
|
||||||
type Task struct {
|
|
||||||
status int32
|
|
||||||
}
|
|
||||||
|
|
||||||
func (this *Task) IsRunning() bool {
|
|
||||||
return this.status == 0
|
|
||||||
}
|
|
||||||
|
|
||||||
func (this *Task) IsSucc() bool {
|
|
||||||
return this.status > 0
|
|
||||||
}
|
|
||||||
|
|
||||||
func (this *Task) IsFail() bool {
|
|
||||||
return this.status < 0
|
|
||||||
}
|
|
||||||
|
|
||||||
func (this *Task) SetSucc() {
|
|
||||||
this.status = 1
|
|
||||||
}
|
|
||||||
|
|
||||||
func (this *Task) SetFail() {
|
|
||||||
this.status = -1
|
|
||||||
}
|
|
||||||
|
|
||||||
func (this *Task) run(cb func(*Task)) *Task {
|
|
||||||
for this.IsRunning() {
|
|
||||||
cb(this)
|
|
||||||
}
|
|
||||||
return this
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewTask(cb func(*Task)) *Task {
|
|
||||||
return new(Task).run(cb)
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user