Skip to content

Commit

Permalink
dev: refactor AbstractConnectionResolver::get_ids() to ::prepare_ids()
Browse files Browse the repository at this point in the history
  • Loading branch information
justlevine committed May 4, 2024
1 parent 5673054 commit c7ad0bf
Showing 1 changed file with 35 additions and 16 deletions.
51 changes: 35 additions & 16 deletions src/Data/Connection/AbstractConnectionResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -669,9 +669,7 @@ public function get_query() {
*/
public function get_ids(): array {
if ( ! isset( $this->ids ) ) {
$ids = $this->get_ids_from_query();

$ids = $this->apply_cursors_to_ids( $ids );
$ids = $this->prepare_ids();

/**
* Filter the connection IDs
Expand Down Expand Up @@ -936,9 +934,11 @@ public function get_connection() {
*/
return new Deferred(
function () {
if ( ! empty( $this->ids ) ) {
// Load the IDs.
$this->get_loader()->load_many( $this->ids );
$ids = $this->get_ids();

if ( ! empty( $ids ) ) {
// Load the ids.
$this->get_loader()->load_many( $ids );
}

$edges = $this->get_edges();
Expand Down Expand Up @@ -992,18 +992,18 @@ protected function execute_and_get_ids(): array {

$this->query = $this->get_query();

$this->ids = $this->get_ids();
$ids = $this->get_ids();

if ( empty( $this->ids ) ) {
if ( empty( $ids ) ) {
return [];
}

/**
* Buffer the IDs for deferred resolution
*/
$this->get_loader()->buffer( $this->ids );
$this->get_loader()->buffer( $ids );

return $this->ids;
return $ids;
}

/**
Expand Down Expand Up @@ -1152,6 +1152,19 @@ protected function prepare_nodes(): array {
return $nodes;
}

/**
* Prepares the IDs for the connection.
*
* @used-by self::get_ids()
*
* @return int[]|string[]
*/
protected function prepare_ids(): array {
$ids = $this->get_ids_from_query();

return $this->apply_cursors_to_ids( $ids );
}

/**
* Gets the IDs for the currently-paginated slice of nodes.
*
Expand All @@ -1162,17 +1175,19 @@ protected function prepare_nodes(): array {
* @return int[]|string[]
*/
protected function get_ids_for_nodes() {
if ( empty( $this->ids ) ) {
$ids = $this->get_ids();

if ( empty( $ids ) ) {
return [];
}

// If we're going backwards then our overfetched ID is at the front.
if ( ! empty( $this->args['last'] ) && count( $this->ids ) > absint( $this->args['last'] ) ) {
return array_slice( $this->ids, count( $this->ids ) - absint( $this->args['last'] ), $this->get_query_amount(), true );
if ( ! empty( $this->args['last'] ) && count( $ids ) > absint( $this->args['last'] ) ) {
return array_slice( $ids, count( $ids ) - absint( $this->args['last'] ), $this->get_query_amount(), true );
}

// If we're going forwards, our overfetched ID is at the back.
return array_slice( $this->ids, 0, $this->get_query_amount(), true );
return array_slice( $ids, 0, $this->get_query_amount(), true );
}

/**
Expand Down Expand Up @@ -1346,7 +1361,9 @@ public function get_before_offset() {
*/
public function has_next_page(): bool {
if ( ! empty( $this->args['first'] ) ) {
return ! empty( $this->ids ) && count( $this->ids ) > $this->get_query_amount();
$ids = $this->get_ids();

return ! empty( $ids ) && count( $ids ) > $this->get_query_amount();
}

$before_offset = $this->get_before_offset();
Expand All @@ -1365,7 +1382,9 @@ public function has_next_page(): bool {
*/
public function has_previous_page(): bool {
if ( ! empty( $this->args['last'] ) ) {
return ! empty( $this->ids ) && count( $this->ids ) > $this->get_query_amount();
$ids = $this->get_ids();

return ! empty( $ids ) && count( $ids ) > $this->get_query_amount();
}

$after_offset = $this->get_after_offset();
Expand Down

0 comments on commit c7ad0bf

Please sign in to comment.