WP_HTTP_Polling_Sync_Server::process_sync_update( string $room, int $client_id, int $cursor,  $update ): true|WP_Error

In this article

This function’s access is marked private. This means it is not intended for use by plugin or theme developers, only by core. It is listed here for completeness.

Processes a sync update based on its type.

Parameters

$roomstringrequired
Room identifier.
$client_idintrequired
Client identifier.
$cursorintrequired
Client cursor (marker of last seen update).
string, type: string} $update Sync update.

Return

true|WP_Error True on success, WP_Error on storage failure.

Source

private function process_sync_update( string $room, int $client_id, int $cursor, array $update ) {
	$data = $update['data'];
	$type = $update['type'];

	switch ( $type ) {
		case self::UPDATE_TYPE_COMPACTION:
			/*
			 * Compaction replaces updates the client has already seen. Only remove
			 * updates with markers before the client's cursor to preserve updates
			 * that arrived since the client's last sync.
			 *
			 * Check for a newer compaction update first. If one exists, skip this
			 * compaction to avoid overwriting it.
			 */
			$updates_after_cursor = $this->storage->get_updates_after_cursor( $room, $cursor );
			$has_newer_compaction = false;

			foreach ( $updates_after_cursor as $existing ) {
				if ( self::UPDATE_TYPE_COMPACTION === $existing['type'] ) {
					$has_newer_compaction = true;
					break;
				}
			}

			if ( ! $has_newer_compaction ) {
				if ( ! $this->storage->remove_updates_before_cursor( $room, $cursor ) ) {
					return new WP_Error(
						'rest_sync_storage_error',
						__( 'Failed to remove updates during compaction.' ),
						array( 'status' => 500 )
					);
				}

				return $this->add_update( $room, $client_id, $type, $data );
			}

			/*
			 * A newer compaction already advanced the cursor, but we
			 * can not safely drop an update. The incoming bytes still encode
			 * operations other clients may not have seen, so store them as a
			 * regular update. Y.applyUpdateV2 merges state-as-update blobs
			 * idempotently, so overlap with the existing compaction is safe.
			 */
			return $this->add_update( $room, $client_id, self::UPDATE_TYPE_UPDATE, $data );

		case self::UPDATE_TYPE_SYNC_STEP1:
		case self::UPDATE_TYPE_SYNC_STEP2:
		case self::UPDATE_TYPE_UPDATE:
			/*
			 * Sync step 1 announces a client's state vector. Other clients need
			 * to see it so they can respond with sync_step2 containing missing
			 * updates. The cursor-based filtering prevents re-delivery.
			 *
			 * Sync step 2 contains updates for a specific client.
			 *
			 * All updates are stored persistently.
			 */
			return $this->add_update( $room, $client_id, $type, $data );
	}

	return new WP_Error(
		'rest_invalid_update_type',
		__( 'Invalid sync update type.' ),
		array( 'status' => 400 )
	);
}

Changelog

VersionDescription
7.0.0Introduced.

User Contributed Notes

You must log in before being able to contribute a note or feedback.