1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace WyriHaximus\Recoil; |
4
|
|
|
|
5
|
|
|
use Recoil\Kernel; |
6
|
|
|
use Rx\DisposableInterface; |
7
|
|
|
use Rx\ObservableInterface; |
8
|
|
|
use Rx\Subject\Subject; |
9
|
|
|
|
10
|
|
|
final class InfiniteCaller implements QueueCallerInterface |
11
|
|
|
{ |
12
|
|
|
/** @var Kernel */ |
13
|
|
|
private $kernel; |
14
|
|
|
|
15
|
|
|
/** @var State */ |
16
|
|
|
private $state; |
17
|
|
|
|
18
|
|
|
/** @var QueueCallerInterface[] */ |
19
|
|
|
private $callers = []; |
20
|
|
|
|
21
|
|
|
/** @var string[] */ |
22
|
|
|
private $readyCallers = []; |
23
|
|
|
|
24
|
|
|
/** @var State[] */ |
25
|
|
|
private $callerState = []; |
26
|
|
|
|
27
|
|
|
/** @var Subject */ |
28
|
|
|
private $callerStream = []; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param Kernel $kernel |
32
|
|
|
*/ |
33
|
1 |
|
public function __construct(Kernel $kernel) |
34
|
|
|
{ |
35
|
1 |
|
$this->kernel = $kernel; |
36
|
|
|
|
37
|
1 |
|
$this->state = new State(); |
38
|
1 |
|
$this->state->onNext(State::WAITING); |
39
|
1 |
|
} |
40
|
|
|
|
41
|
1 |
|
public function call(ObservableInterface $observable): State |
42
|
|
|
{ |
43
|
|
|
try { |
44
|
|
|
$observable->subscribe(function (Call $call) { |
45
|
|
|
try { |
46
|
1 |
|
$this->delegateCall($call); |
47
|
|
|
} catch (\Throwable $et) { |
48
|
|
|
echo (string)$et; |
49
|
|
|
} |
50
|
1 |
|
}); |
51
|
|
|
} catch (\Throwable $et) { |
52
|
|
|
echo (string)$et; |
53
|
|
|
} |
54
|
|
|
|
55
|
1 |
|
return $this->state; |
56
|
|
|
} |
57
|
|
|
|
58
|
1 |
|
private function delegateCall(Call $call): void |
59
|
|
|
{ |
60
|
|
|
/*if (count($this->readyCallers) > 0) { |
|
|
|
|
61
|
|
|
$qcHash = array_pop($this->readyCallers); |
62
|
|
|
$this->callerStream[$qcHash]->onNext($call); |
63
|
|
|
|
64
|
|
|
return; |
65
|
|
|
}*/ |
66
|
|
|
|
67
|
1 |
|
$stream = new Subject(); |
68
|
1 |
|
$caller = new QueueCaller($this->kernel); |
69
|
1 |
|
$qcHash = spl_object_hash($caller); |
70
|
|
|
|
71
|
1 |
|
$this->callers[$qcHash] = $caller; |
72
|
1 |
|
$this->callerStream[$qcHash] = $stream; |
73
|
1 |
|
$this->callerState[$qcHash] = $caller->call($stream); |
74
|
|
|
/** @var DisposableInterface $disposeable */ |
75
|
|
|
$disposeable = $this->callerState[$qcHash]->filter(function (int $state) { |
|
|
|
|
76
|
1 |
|
return $state === State::WAITING; |
77
|
|
|
})->subscribe(function () use (&$disposeable, $stream, $call) { |
78
|
1 |
|
$stream->onNext($call); |
79
|
1 |
|
$disposeable->dispose(); |
80
|
1 |
|
}); |
81
|
1 |
|
} |
82
|
|
|
} |
83
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.