Move Promise classes to their own namespace

This commit is contained in:
Dylan K. Taylor
2021-11-02 17:10:07 +00:00
parent 38f97bed52
commit d184838ba0
5 changed files with 7 additions and 7 deletions

View File

@@ -1,55 +0,0 @@
<?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\utils;
use function spl_object_id;
/**
* @phpstan-template TValue
*/
final class Promise{
/**
* @internal Do NOT call this directly; create a new Resolver and call Resolver->promise()
* @see PromiseResolver
* @phpstan-param PromiseSharedData<TValue> $shared
*/
public function __construct(private PromiseSharedData $shared){}
/**
* @phpstan-param \Closure(TValue) : void $onSuccess
* @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);
}else{
$this->shared->onSuccess[spl_object_id($onSuccess)] = $onSuccess;
$this->shared->onFailure[spl_object_id($onFailure)] = $onFailure;
}
}
public function isResolved() : bool{
return $this->shared->resolved;
}
}

View File

@@ -1,75 +0,0 @@
<?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\utils;
/**
* @phpstan-template TValue
*/
final class PromiseResolver{
/** @phpstan-var PromiseSharedData<TValue> */
private PromiseSharedData $shared;
/** @phpstan-var Promise<TValue> */
private Promise $promise;
public function __construct(){
$this->shared = new PromiseSharedData();
$this->promise = new Promise($this->shared);
}
/**
* @param mixed $value
* @phpstan-param TValue $value
*/
public function resolve($value) : void{
if($this->shared->resolved){
throw new \LogicException("Promise has already been resolved/rejected");
}
$this->shared->resolved = true;
$this->shared->result = $value;
foreach($this->shared->onSuccess as $c){
$c($value);
}
$this->shared->onSuccess = [];
$this->shared->onFailure = [];
}
public function reject() : void{
if($this->shared->resolved){
throw new \LogicException("Promise has already been resolved/rejected");
}
$this->shared->resolved = true;
foreach($this->shared->onFailure as $c){
$c();
}
$this->shared->onSuccess = [];
$this->shared->onFailure = [];
}
/**
* @phpstan-return Promise<TValue>
*/
public function getPromise() : Promise{
return $this->promise;
}
}

View File

@@ -1,51 +0,0 @@
<?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\utils;
/**
* @internal
* @see PromiseResolver
* @phpstan-template TValue
*/
final class PromiseSharedData{
/**
* @var \Closure[]
* @phpstan-var array<int, \Closure(TValue) : void>
*/
public array $onSuccess = [];
/**
* @var \Closure[]
* @phpstan-var array<int, \Closure() : void>
*/
public array $onFailure = [];
public bool $resolved = false;
/**
* @var mixed
* @phpstan-var TValue|null
*/
public $result = null;
}