mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-06-03 18:42:37 +00:00
Fixed Promise<null> calling rejection handler given after being successfully resolved
closes #6110 this is a weird use case, but it should work nonetheless.
This commit is contained in:
parent
e4888d7102
commit
73b1fba53c
@ -41,8 +41,11 @@ final class Promise{
|
||||
* @phpstan-param \Closure() : void $onFailure
|
||||
*/
|
||||
public function onCompletion(\Closure $onSuccess, \Closure $onFailure) : void{
|
||||
if($this->shared->resolved){
|
||||
$this->shared->result === null ? $onFailure() : $onSuccess($this->shared->result);
|
||||
$state = $this->shared->state;
|
||||
if($state === true){
|
||||
$onSuccess($this->shared->result);
|
||||
}elseif($state === false){
|
||||
$onFailure();
|
||||
}else{
|
||||
$this->shared->onSuccess[spl_object_id($onSuccess)] = $onSuccess;
|
||||
$this->shared->onFailure[spl_object_id($onFailure)] = $onFailure;
|
||||
@ -50,6 +53,8 @@ final class Promise{
|
||||
}
|
||||
|
||||
public function isResolved() : bool{
|
||||
return $this->shared->resolved;
|
||||
//TODO: perhaps this should return true when rejected? currently there's no way to tell if a promise was
|
||||
//rejected or just hasn't been resolved yet
|
||||
return $this->shared->state === true;
|
||||
}
|
||||
}
|
||||
|
@ -41,10 +41,10 @@ final class PromiseResolver{
|
||||
* @phpstan-param TValue $value
|
||||
*/
|
||||
public function resolve(mixed $value) : void{
|
||||
if($this->shared->resolved){
|
||||
if($this->shared->state !== null){
|
||||
throw new \LogicException("Promise has already been resolved/rejected");
|
||||
}
|
||||
$this->shared->resolved = true;
|
||||
$this->shared->state = true;
|
||||
$this->shared->result = $value;
|
||||
foreach($this->shared->onSuccess as $c){
|
||||
$c($value);
|
||||
@ -54,10 +54,10 @@ final class PromiseResolver{
|
||||
}
|
||||
|
||||
public function reject() : void{
|
||||
if($this->shared->resolved){
|
||||
if($this->shared->state !== null){
|
||||
throw new \LogicException("Promise has already been resolved/rejected");
|
||||
}
|
||||
$this->shared->resolved = true;
|
||||
$this->shared->state = false;
|
||||
foreach($this->shared->onFailure as $c){
|
||||
$c();
|
||||
}
|
||||
|
@ -41,8 +41,8 @@ final class PromiseSharedData{
|
||||
*/
|
||||
public array $onFailure = [];
|
||||
|
||||
public bool $resolved = false;
|
||||
public ?bool $state = null;
|
||||
|
||||
/** @phpstan-var TValue|null */
|
||||
public mixed $result = null;
|
||||
/** @phpstan-var TValue */
|
||||
public mixed $result;
|
||||
}
|
||||
|
42
tests/phpunit/promise/PromiseTest.php
Normal file
42
tests/phpunit/promise/PromiseTest.php
Normal file
@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
*
|
||||
* ____ _ _ __ __ _ __ __ ____
|
||||
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
|
||||
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
|
||||
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
|
||||
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* @author PocketMine Team
|
||||
* @link http://www.pocketmine.net/
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace pocketmine\promise;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class PromiseTest extends TestCase{
|
||||
|
||||
public function testPromiseNull() : void{
|
||||
$resolver = new PromiseResolver();
|
||||
$resolver->resolve(null);
|
||||
$resolver->getPromise()->onCompletion(
|
||||
function(mixed $value) : void{
|
||||
self::assertNull($value);
|
||||
},
|
||||
function() : void{
|
||||
self::fail("Promise should not be rejected");
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user