Skip to content

Commit

Permalink
Use static internal callbacks without binding to parent promise
Browse files Browse the repository at this point in the history
  • Loading branch information
clue committed Jun 11, 2018
1 parent 5e60e55 commit 72087fd
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/Promise.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function done(callable $onFulfilled = null, callable $onRejected = null,
return $this->result->done($onFulfilled, $onRejected, $onProgress);
}

$this->handlers[] = function (ExtendedPromiseInterface $promise) use ($onFulfilled, $onRejected) {
$this->handlers[] = static function (ExtendedPromiseInterface $promise) use ($onFulfilled, $onRejected) {
$promise
->done($onFulfilled, $onRejected);
};
Expand All @@ -73,7 +73,7 @@ public function done(callable $onFulfilled = null, callable $onRejected = null,

public function otherwise(callable $onRejected)
{
return $this->then(null, function ($reason) use ($onRejected) {
return $this->then(null, static function ($reason) use ($onRejected) {
if (!_checkTypehint($onRejected, $reason)) {
return new RejectedPromise($reason);
}
Expand All @@ -84,11 +84,11 @@ public function otherwise(callable $onRejected)

public function always(callable $onFulfilledOrRejected)
{
return $this->then(function ($value) use ($onFulfilledOrRejected) {
return $this->then(static function ($value) use ($onFulfilledOrRejected) {
return resolve($onFulfilledOrRejected())->then(function () use ($value) {
return $value;
});
}, function ($reason) use ($onFulfilledOrRejected) {
}, static function ($reason) use ($onFulfilledOrRejected) {
return resolve($onFulfilledOrRejected())->then(function () use ($reason) {
return new RejectedPromise($reason);
});
Expand Down Expand Up @@ -116,7 +116,7 @@ private function resolver(callable $onFulfilled = null, callable $onRejected = n
{
return function ($resolve, $reject, $notify) use ($onFulfilled, $onRejected, $onProgress) {
if ($onProgress) {
$progressHandler = function ($update) use ($notify, $onProgress) {
$progressHandler = static function ($update) use ($notify, $onProgress) {
try {
$notify($onProgress($update));
} catch (\Throwable $e) {
Expand All @@ -129,7 +129,7 @@ private function resolver(callable $onFulfilled = null, callable $onRejected = n
$progressHandler = $notify;
}

$this->handlers[] = function (ExtendedPromiseInterface $promise) use ($onFulfilled, $onRejected, $resolve, $reject, $progressHandler) {
$this->handlers[] = static function (ExtendedPromiseInterface $promise) use ($onFulfilled, $onRejected, $resolve, $reject, $progressHandler) {
$promise
->then($onFulfilled, $onRejected)
->done($resolve, $reject, $progressHandler);
Expand Down
66 changes: 66 additions & 0 deletions tests/PromiseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,72 @@ public function shouldIgnoreNotifyAfterReject()
$promise->cancel();
}


/** @test */
public function shouldNotLeaveGarbageCyclesWhenRemovingLastReferenceToPendingPromise()
{
gc_collect_cycles();
$promise = new Promise(function () { });
unset($promise);

$this->assertSame(0, gc_collect_cycles());
}

/** @test */
public function shouldNotLeaveGarbageCyclesWhenRemovingLastReferenceToPendingPromiseWithThenFollowers()
{
gc_collect_cycles();
$promise = new Promise(function () { });
$promise->then()->then()->then();
unset($promise);

$this->assertSame(0, gc_collect_cycles());
}

/** @test */
public function shouldNotLeaveGarbageCyclesWhenRemovingLastReferenceToPendingPromiseWithDoneFollowers()
{
gc_collect_cycles();
$promise = new Promise(function () { });
$promise->done();
unset($promise);

$this->assertSame(0, gc_collect_cycles());
}

/** @test */
public function shouldNotLeaveGarbageCyclesWhenRemovingLastReferenceToPendingPromiseWithOtherwiseFollowers()
{
gc_collect_cycles();
$promise = new Promise(function () { });
$promise->otherwise(function () { });
unset($promise);

$this->assertSame(0, gc_collect_cycles());
}

/** @test */
public function shouldNotLeaveGarbageCyclesWhenRemovingLastReferenceToPendingPromiseWithAlwaysFollowers()
{
gc_collect_cycles();
$promise = new Promise(function () { });
$promise->always(function () { });
unset($promise);

$this->assertSame(0, gc_collect_cycles());
}

/** @test */
public function shouldNotLeaveGarbageCyclesWhenRemovingLastReferenceToPendingPromiseWithProgressFollowers()
{
gc_collect_cycles();
$promise = new Promise(function () { });
$promise->then(null, null, function () { });
unset($promise);

$this->assertSame(0, gc_collect_cycles());
}

/** @test */
public function shouldFulfillIfFullfilledWithSimplePromise()
{
Expand Down

0 comments on commit 72087fd

Please sign in to comment.