Use static child canceller callback without binding to parent promise
Christian Lück authored 6 years ago
Cees-Jan Kiewiet committed 4 years ago
26 | 26 |
return new static($this->resolver($onFulfilled, $onRejected));
|
27 | 27 |
}
|
28 | 28 |
|
29 | |
$this->requiredCancelRequests++;
|
30 | |
|
31 | |
return new static($this->resolver($onFulfilled, $onRejected), function () {
|
32 | |
$this->requiredCancelRequests--;
|
33 | |
|
34 | |
if ($this->requiredCancelRequests <= 0) {
|
35 | |
$this->cancel();
|
36 | |
}
|
37 | |
});
|
|
29 |
// keep a reference to this promise instance for the static canceller function.
|
|
30 |
// see also parentCancellerFunction() for more details.
|
|
31 |
$parent = $this;
|
|
32 |
++$parent->requiredCancelRequests;
|
|
33 |
|
|
34 |
return new static(
|
|
35 |
$this->resolver($onFulfilled, $onRejected),
|
|
36 |
static function () use (&$parent) {
|
|
37 |
--$parent->requiredCancelRequests;
|
|
38 |
|
|
39 |
if ($parent->requiredCancelRequests <= 0) {
|
|
40 |
$parent->cancel();
|
|
41 |
}
|
|
42 |
|
|
43 |
$parent = null;
|
|
44 |
}
|
|
45 |
);
|
38 | 46 |
}
|
39 | 47 |
|
40 | 48 |
public function done(callable $onFulfilled = null, callable $onRejected = null): void
|
|
118 | 126 |
->done($resolve, $reject);
|
119 | 127 |
};
|
120 | 128 |
};
|
121 | |
}
|
122 | |
|
123 | |
private function resolve($value = null): void
|
124 | |
{
|
125 | |
if (null !== $this->result) {
|
126 | |
return;
|
127 | |
}
|
128 | |
|
129 | |
$this->settle(resolve($value));
|
130 | 129 |
}
|
131 | 130 |
|
132 | 131 |
private function reject(\Throwable $reason): void
|
96 | 96 |
}
|
97 | 97 |
|
98 | 98 |
/** @test */
|
|
99 |
public function shouldRejectWithoutCreatingGarbageCyclesIfParentCancellerRejectsWithException()
|
|
100 |
{
|
|
101 |
gc_collect_cycles();
|
|
102 |
$promise = new Promise(function ($resolve, $reject) { }, function ($resolve, $reject) {
|
|
103 |
$reject(new \Exception('foo'));
|
|
104 |
});
|
|
105 |
$promise->then()->then()->then()->cancel();
|
|
106 |
unset($promise);
|
|
107 |
|
|
108 |
$this->assertSame(0, gc_collect_cycles());
|
|
109 |
}
|
|
110 |
|
|
111 |
/** @test */
|
99 | 112 |
public function shouldRejectWithoutCreatingGarbageCyclesIfResolverThrowsException()
|
100 | 113 |
{
|
101 | 114 |
gc_collect_cycles();
|