Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change promise usage to recommendation #49

Merged
merged 1 commit into from Oct 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 5 additions & 41 deletions src/AbstractSyncAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,9 @@
namespace React\Filesystem;

use DateTime;
use LogicException;
use React\Filesystem\Node\NodeInterface;
use React\Filesystem\Stream\StreamFactory;
use React\Promise\FulfilledPromise;
use React\Promise\PromiseInterface;
use React\Promise\RejectedPromise;
use WyriHaximus\React\ChildProcess\Messenger\Messages\Factory;
use WyriHaximus\React\ChildProcess\Messenger\Messenger;

abstract class AbstractSyncAdapter implements AdapterInterface
{
Expand Down Expand Up @@ -123,8 +119,6 @@ protected function processLsContents($basePath, $result, ObjectStream $stream)
];
$promises[] = \React\Filesystem\detectType($this->typeDetectors, $node)->then(function (NodeInterface $node) use ($stream) {
$stream->write($node);

return new FulfilledPromise();
});
}

Expand Down Expand Up @@ -154,19 +148,7 @@ public function touch($path, $mode = self::CREATION_MODE)
*/
public function open($path, $flags, $mode = self::CREATION_MODE)
{
return new RejectedPromise();
$id = null;
return \WyriHaximus\React\ChildProcess\Messenger\Factory::parentFromClass(self::CHILD_CLASS_NAME, $this->loop)->then(function (Messenger $messenger) use (&$id, $path, $flags, $mode) {
$id = count($this->fileDescriptors);
$this->fileDescriptors[$id] = $messenger;
return $this->fileDescriptors[$id]->rpc(Factory::rpc('open', [
'path' => $path,
'flags' => $flags,
'mode' => $mode,
]));
})->then(function () use ($path, $flags, &$id) {
return \React\Promise\resolve(StreamFactory::create($path, $id, $flags, $this));
});
return \React\Promise\reject(new LogicException('Not implemented'));
}

/**
Expand All @@ -177,13 +159,7 @@ public function open($path, $flags, $mode = self::CREATION_MODE)
*/
public function read($fileDescriptor, $length, $offset)
{
return new RejectedPromise();
return $this->fileDescriptors[$fileDescriptor]->rpc(Factory::rpc('read', [
'length' => $length,
'offset' => $offset,
]))->then(function ($payload) {
return \React\Promise\resolve($payload['chunk']);
});
return \React\Promise\reject(new LogicException('Not implemented'));
}

/**
Expand All @@ -195,12 +171,7 @@ public function read($fileDescriptor, $length, $offset)
*/
public function write($fileDescriptor, $data, $length, $offset)
{
return new RejectedPromise();
return $this->fileDescriptors[$fileDescriptor]->rpc(Factory::rpc('write', [
'chunk' => $data,
'length' => $length,
'offset' => $offset,
]));
return \React\Promise\reject(new LogicException('Not implemented'));
}

/**
Expand All @@ -209,14 +180,7 @@ public function write($fileDescriptor, $data, $length, $offset)
*/
public function close($fd)
{
return new RejectedPromise();
$fileDescriptor = $this->fileDescriptors[$fd];
unset($this->fileDescriptors[$fd]);
return $fileDescriptor->rpc(Factory::rpc('close'))->then(function () use ($fileDescriptor) {
return $fileDescriptor->softTerminate();
}, function () use ($fileDescriptor) {
return $fileDescriptor->softTerminate();
});
return \React\Promise\reject(new LogicException('Not implemented'));
}

