Skip to content
Closed
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
17 changes: 12 additions & 5 deletions packages/sync/src/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -610,11 +610,18 @@ export function createSyncManager( debug = false ): SyncManager {
} );

// Use the invalidated keys to get the updated values from the entity.
const changes = invalidatedKeys.reduce(
( acc, key ) =>
Object.assign( acc, {
[ key ]: record[ key ],
} ),
const changes = invalidatedKeys.reduce< Partial< ObjectData > >(
( acc, key ) => {
acc[ key ] = record[ key ];
// The entity record never exposes a `blocks` property (only
// serialized `content`), so also carry `content` along to let
// `applyPostChangesToCRDTDoc` re-derive blocks from content
// instead of wiping them.
if ( key === 'blocks' && record.content ) {
acc.content = record.content;
}
return acc;
},
{}
);

Expand Down
43 changes: 43 additions & 0 deletions packages/sync/src/test/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,49 @@ describe( 'SyncManager', () => {
1
);
} );

it( 'carries record.content along when blocks is an invalidated field', async () => {
mockRecord = {
...mockRecord,
content: 'Test content',
};

mockSyncConfig = {
...mockSyncConfig,
getPersistedCRDTDoc: jest.fn( () =>
createPersistedCRDTDoc( {
...mockRecord,
blocks: [ { name: 'core/paragraph' } ],
} )
),
};

const manager = createSyncManager();

await manager.load(
mockSyncConfig,
'post',
'123',
mockRecord,
mockHandlers
);

// The entity record never exposes a `blocks` property, so the
// change for the invalidated `blocks` key should also carry
// `content` along, letting the CRDT doc re-derive blocks from
// content instead of wiping them.
const expectedChanges = {
blocks: undefined,
content: mockRecord.content,
};

expect(
mockSyncConfig.applyChangesToCRDTDoc
).toHaveBeenCalledTimes( 1 );
expect(
mockSyncConfig.applyChangesToCRDTDoc
).toHaveBeenCalledWith( expect.any( Y.Doc ), expectedChanges );
} );
} );
} );

Expand Down
Loading