88
99// 轻量级的持续Tick循环
1010type Loop struct {
11- Context interface {}
12- Duration time.Duration
13- userCallback func (* Loop )
11+ Context interface {}
12+ Duration time.Duration
13+ notifyCallback func (* Loop )
1414
1515 running int64
1616
@@ -71,12 +71,26 @@ func (self *Loop) Stop() {
7171 self .setRunning (false )
7272}
7373
74+ func (self * Loop ) Resume () {
75+
76+ self .setRunning (true )
77+ }
78+
7479// 马上调用一次用户回调
7580func (self * Loop ) Notify () * Loop {
76- self .userCallback (self )
81+ self .notifyCallback (self )
82+ return self
83+ }
84+
85+ func (self * Loop ) SetNotifyFunc (notifyCallback func (* Loop )) * Loop {
86+ self .notifyCallback = notifyCallback
7787 return self
7888}
7989
90+ func (self * Loop ) NotifyFunc () func (* Loop ) {
91+ return self .notifyCallback
92+ }
93+
8094func tick (ctx interface {}, nextLoop bool ) {
8195
8296 loop := ctx .(* Loop )
@@ -92,13 +106,13 @@ func tick(ctx interface{}, nextLoop bool) {
92106
93107// 执行一个循环, 持续调用callback, 周期是duration
94108// context: 将context上下文传递到带有context指针的函数回调中
95- func NewLoop (q cellnet.EventQueue , duration time.Duration , callback func (* Loop ), context interface {}) * Loop {
109+ func NewLoop (q cellnet.EventQueue , duration time.Duration , notifyCallback func (* Loop ), context interface {}) * Loop {
96110
97111 self := & Loop {
98- Context : context ,
99- Duration : duration ,
100- userCallback : callback ,
101- Queue : q ,
112+ Context : context ,
113+ Duration : duration ,
114+ notifyCallback : notifyCallback ,
115+ Queue : q ,
102116 }
103117
104118 return self
0 commit comments