Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 41 additions & 1 deletion lib/experimental/synchronization.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,44 @@ function gutenberg_rest_api_crdt_post_meta() {
)
);
}
add_action( 'init', 'gutenberg_rest_api_crdt_post_meta' );
add_action( 'init', 'gutenberg_rest_api_crdt_post_meta', 999, 0 );

/**
* Saves CRDT post meta on autosave requests. Autosaves are sometimes applied to
* the post itself instead of a revision:
*
* https://github.com/WordPress/wordpress-develop/blob/dc62ecbc345ca1b8d1801eca794d71755b7568f1/src/wp-includes/rest-api/endpoints/class-wp-rest-autosaves-controller.php#L235-L244
*
* When this happens, the special post meta handling in `create_post_autosave` is
* not called. We do it manually in this filter before the response is returned.
*
* @param WP_REST_Response $response The response object.
* @param WP_Post $post The post object.
* @param WP_REST_Request $request The request object.
* @return WP_REST_Response Modified response object.
*/
function gutenberg_add_crdt_meta_on_autosave( WP_REST_Response $response, WP_Post $post, WP_REST_Request $request ) {
// Ensure that this is an autosave request.
if ( ! defined( 'DOING_AUTOSAVE' ) || true !== constant( 'DOING_AUTOSAVE' ) ) {
return $response;
}

// Get the persisted CRDT meta from the request.
$meta = $request->get_param( 'meta' );
$key = '_crdt_document'; // Must match the key registered in gutenberg_rest_api_crdt_post_meta().
$value = $meta[ $key ] ?? '';

if ( empty( $value ) ) {
return $response;
}

update_post_meta( $post->ID, $key, $value );

// Update the response.
$data = $response->get_data();
$data['meta'] = array_merge( $data['meta'] ?? array(), array( $key => $value ) );
$response->set_data( $data );

return $response;
}
add_filter( 'rest_prepare_autosave', 'gutenberg_add_crdt_meta_on_autosave', 10, 3 );
13 changes: 13 additions & 0 deletions packages/core-data/src/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -644,6 +644,19 @@ export const saveEntityRecord =
...autosavePost,
...record,
};
// Additionally call the entity-specific pre-persist handler, since
// autosaves can target the actual persisted entity in some cases.
if ( globalThis.IS_GUTENBERG_PLUGIN ) {
if ( entityConfig.__unstablePrePersist ) {
data = {
...data,
...entityConfig.__unstablePrePersist(
persistedRecord,
data
),
};
}
}
data = Object.keys( data ).reduce(
( acc, key ) => {
if (
Expand Down
7 changes: 6 additions & 1 deletion packages/sync/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,14 @@ export function markEntityAsSaved( ydoc: CRDTDoc ): void {
recordMeta.set( SAVED_BY_KEY, ydoc.clientID );
}

function pseudoRandomID(): number {
return Math.floor( Math.random() * 1000000000 );
}

export function serializeCrdtDoc( crdtDoc: CRDTDoc ): string {
return JSON.stringify( {
document: buffer.toBase64( Y.encodeStateAsUpdateV2( crdtDoc ) ),
updateId: pseudoRandomID(), // helps with debugging
} );
}

Expand All @@ -76,7 +81,7 @@ export function deserializeCrdtDoc(
// Overwrite the client ID (which is from a previous session) with a random
// client ID. Deserialized documents should not be used directly. Instead,
// their state should be applied to another in-use document.
ydoc.clientID = Math.floor( Math.random() * 1000000000 );
ydoc.clientID = pseudoRandomID();

return ydoc;
} catch ( e ) {
Expand Down
Loading