Skip to content

Commit 74d2bb4

Browse files
committed
Merge pull request #47 from clue-labs/double-cancellation
Cancellation handler must only be called once, ignore double cancellation
2 parents 9892525 + 98c4441 commit 74d2bb4

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

src/Promise.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,10 @@ public function cancel()
9191
return;
9292
}
9393

94-
$this->call($this->canceller);
94+
$canceller = $this->canceller;
95+
$this->canceller = null;
96+
97+
$this->call($canceller);
9598
}
9699

97100
private function resolver(callable $onFulfilled = null, callable $onRejected = null, callable $onProgress = null)

tests/PromiseTest/CancelTestTrait.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,31 @@ public function cancelShouldTriggerCancellerWhenAllChildrenCancel()
189189
$child2->cancel();
190190
}
191191

192+
/** @test */
193+
public function cancelShouldNotTriggerCancellerWhenCancellingOneChildrenMultipleTimes()
194+
{
195+
$adapter = $this->getPromiseTestAdapter($this->expectCallableNever());
196+
197+
$child1 = $adapter->promise()
198+
->then()
199+
->then();
200+
201+
$child2 = $adapter->promise()
202+
->then();
203+
204+
$child1->cancel();
205+
$child1->cancel();
206+
}
207+
208+
/** @test */
209+
public function cancelShouldTriggerCancellerOnlyOnceWhenCancellingMultipleTimes()
210+
{
211+
$adapter = $this->getPromiseTestAdapter($this->expectCallableOnce());
212+
213+
$adapter->promise()->cancel();
214+
$adapter->promise()->cancel();
215+
}
216+
192217
/** @test */
193218
public function cancelShouldAlwaysTriggerCancellerWhenCalledOnRootPromise()
194219
{

0 commit comments

Comments
 (0)