Skip to content

Commit

Permalink
Forget first response to save more memory
Browse files Browse the repository at this point in the history
  • Loading branch information
cerbero90 committed Feb 11, 2024
1 parent b363fa1 commit ed81c94
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 23 deletions.
13 changes: 4 additions & 9 deletions src/Paginations/Pagination.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

namespace Cerbero\LazyJsonPages\Paginations;

use Cerbero\LazyJsonPages\Concerns\ParsesPages;
use Cerbero\LazyJsonPages\Concerns\ResolvesPages;
use Cerbero\LazyJsonPages\Concerns\YieldsPaginatedItems;
use Cerbero\LazyJsonPages\Dtos\Config;
use Cerbero\LazyJsonPages\Services\Book;
use Cerbero\LazyJsonPages\Sources\Source;
use Cerbero\LazyJsonPages\Sources\AnySource;
use IteratorAggregate;
use Traversable;

Expand All @@ -19,19 +19,14 @@
*/
abstract class Pagination implements IteratorAggregate
{
use YieldsPaginatedItems;
use ParsesPages;
use ResolvesPages;

/**
* The collector of pages.
*/
public readonly Book $book;

/**
* The number of items per page.
*/
protected readonly int $itemsPerPage;

/**
* Yield the paginated items.
*
Expand All @@ -43,7 +38,7 @@ abstract public function getIterator(): Traversable;
* Instantiate the class.
*/
final public function __construct(
protected readonly Source $source,
protected readonly AnySource $source,
protected readonly Config $config,
) {
$this->book = new Book();
Expand Down
21 changes: 19 additions & 2 deletions src/Sources/AnySource.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@ class AnySource extends Source
/**
* The matching source.
*/
protected ?Source $matchingSource;
protected readonly Source $matchingSource;

/**
* The cached HTTP response.
*/
protected ?ResponseInterface $response;

/**
* Retrieve the HTTP request.
Expand Down Expand Up @@ -67,6 +72,18 @@ protected function matchingSource(): Source
*/
public function response(): ResponseInterface
{
return $this->matchingSource()->response();
return $this->response ??= $this->matchingSource()->response();
}

/**
* Retrieve the HTTP response and forget it to save memory.
*/
public function pullResponse(): ResponseInterface
{
$response = $this->response();

$this->response = null;

return $response;
}
}
14 changes: 2 additions & 12 deletions src/Sources/Endpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,6 @@ class Endpoint extends Source
{
use DetectsEndpoints;

/**
* The HTTP request.
*/
protected readonly RequestInterface $request;

/**
* The HTTP response value object
*/
protected readonly ResponseInterface $response;

/**
* Determine whether this class can handle the source.
*/
Expand All @@ -44,7 +34,7 @@ public function matches(): bool
*/
public function request(): RequestInterface
{
return $this->request ??= new Request('GET', $this->source);
return new Request('GET', $this->source);
}

/**
Expand All @@ -54,6 +44,6 @@ public function request(): RequestInterface
*/
public function response(): ResponseInterface
{
return $this->response ??= Client::instance()->send($this->request());
return Client::instance()->send($this->request());
}
}

0 comments on commit ed81c94

Please sign in to comment.