github.com/isyscore/isc-gobase@v1.5.3-0.20231218061332-cbc7451899e9/time/timer.go (about)

     1  package time
     2  
     3  import (
     4  	t0 "time"
     5  )
     6  
     7  type OnTimerFunc func(t *Timer)
     8  
     9  type Timer struct {
    10  	Interval      int64
    11  	IsStopped     bool
    12  	OnTimer       OnTimerFunc // 这个函数将在[协程]内异步调用
    13  	OnBeforeTimer OnTimerFunc // 这个函数在[主线程]内同步调用
    14  	OnAfterTimer  OnTimerFunc // 这个函数在[主线程]内同步调用
    15  	ticker        *t0.Ticker
    16  	shiftStatus   bool
    17  }
    18  
    19  func NewTimer() *Timer {
    20  	intv := int64(float64(t0.Second) * 1.0)
    21  	return &Timer{
    22  		Interval:    intv,
    23  		IsStopped:   true,
    24  		ticker:      nil,
    25  		shiftStatus: false,
    26  	}
    27  }
    28  
    29  func NewTimerWithInterval(seconds float64) *Timer {
    30  	intv := int64(float64(t0.Second) * seconds)
    31  	return &Timer{
    32  		Interval:    intv,
    33  		IsStopped:   true,
    34  		ticker:      nil,
    35  		shiftStatus: false,
    36  	}
    37  }
    38  
    39  func NewTimerWithFire(seconds float64, onTimer OnTimerFunc) *Timer {
    40  	intv := int64(float64(t0.Second) * seconds)
    41  	return &Timer{
    42  		Interval:    intv,
    43  		IsStopped:   true,
    44  		OnTimer:     onTimer,
    45  		ticker:      nil,
    46  		shiftStatus: false,
    47  	}
    48  }
    49  
    50  func (t *Timer) Start() {
    51  	t.IsStopped = false
    52  	if !t.shiftStatus {
    53  		if t.OnBeforeTimer != nil {
    54  			t.OnBeforeTimer(t)
    55  		}
    56  	}
    57  
    58  	t.ticker = t0.NewTicker(t0.Duration(t.Interval))
    59  	go func(tk *t0.Ticker) {
    60  		for !t.IsStopped {
    61  			_, ok := <-tk.C
    62  			if !ok {
    63  				break
    64  			}
    65  			if t.OnTimer != nil {
    66  				t.OnTimer(t)
    67  			}
    68  		}
    69  	}(t.ticker)
    70  }
    71  
    72  func (t *Timer) Stop() {
    73  	t.IsStopped = true
    74  	if t.ticker != nil {
    75  		t.ticker.Stop()
    76  		t.ticker = nil
    77  	}
    78  	if !t.shiftStatus {
    79  		if t.OnAfterTimer != nil {
    80  			t.OnAfterTimer(t)
    81  		}
    82  	}
    83  }
    84  
    85  func (t *Timer) SetInterval(seconds float64) {
    86  	shouldStart := false
    87  	t.shiftStatus = true
    88  	if !t.IsStopped {
    89  		shouldStart = true
    90  		t.Stop()
    91  	}
    92  	intv := int64(float64(t0.Second) * seconds)
    93  	t.Interval = intv
    94  	if shouldStart {
    95  		t.Start()
    96  	}
    97  	t.shiftStatus = false
    98  }
    99  
   100  func (t *Timer) SetOnTimer(event OnTimerFunc) {
   101  	shouldStart := false
   102  	t.shiftStatus = true
   103  	if !t.IsStopped {
   104  		shouldStart = true
   105  		t.Stop()
   106  	}
   107  	t.OnTimer = event
   108  	if shouldStart {
   109  		t.Start()
   110  	}
   111  	t.shiftStatus = false
   112  }
   113  
   114  func (t *Timer) SetOnBefore(event OnTimerFunc) {
   115  	t.OnBeforeTimer = event
   116  }
   117  
   118  func (t *Timer) SetOnAfter(event OnTimerFunc) {
   119  	t.OnAfterTimer = event
   120  }