/**
Expand Down
3 changes: 0 additions & 3 deletions src/Eio/Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use React\Filesystem\Stream\StreamFactory;
use React\Filesystem\TypeDetectorInterface;
use React\Promise\Deferred;
use React\Promise\FulfilledPromise;

class Adapter implements AdapterInterface
{
Expand Down Expand Up @@ -228,8 +227,6 @@ protected function processLsContents($basePath, $result, ObjectStream $stream)
];
$promises[] = \React\Filesystem\detectType($this->typeDetectors, $node)->then(function (NodeInterface $node) use ($stream) {
$stream->write($node);

return new FulfilledPromise();
});
}

Expand Down
4 changes: 2 additions & 2 deletions src/Eio/ConstTypeDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace React\Filesystem\Eio;

use Exception;
use React\Filesystem\FilesystemInterface;
use React\Filesystem\TypeDetectorInterface;
use React\Promise\RejectedPromise;

class ConstTypeDetector implements TypeDetectorInterface
{
Expand Down Expand Up @@ -37,7 +37,7 @@ public function __construct(FilesystemInterface $filesystem)
public function detect(array $node)
{
if (!isset($node['type']) || !isset($this->mapping[$node['type']])) {
return new RejectedPromise();
return \React\Promise\reject(new Exception('Unknown node type'));
}

return \React\Promise\resolve([
Expand Down
4 changes: 2 additions & 2 deletions src/MappedTypeDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace React\Filesystem;

use React\Promise\RejectedPromise;
use Exception;

class MappedTypeDetector implements TypeDetectorInterface
{
Expand Down Expand Up @@ -52,7 +52,7 @@ public function __construct(FilesystemInterface $filesystem, $options = [])
public function detect(array $node)
{
if (!isset($node['type']) || !isset($this->mapping[$node['type']])) {
return new RejectedPromise();
return \React\Promise\reject(new Exception('Unknown type'));
}

return \React\Promise\resolve([
Expand Down
6 changes: 3 additions & 3 deletions src/ModeTypeDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace React\Filesystem;

use Exception;
use React\Filesystem\FilesystemInterface;
use React\Filesystem\TypeDetectorInterface;
use React\Promise\RejectedPromise;

class ModeTypeDetector implements TypeDetectorInterface
{
Expand Down Expand Up @@ -43,7 +43,7 @@ public function detect(array $node)

protected function walkMapping($stat)
{
$promiseChain = new RejectedPromise();
$promiseChain = \React\Promise\reject(new Exception('Unknown type'));
foreach ($this->mapping as $mappingMode => $method) {
$promiseChain = $promiseChain->otherwise(function () use ($stat, $mappingMode, $method) {
return $this->matchMapping($stat['mode'], $mappingMode, $method);
Expand All @@ -61,6 +61,6 @@ protected function matchMapping($mode, $mappingMode, $method)
]);
}

return new RejectedPromise();
return \React\Promise\reject(new Exception('Unknown filesystem method for type'));
}
}
5 changes: 1 addition & 4 deletions src/Node/Directory.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use React\Filesystem\ObjectStream;
use React\Filesystem\ObjectStreamSink;
use React\Promise\Deferred;
use React\Promise\FulfilledPromise;

class Directory implements DirectoryInterface
{
Expand Down Expand Up @@ -114,15 +113,13 @@ protected function processSizeContents($nodes, $recursive)
$numbers['directories'] += $size['directories'];
$numbers['files'] += $size['files'];
$numbers['size'] += $size['size'];
return new FulfilledPromise();
});
}
break;
case $node instanceof File:
$numbers['files']++;
$promises[] = $node->size()->then(function ($size) use (&$numbers) {
$numbers['size'] += $size;
return new FulfilledPromise();
});
break;
}
Expand Down Expand Up @@ -189,7 +186,7 @@ public function createRecursive($mode = AdapterInterface::CREATION_MODE)
})->then(function () use ($mode) {
return $this->create($mode);
})->then(function () {
return new FulfilledPromise();
return null;
});
}

Expand Down
14 changes: 6 additions & 8 deletions src/Node/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@

namespace React\Filesystem\Node;

use Exception;
use React\Filesystem\AdapterInterface;
use React\Filesystem\FilesystemInterface;
use React\Filesystem\ObjectStream;
use React\Filesystem\ObjectStreamSink;
use React\Filesystem\Stream\GenericStreamInterface;
use React\Promise\FulfilledPromise;
use React\Promise\RejectedPromise;
use React\Promise\Stream;
use React\Stream\ReadableStreamInterface;
use React\Stream\WritableStreamInterface;
Expand Down Expand Up @@ -50,9 +49,9 @@ public function __construct($filename, FilesystemInterface $filesystem)
public function exists()
{
return $this->stat()->then(function () {
return new FulfilledPromise();
return null;
}, function () {
return new RejectedPromise(new \Exception('Not found'));
throw new Exception('Not found');
});
}

Expand Down Expand Up @@ -96,7 +95,7 @@ public function rename($toFilename)
public function create($mode = AdapterInterface::CREATION_MODE, $time = null)
{
return $this->stat()->then(function () {
return new RejectedPromise(new \Exception('File exists'));
throw new \Exception('File exists already');
}, function () use ($mode, $time) {
return $this->adapter->touch($this->path, $mode, $time);
});
Expand All @@ -116,7 +115,7 @@ public function touch($mode = AdapterInterface::CREATION_MODE, $time = null)
public function open($flags, $mode = AdapterInterface::CREATION_MODE)
{
if ($this->open === true) {
return new RejectedPromise();
return \React\Promise\reject(new Exception('File is already open'));
}

return $this->adapter->open($this->path, $flags, $mode)->then(function (GenericStreamInterface $stream) {
Expand All @@ -132,13 +131,12 @@ public function open($flags, $mode = AdapterInterface::CREATION_MODE)
public function close()
{
if ($this->open === false) {
return new RejectedPromise();
return \React\Promise\reject(new Exception('File is already closed'));
}

return $this->adapter->close($this->fileDescriptor)->then(function () {
$this->open = false;
$this->fileDescriptor = null;
return new FulfilledPromise();
});
}

Expand Down
3 changes: 1 addition & 2 deletions src/OpenFileLimiter.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace React\Filesystem;

use React\Promise\Deferred;
use React\Promise\FulfilledPromise;

class OpenFileLimiter
{
Expand Down Expand Up @@ -43,7 +42,7 @@ public function open()
{
if ($this->current < $this->limit) {
$this->current++;
return new FulfilledPromise();
return \React\Promise\resolve();
}

$deferred = new Deferred();
Expand Down
4 changes: 1 addition & 3 deletions src/Stream/DuplexStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use Evenement\EventEmitter;
use React\Filesystem\AdapterInterface;
use React\Stream\DuplexStreamInterface;
use React\Promise\FulfilledPromise;

class DuplexStream extends EventEmitter implements DuplexStreamInterface, GenericStreamInterface
{
Expand Down Expand Up @@ -39,12 +38,11 @@ protected function readChunk()
protected function resolveSize()
{
if ($this->readCursor < $this->size) {
return new FulfilledPromise();
return \React\Promise\resolve();
}

return $this->getFilesystem()->stat($this->path)->then(function ($stat) {
$this->size = $stat['size'];
return new FulfilledPromise();
});
}
}
7 changes: 3 additions & 4 deletions src/Stream/ReadableStreamTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

namespace React\Filesystem\Stream;

use React\Promise\FulfilledPromise;
use React\Promise\RejectedPromise;
use Exception;
use React\Stream\Util;
use React\Stream\WritableStreamInterface;

Expand All @@ -27,11 +26,11 @@ public function resume()
if ($this->size === null && $this->sizeLookupPromise === null) {
$this->sizeLookupPromise = $this->getFilesystem()->stat($this->getPath())->then(function ($info) {
if ($this->size !== null) {
return new RejectedPromise();
throw new Exception('File was already stat-ed');
}

$this->size = $info['size'];
$this->readCursor = 0;
return new FulfilledPromise();
});
}

Expand Down
4 changes: 2 additions & 2 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace React\Filesystem;

use React\Promise\RejectedPromise;
use Exception;

/**
* @param AdapterInterface $adapter
Expand Down Expand Up @@ -40,7 +40,7 @@ function getOpenFileLimit(array $options)
*/
function detectType(array $typeDetectors, array $node)
{
$promiseChain = new RejectedPromise();
$promiseChain = \React\Promise\reject(new Exception('Unknown type'));
foreach ($typeDetectors as $detector) {
$promiseChain = $promiseChain->otherwise(function () use ($node, $detector) {
return $detector->detect($node);
Expand Down
3 changes: 2 additions & 1 deletion tests/Eio/ConstTypeDetectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace React\Tests\Filesystem\Eio;

use Exception;
use React\Filesystem\Eio\ConstTypeDetector;
use React\Filesystem\Filesystem;
use React\Tests\Filesystem\TestCase;
Expand Down Expand Up @@ -62,7 +63,7 @@ public function testDetectUnknown()
(new ConstTypeDetector($filesystem))->detect([
'type' => 123,
])->otherwise(function ($result) use (&$callbackFired) {
$this->assertNull($result);
$this->assertInstanceOf('Exception', $result);
$callbackFired = true;
});

Expand Down
3 changes: 2 additions & 1 deletion tests/ModeTypeDetectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace React\Tests\Filesystem;

use Exception;
use React\Filesystem\Filesystem;
use React\Filesystem\ModeTypeDetector;
use React\Promise\FulfilledPromise;
Expand Down Expand Up @@ -69,7 +70,7 @@ public function testDetectUnknown()
(new ModeTypeDetector($filesystem))->detect([
'path' => 'foo.bar',
])->otherwise(function ($result) use (&$callbackFired) {
$this->assertNull($result);
$this->assertInstanceOf('Exception', $result);
$callbackFired = true;
});

Expand Down
Loading