Skip to content

Commit

Permalink
feat(File): removing some unnecessary Futures
Browse files Browse the repository at this point in the history
  • Loading branch information
razshare committed Apr 5, 2024
1 parent 1143893 commit 7177929
Show file tree
Hide file tree
Showing 11 changed files with 140 additions and 152 deletions.
6 changes: 3 additions & 3 deletions docs/1.router.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function main(): Unsafe {
$server = Server::create()->try($error)
or yield $error;

$server->start()->await()->try($error)
$server->start()->try($error)
or yield $error;
});
}
Expand Down Expand Up @@ -60,7 +60,7 @@ function main(): Unsafe {
$server->router->get('/cats', handler(...))->try($error)
or yield $error;

$server->start()->await()->try($error)
$server->start()->try($error)
or yield $error;
});
}
Expand Down Expand Up @@ -114,7 +114,7 @@ function main(): Unsafe {
$server->router->post('/cats', handler(...))->try($error)
or yield $error;

$server->start()->await()->try($error)
$server->start()->try($error)
or yield $error;
});
}
Expand Down
4 changes: 2 additions & 2 deletions docs/18.open-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ function main():Unsafe {
$server->router->get("/openapi", openapi(...))->try($error)
or yield $error;

$server->start()->await()->try($error)
$server->start()->try($error)
or yield $error;
});
}
Expand Down Expand Up @@ -124,7 +124,7 @@ function main(): Unsafe {
$server->router->get('/test/{value}', handler(...))->try($error)
or yield $error;

$server->start()->await()->try($error)
$server->start()->try($error)
or yield $error;
});
}
Expand Down
8 changes: 4 additions & 4 deletions docs/2.path-parameters.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function main(): Unsafe {
$server->router->get('/about/{name}', handler(...))->try($error)
or yield $error;

$server->start()->await()->try($error)
$server->start()->try($error)
or yield $error;
});
}
Expand Down Expand Up @@ -62,7 +62,7 @@ function main(): Unsafe {
$server->router->post('/set/age/{age}', handler(...))->try($error)
or yield $error;

$server->start()->await()->try($error)
$server->start()->try($error)
or yield $error;
});
}
Expand Down Expand Up @@ -97,7 +97,7 @@ function main(): Unsafe {
$server->router->get('/about/{name}', handler(...))->try($error)
or yield $error;

$server->start()->await()->try($error)
$server->start()->try($error)
or yield $error;
});
}
Expand Down Expand Up @@ -128,7 +128,7 @@ function main(): Unsafe {
$server->router->get('/about/{name}/child/{childName}', handler(...))->try($error)
or yield $error;

$server->start()->await()->try($error)
$server->start()->try($error)
or yield $error;
});
}
Expand Down
6 changes: 3 additions & 3 deletions docs/4.session.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ function main(): Unsafe {
$server->router->get('/', handler(...))->try($error)
or yield $error;

$server->start()->await()->try($error)
$server->start()->try($error)
or yield $error;
});
}
Expand Down Expand Up @@ -84,7 +84,7 @@ function main(): Unsafe {
$server->router->get('/', handler(...))->try($error)
or yield $error;

$server->start()->await()->try($error)
$server->start()->try($error)
or yield $error;
});
}
Expand Down Expand Up @@ -117,7 +117,7 @@ function main(): Unsafe {
return anyError(function(){
Container::provide(SessionInterface::class, fn(Request $request) => new CustomSession($request));
$server = Server::create()->try($error) or yield $error;
$server->start()->await()->try($error) or yield $error;
$server->start()->try($error) or yield $error;
});
}
```
2 changes: 1 addition & 1 deletion docs/7.byte-range-requests.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ function main(): Unsafe {
$server->router->get('/{fileName}', handler(...))->try($error)
or yield $error;

$server->start()->await()->try($error)
$server->start()->try($error)
or yield $error;
});
}
Expand Down
2 changes: 1 addition & 1 deletion docs/8.custom-attributes.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ function main(): Unsafe {
$server->router->get("/", handler(...))->try($error)
or yield $error;

$server->start()->await()->try($error)
$server->start()->try($error)
or yield $error;
});
}
Expand Down
124 changes: 58 additions & 66 deletions src/lib/Core/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use function Amp\File\getSize;
use function Amp\File\getStatus;
use function Amp\File\openFile;
use Amp\Future;
use Dotenv\Dotenv;
use Error;
use Throwable;
Expand Down Expand Up @@ -66,48 +65,46 @@ public static function checksum(string $fileNameA, string $fileNameB, bool $bina

/**
* Copy a file.
* @param string $from
* @param string $to
* @return Future<Unsafe<void>>
* @param string $from
* @param string $to
* @return Unsafe<void>
*/
public static function copy(string $from, string $to):Future {
return async(static function() use ($from, $to) {
$source = File::open($from)->try($error);
if ($error) {
return error($error);
}
public static function copy(string $from, string $to):Unsafe {
$source = File::open($from)->try($error);
if ($error) {
return error($error);
}

$toDirectory = dirname($to);
$toDirectory = dirname($to);

if (!File::exists($toDirectory)) {
Directory::create($toDirectory)->try($error);
if ($error) {
return error($error);
}
if (!File::exists($toDirectory)) {
Directory::create($toDirectory)->try($error);
if ($error) {
return error($error);
}
}

$dirname = dirname($to);
$dirname = dirname($to);

if (false === $dirname) {
Directory::create($dirname)->try($error);
if ($error) {
return $error;
}
if (false === $dirname) {
Directory::create($dirname)->try($error);
if ($error) {
return $error;
}
}

if (!File::exists($dirname)) {
}
if (!File::exists($dirname)) {
}

$destination = File::open($to, 'x')->try($error);
$destination = File::open($to, 'x')->try($error);

if ($error) {
return error($error);
}
if ($error) {
return error($error);
}

$stream = $source->getAmpFile();
$stream = $source->getAmpFile();

return $destination->writeStream($stream)->await();
});
return $destination->writeStream($stream);
}


Expand Down Expand Up @@ -323,51 +320,46 @@ public function truncate(int $size) {
}

/**
* @param string $content
* @param int $chunkSize
* @return Future<Unsafe<void>>
* @param string $content
* @param int $chunkSize
* @return Unsafe<void>
*/
public function write(string $content, int $chunkSize = 8192):Future {
$ampFile = $this->ampFile;
return async(static function() use ($ampFile, $content, $chunkSize) {
try {
$wroteSoFar = 0;
$length = strlen($content);
while (true) {
$step = $wroteSoFar + $chunkSize;
$chunk = substr($content, $wroteSoFar, $step);
async(static fn () => $ampFile->write($chunk))->await();
$wroteSoFar = $wroteSoFar + $step;
if ($wroteSoFar >= $length) {
return ok();
}
public function write(string $content, int $chunkSize = 8192):Unsafe {
try {
$wroteSoFar = 0;
$length = strlen($content);
while (true) {
$step = $wroteSoFar + $chunkSize;
$chunk = substr($content, $wroteSoFar, $step);
async(static fn () => $this->ampFile->write($chunk))->await();
$wroteSoFar = $wroteSoFar + $step;
if ($wroteSoFar >= $length) {
return ok();
}
} catch(Throwable $e) {
return error($e);
}
});
} catch(Throwable $e) {
return error($e);
}
}

/**
* @param ReadableStream $readableStream
* @param int $chunkSize
* @return Future<Unsafe<void>>
* @param ReadableStream $readableStream
* @param int $chunkSize
* @return Unsafe<void>
*/
public function writeStream(ReadableStream $readableStream, int $chunkSize = 8192):Future {
public function writeStream(ReadableStream $readableStream, int $chunkSize = 8192):Unsafe {
$ampFile = $this->ampFile;
return async(function() use ($ampFile, $readableStream, $chunkSize) {
try {
while (true) {
$chunk = async(static fn () => $readableStream->read(null, $chunkSize))->await();
if (null === $chunk) {
return ok();
}
async(static fn () => $ampFile->write($chunk))->await();
try {
while (true) {
$chunk = async(static fn () => $readableStream->read(null, $chunkSize))->await();
if (null === $chunk) {
return ok();
}
} catch(Throwable $e) {
return error($e);
async(static fn () => $ampFile->write($chunk))->await();
}
});
} catch(Throwable $e) {
return error($e);
}
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/lib/Core/GoffiContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public static function create(string $interface, string $fileName):Unsafe {
$phar = Phar::running();
$headerFileName = './'.basename('.'.str_replace($phar, '', $localHeaderFileName));
if (!File::exists($headerFileName)) {
File::copy($localHeaderFileName, $headerFileName)->await()->try($error);
File::copy($localHeaderFileName, $headerFileName)->try($error);
if ($error) {
return error($error);
}
Expand Down Expand Up @@ -65,7 +65,7 @@ public static function create(string $interface, string $fileName):Unsafe {
$externalFileName = './'.basename('.'.str_replace($phar, '', $fileName));

if (!File::exists($externalFileName)) {
File::copy($fileName, $externalFileName)->await()->try($error);
File::copy($fileName, $externalFileName)->try($error);
if ($error) {
return error($error);
}
Expand Down
Loading

0 comments on commit 7177929

Please sign in to comment.