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; }); } }
/** * @memcheck */ public function testDeferredThenDeferred() { $d1 = new Deferred(function () { $this->data["d1.cancel"] = true; }); $d2 = new Deferred(function () { $this->data["d2.cancel"] = true; }); $d2->then(function ($result) { $this->data["result"] = $this->describe($result); }, function ($result) { $this->data["error"] = $this->describe($result); }); $d1->then($d2); $d2->done("iddqd"); $this->assertSame(['result' => "iddqd"], $this->data); }
/** * @expectedException \ION\InvalidUsageException * @expectedExceptionMessage Deferred has been finished * @memcheck */ public function testRejectAfterFail() { $defer = new Deferred(function () { }); $defer->then(function () { }); $defer->fail(new \RuntimeException("errorka")); $defer->cancel("bad reason"); }