Codebase list php-react-promise / ab526c8
Use static child canceller callback without binding to parent promise Christian Lück authored 6 years ago Cees-Jan Kiewiet committed 4 years ago
2 changed file(s) with 30 addition(s) and 18 deletion(s). Raw diff Collapse all Expand all
2626 return new static($this->resolver($onFulfilled, $onRejected));
2727 }
2828
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 );
3846 }
3947
4048 public function done(callable $onFulfilled = null, callable $onRejected = null): void
118126 ->done($resolve, $reject);
119127 };
120128 };
121 }
122
123 private function resolve($value = null): void
124 {
125 if (null !== $this->result) {
126 return;
127 }
128
129 $this->settle(resolve($value));
130129 }
131130
132131 private function reject(\Throwable $reason): void
9696 }
9797
9898 /** @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 */
99112 public function shouldRejectWithoutCreatingGarbageCyclesIfResolverThrowsException()
100113 {
101114 gc_collect_cycles();