From b6f20e6837ac50d25fd84c1985e8c92f669f27a6 Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Tue, 12 Dec 2023 11:07:05 +0800 Subject: [PATCH] 1 --- async_task.go | 76 +++++++++++++++++++++++++++++++++++++++++++++++++++ task.go | 36 ------------------------ 2 files changed, 76 insertions(+), 36 deletions(-) create mode 100644 async_task.go delete mode 100644 task.go diff --git a/async_task.go b/async_task.go new file mode 100644 index 0000000..a0bc5fe --- /dev/null +++ b/async_task.go @@ -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() +} diff --git a/task.go b/task.go deleted file mode 100644 index 75789cd..0000000 --- a/task.go +++ /dev/null @@ -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) -}