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
25 changes: 24 additions & 1 deletion packages/core-data/src/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
getSyncManager,
} from './sync';
import logEntityDeprecation from './utils/log-entity-deprecation';
import { POST_META_KEY_FOR_CRDT_DOC_PERSISTENCE } from './utils/crdt';

function addTitleToAutoDraft( record ) {
return record.status === 'auto-draft' ? { ...record, title: '' } : record;
Expand Down Expand Up @@ -779,10 +780,32 @@ export const saveEntityRecord =
) ),
};
}

let fetchPayload = edits;

if ( persistedRecord ) {
const objectType = `${ kind }/${ name }`;
const serializedDoc =
await getSyncManager()?.createPersistedCRDTDoc(
objectType,
persistedRecord[ entityIdKey ]
);

if ( serializedDoc ) {
fetchPayload = {
...fetchPayload,
meta: {
...fetchPayload.meta,
[ POST_META_KEY_FOR_CRDT_DOC_PERSISTENCE ]:
serializedDoc,
},
};
}
}
updatedRecord = await __unstableFetch( {
path,
method: recordId ? 'PUT' : 'POST',
data: edits,
data: fetchPayload,
} );
dispatch.receiveEntityRecords(
kind,
Expand Down
18 changes: 0 additions & 18 deletions packages/core-data/src/entities.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import { __ } from '@wordpress/i18n';
* Internal dependencies
*/
import { PostEditorAwareness } from './awareness/post-editor-awareness';
import { getSyncManager } from './sync';
import {
applyPostChangesToCRDTDoc,
defaultCollectionSyncConfig,
Expand Down Expand Up @@ -306,23 +305,6 @@ export const prePersistPostType = async (
}
}

// Add meta for persisted CRDT document.
if ( persistedRecord ) {
const objectType = `postType/${ name }`;
const objectId = persistedRecord.id;
const serializedDoc = await getSyncManager()?.createPersistedCRDTDoc(
objectType,
objectId
);

if ( serializedDoc ) {
newEdits.meta = {
...edits.meta,
[ POST_META_KEY_FOR_CRDT_DOC_PERSISTENCE ]: serializedDoc,
};
}
}

return newEdits;
};

Expand Down
31 changes: 1 addition & 30 deletions packages/core-data/src/test/entities.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,7 @@ import {
prePersistPostType,
additionalEntityConfigLoaders,
} from '../entities';
import { getSyncManager } from '../sync';
import {
applyPostChangesToCRDTDoc,
POST_META_KEY_FOR_CRDT_DOC_PERSISTENCE,
} from '../utils/crdt';
import { applyPostChangesToCRDTDoc } from '../utils/crdt';

describe( 'getMethodName', () => {
it( 'should return the right method name for an entity with the root kind', () => {
Expand Down Expand Up @@ -107,31 +103,6 @@ describe( 'prePersistPostType', () => {
await prePersistPostType( record, edits, 'post', true )
).toEqual( {} );
} );

it( 'adds meta with serialized CRDT doc when createPersistedCRDTDoc returns a value', async () => {
const mockSerializedDoc = 'serialized-crdt-doc-data';
getSyncManager.mockReturnValue( {
createPersistedCRDTDoc: jest
.fn()
.mockReturnValue( mockSerializedDoc ),
} );

const record = { id: 123, status: 'publish' };
const edits = {};
const result = await prePersistPostType( record, edits, 'post', false );

expect( result.meta ).toEqual( {
[ POST_META_KEY_FOR_CRDT_DOC_PERSISTENCE ]: mockSerializedDoc,
} );

expect( getSyncManager ).toHaveBeenCalled();
expect( getSyncManager().createPersistedCRDTDoc ).toHaveBeenCalledWith(
'postType/post',
123
);

getSyncManager.mockReset();
} );
} );

describe( 'loadPostTypeEntities', () => {
Expand Down
Loading