public function start() { echo "Start Listen...\n"; while ($this->state != self::BOT_EXIT) { $defer = new Deferred(function () { }); $this->state = self::BOT_WAIT_FOR_MESSAGES; try { $request = $this->requestApi('getUpdates', ['timeout' => $this->storage['lpTimeout'], 'offset' => $this->lastMessageId], ['timeout' => -1]); if ($request->status_code != 200) { $defer->fail(new \ErrorException('ServerError')); } $data = json_decode($request->body, true); $result = $data['result']; if (!empty($result)) { $message = $result[count($result) - 1]; $this->lastMessageId = $message['update_id'] + 1; $defer->done($message['message']); } } catch (\ErrorException $e) { echo $e->getMessage(); } $defer->onDone(function ($data) { $this->dispatchMessage($data); }); $defer->onFail(function () { $this->state = self::BOT_EXIT; }); } }
/** * @expectedException \ION\InvalidUsageException * @expectedExceptionMessage Deferred has been finished * @memcheck */ public function testRejectAfterDone() { $defer = new Deferred(function ($reason) { }); $defer->then(function ($data) { }); $defer->done("some result"); $defer->cancel("bad reason"); }