WP_HTTP_Polling_Sync_Server::process_sync_update
Processes a sync update based on its type.
Method of the class: WP_HTTP_Polling_Sync_Server{}
No Hooks.
Returns
true|WP_Error. True on success, WP_Error on storage failure.
Usage
// private - for code of main (parent) class only $result = $this->process_sync_update( $room, $client_id, $cursor, $update );
- $room(string) (required)
- Room identifier.
- $client_id(int) (required)
- Client identifier.
- $cursor(int) (required)
- Client cursor (marker of last seen update).
- $update(array) (required)
- .
Changelog
| Since 7.0.0 | Introduced. |
WP_HTTP_Polling_Sync_Server::process_sync_update() WP HTTP Polling Sync Server::process sync update code WP 7.0
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 )
);
}