Skip to content

Commit d5b9c54

Browse files
committed
Merge branch 'release/0.6.4'
2 parents 4cc39f6 + 9b4d7ab commit d5b9c54

File tree

12 files changed

+209
-65
lines changed

12 files changed

+209
-65
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11

2+
### [0.6.4] - 2016-10-03
3+
4+
* refactoring
5+
26
### [0.6.3] - 2016-10-02
37

48
* cs fixes

src/Tidal/WampWatch/Adapter/React/DeferredAdapter.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
namespace Tidal\WampWatch\Adapter\React;
1313

1414
use Tidal\WampWatch\Async\DeferredInterface;
15-
use Tidal\WampWatch\Async\PromiseInterface;
1615
use React\Promise\Deferred;
1716

1817
class DeferredAdapter implements DeferredInterface
@@ -70,7 +69,7 @@ public function getPromiseClass()
7069
}
7170

7271
/**
73-
* @return PromiseInterface
72+
* @return PromiseAdapter
7473
*/
7574
public function promise()
7675
{
@@ -102,7 +101,7 @@ public function notify($update = null)
102101
}
103102

104103
/**
105-
* @return PromiseInterface
104+
* @return PromiseAdapter
106105
*/
107106
private function createPromise()
108107
{

src/Tidal/WampWatch/Adapter/React/PromiseAdapter.php

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,19 @@ public function getAdaptee()
5656
*/
5757
public function then(callable $onFulfilled = null, callable $onRejected = null, callable $onProgress = null)
5858
{
59-
$this->adaptee->then($onFulfilled, $onRejected, $onProgress);
59+
$this->adaptee->then(function () use ($onFulfilled) {
60+
if ($onFulfilled !== null) {
61+
return call_user_func_array($onFulfilled, func_get_args());
62+
}
63+
}, function () use ($onRejected) {
64+
if ($onRejected !== null) {
65+
return call_user_func_array($onRejected, func_get_args());
66+
}
67+
}, function () use ($onProgress) {
68+
if ($onProgress !== null) {
69+
return call_user_func_array($onProgress, func_get_args());
70+
}
71+
});
6072

6173
return $this;
6274
}
@@ -70,7 +82,19 @@ public function then(callable $onFulfilled = null, callable $onRejected = null,
7082
*/
7183
public function done(callable $onFulfilled = null, callable $onRejected = null, callable $onProgress = null)
7284
{
73-
$this->adaptee->done($onFulfilled, $onRejected, $onProgress);
85+
$this->adaptee->done(function () use ($onFulfilled) {
86+
if ($onFulfilled !== null) {
87+
return call_user_func_array($onFulfilled, func_get_args());
88+
}
89+
}, function () use ($onRejected) {
90+
if ($onRejected !== null) {
91+
return call_user_func_array($onRejected, func_get_args());
92+
}
93+
}, function () use ($onProgress) {
94+
if ($onProgress !== null) {
95+
return call_user_func_array($onProgress, func_get_args());
96+
}
97+
});
7498

7599
return $this;
76100
}
@@ -82,7 +106,9 @@ public function done(callable $onFulfilled = null, callable $onRejected = null,
82106
*/
83107
public function otherwise(callable $onRejected)
84108
{
85-
$this->adaptee->otherwise($onRejected);
109+
$this->adaptee->otherwise(function () use ($onRejected) {
110+
return call_user_func_array($onRejected, func_get_args());
111+
});
86112

87113
return $this;
88114
}
@@ -94,7 +120,9 @@ public function otherwise(callable $onRejected)
94120
*/
95121
public function always(callable $onAlways)
96122
{
97-
$this->adaptee->always($onAlways);
123+
$this->adaptee->always(function () use ($onAlways) {
124+
return call_user_func_array($onAlways, func_get_args());
125+
});
98126

99127
return $this;
100128
}
@@ -106,7 +134,9 @@ public function always(callable $onAlways)
106134
*/
107135
public function progress(callable $onProgress)
108136
{
109-
$this->adaptee->progress($onProgress);
137+
$this->adaptee->progress(function () use ($onProgress) {
138+
return call_user_func_array($onProgress, func_get_args());
139+
});
110140

111141
return $this;
112142
}

src/Tidal/WampWatch/Adapter/Thruway/ClientSession.php

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
use Thruway\ClientSession as ThruwaySession;
1515
use Tidal\WampWatch\ClientSessionInterface;
16+
use React\Promise\Promise as ReactPromise;
17+
use Tidal\WampWatch\Adapter\React\PromiseAdapter;
1618

1719
class ClientSession implements ClientSessionInterface
1820
{
@@ -33,11 +35,13 @@ public function __construct(ThruwaySession $thruwaySession)
3335
* @param callable $callback
3436
* @param $options array
3537
*
36-
* @return \React\Promise\Promise
38+
* @return PromiseAdapter
3739
*/
3840
public function subscribe($topicName, callable $callback, $options = null)
3941
{
40-
return $this->thruwaySession->subscribe($topicName, $callback, $options);
42+
return $this->createPromiseAdapter(
43+
$this->thruwaySession->subscribe($topicName, $callback, $options)
44+
);
4145
}
4246

4347
/**
@@ -48,11 +52,13 @@ public function subscribe($topicName, callable $callback, $options = null)
4852
* @param array|mixed $argumentsKw
4953
* @param array|mixed $options
5054
*
51-
* @return \React\Promise\Promise
55+
* @return PromiseAdapter
5256
*/
5357
public function publish($topicName, $arguments = null, $argumentsKw = null, $options = null)
5458
{
55-
return $this->thruwaySession->publish($topicName, $arguments, $argumentsKw, $options);
59+
return $this->createPromiseAdapter(
60+
$this->thruwaySession->publish($topicName, $arguments, $argumentsKw, $options)
61+
);
5662
}
5763

5864
/**
@@ -62,23 +68,27 @@ public function publish($topicName, $arguments = null, $argumentsKw = null, $opt
6268
* @param callable $callback
6369
* @param array|mixed $options
6470
*
65-
* @return \React\Promise\Promise
71+
* @return PromiseAdapter
6672
*/
6773
public function register($procedureName, callable $callback, $options = null)
6874
{
69-
return $this->thruwaySession->register($procedureName, $callback, $options);
75+
return $this->createPromiseAdapter(
76+
$this->thruwaySession->register($procedureName, $callback, $options)
77+
);
7078
}
7179

7280
/**
7381
* Unregister.
7482
*
7583
* @param string $procedureName
7684
*
77-
* @return \React\Promise\Promise
85+
* @return PromiseAdapter
7886
*/
7987
public function unregister($procedureName)
8088
{
81-
return $this->thruwaySession->unregister($procedureName);
89+
return $this->createPromiseAdapter(
90+
$this->thruwaySession->unregister($procedureName)
91+
);
8292
}
8393

8494
/**
@@ -89,11 +99,13 @@ public function unregister($procedureName)
8999
* @param array|mixed $argumentsKw
90100
* @param array|mixed $options
91101
*
92-
* @return \React\Promise\Promise
102+
* @return PromiseAdapter
93103
*/
94104
public function call($procedureName, $arguments = null, $argumentsKw = null, $options = null)
95105
{
96-
return $this->thruwaySession->call($procedureName, $arguments, $argumentsKw, $options);
106+
return $this->createPromiseAdapter(
107+
$this->thruwaySession->call($procedureName, $arguments, $argumentsKw, $options)
108+
);
97109
}
98110

99111
/**
@@ -112,8 +124,21 @@ public function getSessionId()
112124
return $this->thruwaySession->getSessionId();
113125
}
114126

127+
/**
128+
* @param $msg
129+
*/
115130
public function sendMessage($msg)
116131
{
117132
$this->thruwaySession->sendMessage($msg);
118133
}
134+
135+
/**
136+
* @param ReactPromise $promise
137+
*
138+
* @return PromiseAdapter
139+
*/
140+
private function createPromiseAdapter(ReactPromise $promise)
141+
{
142+
return new PromiseAdapter($promise);
143+
}
119144
}

src/Tidal/WampWatch/Model/Contract/RouterInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
interface RouterInterface
1717
{
1818
/**
19-
* @return string;
19+
* @return string
2020
*/
2121
public function getUri();
2222

src/Tidal/WampWatch/MonitorTrait.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
use React\Promise\Promise;
1616
use Tidal\WampWatch\ClientSessionInterface as ClientSession;
1717
use Tidal\WampWatch\Subscription\Collection as SubscriptionCollection;
18+
use React\Promise\Deferred;
19+
use Tidal\WampWatch\Adapter\React\PromiseAdapter;
20+
use Tidal\WampWatch\Adapter\React\DeferredAdapter;
1821

1922
/**
2023
* Description of MonitorTrait.
@@ -188,4 +191,39 @@ private function getErrorCallback()
188191
return $error;
189192
};
190193
}
194+
195+
private function retrieveCallData($procedure, callable $filter = null, $arguments = [])
196+
{
197+
$deferred = new DeferredAdapter(
198+
new Deferred()
199+
);
200+
201+
$filter = $filter ?: function ($res) {
202+
return $res;
203+
};
204+
205+
$this->session->call($procedure, $arguments)
206+
->then(
207+
function ($res) use ($deferred, $filter) {
208+
$deferred->resolve($filter($res));
209+
},
210+
$this->getErrorCallback()
211+
);
212+
213+
return $deferred->promise();
214+
}
215+
216+
/**
217+
* @param callable $callback
218+
*
219+
* @return PromiseAdapter
220+
*/
221+
private function createPromiseAdapter(callable $callback)
222+
{
223+
return new PromiseAdapter(
224+
new Promise(
225+
$callback
226+
)
227+
);
228+
}
191229
}

src/Tidal/WampWatch/SessionMonitor.php

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
namespace Tidal\WampWatch;
1313

1414
use Evenement\EventEmitterInterface;
15-
use React\Promise\Promise;
1615
use Tidal\WampWatch\ClientSessionInterface as ClientSession;
16+
use Tidal\WampWatch\Adapter\React\PromiseAdapter;
1717

1818
/**
1919
* Description of SessionMonitor.
@@ -62,7 +62,7 @@ public function __construct(ClientSession $session)
6262
*
6363
* @param $sessionId
6464
*
65-
* @return \React\Promise\Promise;
65+
* @return PromiseAdapter
6666
*/
6767
public function getSessionInfo($sessionId)
6868
{
@@ -83,17 +83,19 @@ function ($error) {
8383
* registered on the wamp-router in the monitor's realm
8484
* and populates the data via given callback,.
8585
*
86-
* @return Promise
86+
* @return PromiseAdapter
8787
*/
8888
public function getSessionIds()
8989
{
9090
if (!count($this->sessionIds)) {
9191
return $this->retrieveSessionIds();
9292
}
9393

94-
return new Promise(function (callable $resolve) {
95-
$resolve($this->sessionIds);
96-
});
94+
return $this->createPromiseAdapter(
95+
function (callable $resolve) {
96+
$resolve($this->getList());
97+
}
98+
);
9799
}
98100

99101
/**
@@ -189,34 +191,42 @@ protected function initSetupCalls()
189191
/**
190192
* Retrieves the list of current sessionIds on the router.
191193
*
192-
* @return \React\Promise\Promise;
194+
* @return PromiseAdapter
193195
*/
194196
protected function retrieveSessionIds()
195197
{
196-
return $this->session->call(self::SESSION_LIST_TOPIC, [])
197-
->then(
198-
$this->getSessionIdRetrievalCallback(),
199-
$this->getErrorCallback()
200-
);
198+
return $this->retrieveCallData(
199+
self::SESSION_LIST_TOPIC,
200+
$this->getSessionIdRetrievalCallback(),
201+
[]
202+
);
201203
}
202204

205+
/**
206+
* @return \Closure
207+
*/
203208
protected function getSessionIdRetrievalCallback()
204209
{
205210
return function ($res) {
206-
// remove our own sessionID from the tracked sessions
207-
$sessionIds = $this->removeOwnSessionId($res[0]);
208-
$this->setList($sessionIds);
209-
$this->emit('list', [$this->getList()]);
211+
$this->setList($res[0]);
212+
$sessionIds = $this->getList();
213+
$this->emit('list', [$sessionIds]);
210214

211-
return $this->getList();
215+
return $sessionIds;
212216
};
213217
}
214218

219+
/**
220+
* @param $list
221+
*/
215222
protected function setList($list)
216223
{
217-
$this->sessionIds = $list;
224+
$this->sessionIds = $this->removeOwnSessionId($list);
218225
}
219226

227+
/**
228+
* @return array
229+
*/
220230
protected function getList()
221231
{
222232
return $this->sessionIds;
@@ -232,7 +242,8 @@ protected function getList()
232242
protected function removeOwnSessionId(array $sessionsIds)
233243
{
234244
$key = array_search($this->session->getSessionId(), $sessionsIds);
235-
if ($key >= 0) {
245+
246+
if ($key !== false && $key >= 0) {
236247
unset($sessionsIds[$key]);
237248
$sessionsIds = array_values($sessionsIds);
238249
}

0 commit comments

Comments
 (0)