@@ -124,10 +124,11 @@ public function get_awareness_state( string $room, int $timeout = 30 ): array {
124124
125125 // Remove out of date entries and duplicate entries.
126126 $ entries = array ();
127- foreach ( $ cached_awareness as $ index => $ client_awareness ) {
127+ foreach ( $ cached_awareness as $ client_awareness ) {
128128 if ( empty ( $ client_awareness ['timestamp ' ] ) || $ client_awareness ['timestamp ' ] < $ cutoff_timestamp ) {
129129 continue ;
130130 }
131+ // Account for duplicates added by race conditions.
131132 $ entries [ $ client_awareness ['client_id ' ] ] = $ client_awareness ;
132133 }
133134
@@ -401,17 +402,21 @@ public function set_awareness_state( string $room, string $client_id, array $sta
401402
402403 $ data = wp_json_encode ( $ state );
403404
404- /* Check if a row already exists. */
405+ /*
406+ * Check if a row already exists.
407+ *
408+ * In the event of a race condition, the latest row will be returned as the update target.
409+ */
405410 $ exists = $ wpdb ->get_row (
406411 $ wpdb ->prepare (
407- "SELECT id, date_gmt FROM {$ wpdb ->collaboration } WHERE room = %s AND type = 'awareness' AND client_id = %s LIMIT 1 " ,
412+ "SELECT id, date_gmt, data FROM {$ wpdb ->collaboration } WHERE room = %s AND type = 'awareness' AND client_id = %s ORDER BY id DESC LIMIT 1 " ,
408413 $ room ,
409414 $ client_id
410415 )
411416 );
412417
413- if ( $ exists && $ exists ->date_gmt === $ now_mysql ) {
414- // Row already has the current date, consider update a success.
418+ if ( $ exists && $ exists ->date_gmt === $ now_mysql && $ exists -> data === $ data ) {
419+ // Row already has the current date & state , consider update a success.
415420 return true ;
416421 }
417422
0 commit comments