@@ 148-165 (lines=18) @@ | ||
145 | /** |
|
146 | * @inheritdoc |
|
147 | */ |
|
148 | public function addTimer($interval, callable $callback, int $priority = 0) : Timer |
|
149 | { |
|
150 | $timer = new Timer($this, $interval, $callback, false, $priority); |
|
151 | ||
152 | $callback = function ($evTimer) use ($timer) { |
|
153 | call_user_func($timer->getCallback(), $timer, $evTimer); |
|
154 | ||
155 | if ($this->isTimerActive($timer)) { |
|
156 | $this->cancelTimer($timer); |
|
157 | } |
|
158 | }; |
|
159 | ||
160 | $event = $this->loop->timer($interval, 0, $callback, null, $priority); |
|
161 | ||
162 | $this->timers->attach($timer, $event); |
|
163 | ||
164 | return $timer; |
|
165 | } |
|
166 | ||
167 | /** |
|
168 | * @inheritdoc |
|
@@ 170-190 (lines=21) @@ | ||
167 | /** |
|
168 | * @inheritdoc |
|
169 | */ |
|
170 | public function addPeriodicTimer($interval, callable $callback, int $priority = 0) : Timer |
|
171 | { |
|
172 | $timer = new Timer($this, $interval, $callback, true, $priority); |
|
173 | ||
174 | $internalCallback = function ($evTimer) use ($timer) { |
|
175 | call_user_func($timer->getCallback(), $timer, $evTimer); |
|
176 | }; |
|
177 | ||
178 | $event = $this->loop->periodic( |
|
179 | $timer->getInterval(), |
|
180 | $timer->getInterval(), |
|
181 | null, |
|
182 | $internalCallback, |
|
183 | null, |
|
184 | $priority |
|
185 | ); |
|
186 | ||
187 | $this->timers->attach($timer, $event); |
|
188 | ||
189 | return $timer; |
|
190 | } |
|
191 | ||
192 | /** |
|
193 | * @inheritdoc |