Skip to content

Commit

Permalink
[Yjs Collab] fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dmonad committed Jan 8, 2025
1 parent a3a944f commit 54dda4d
Show file tree
Hide file tree
Showing 10 changed files with 69 additions and 41 deletions.
1 change: 1 addition & 0 deletions packages/core-data/src/hooks/test/use-entity-record.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ describe( 'useEntityRecord', () => {
parse: false,
} )
);
await waitFor( () => expect( data.status ).not.toEqual( 'RESOLVING' ) );

expect( data ).toEqual( {
edit: expect.any( Function ),
Expand Down
10 changes: 4 additions & 6 deletions packages/core-data/src/hooks/use-entity-block-editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ export default function useEntityBlockEditor( kind, name, { id: _id } = {} ) {
// @todo is this where the content is set / updated?
const providerId = useEntityId( kind, name );
const id = _id ?? providerId;
const { getEntityRecord, getEntityRecordEdits, getEntityConfig } = useSelect( STORE_NAME );
const { getEntityRecord, getEntityRecordEdits, getEntityConfig } =
useSelect( STORE_NAME );
const { content, editedBlocks, meta } = useSelect(
( select ) => {
if ( ! id ) {
Expand Down Expand Up @@ -106,10 +107,7 @@ export default function useEntityBlockEditor( kind, name, { id: _id } = {} ) {
const edits = {
selection,
content: ( { blocks: blocksForSerialization = [] } ) => {
const entityConfig = getEntityConfig(
kind,
name
);
const entityConfig = getEntityConfig( kind, name );
const objectId = entityConfig.getSyncObjectId( id );
return __unstableSerializeAndCleanWithYdoc(
blocksForSerialization,
Expand All @@ -133,7 +131,7 @@ export default function useEntityBlockEditor( kind, name, { id: _id } = {} ) {
meta,
__unstableCreateUndoLevel,
editEntityRecord,
getEntityConfig
getEntityConfig,
]
);

Expand Down
12 changes: 7 additions & 5 deletions packages/core-data/src/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,13 +179,15 @@ export const getEntityRecord =
}
}
registry.batch( () => {
if (window.__experimentalEnableHeartbeatSync = true) {
if ( ( window.__experimentalEnableHeartbeatSync = true ) ) {
// @todo put this somewhere else.
// The document is collaborative. decrease autosave interval.
registry.dispatch( 'core/editor' ).updateEditorSettings( {
autosaveInterval: 5,
localAutosaveInterval: 4,
} );
registry
.dispatch?.( 'core/editor' )
?.updateEditorSettings?.( {
autosaveInterval: 5,
localAutosaveInterval: 4,
} );
}
dispatch.receiveEntityRecords( kind, name, record, query );
dispatch.receiveUserPermissions( receiveUserPermissionArgs );
Expand Down
39 changes: 24 additions & 15 deletions packages/editor/src/components/post-text-editor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,30 @@ import { store as editorStore } from '../../store';
*/
export default function PostTextEditor() {
const instanceId = useInstanceId( PostTextEditor );
const { content, blocks, type, id, entityConfig } = useSelect( ( select ) => {
const { getEditedEntityRecord, getEntityConfig } = select( coreStore );
const { getCurrentPostType, getCurrentPostId } = select( editorStore );
const _type = getCurrentPostType();
const _id = getCurrentPostId();
const editedRecord = getEditedEntityRecord( 'postType', _type, _id );
const _entityConfig = getEntityConfig('postType', _type)
return {
entityConfig: _entityConfig,
content: editedRecord?.content,
blocks: editedRecord?.blocks,
type: _type,
id: _id,
};
}, [] );
const { content, blocks, type, id, entityConfig } = useSelect(
( select ) => {
const { getEditedEntityRecord, getEntityConfig } =
select( coreStore );
const { getCurrentPostType, getCurrentPostId } =
select( editorStore );
const _type = getCurrentPostType();
const _id = getCurrentPostId();
const editedRecord = getEditedEntityRecord(
'postType',
_type,
_id
);
const _entityConfig = getEntityConfig( 'postType', _type );
return {
entityConfig: _entityConfig,
content: editedRecord?.content,
blocks: editedRecord?.blocks,
type: _type,
id: _id,
};
},
[]
);
const { editEntityRecord } = useDispatch( coreStore );
// Replicates the logic found in getEditedPostContent().
const value = useMemo( () => {
Expand Down
7 changes: 6 additions & 1 deletion packages/editor/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
getFreeformContentHandlerName,
getDefaultBlockName,
__unstableSerializeAndCleanWithYdoc,
__unstableSerializeAndClean,
parse,
} from '@wordpress/blocks';
import { isInTheFuture, getDate } from '@wordpress/date';
Expand Down Expand Up @@ -916,6 +917,7 @@ export const getEditedPostContent = createRegistrySelector(
( select ) => ( state ) => {
const postId = getCurrentPostId( state );
const postType = getCurrentPostType( state );

const record = select( coreStore ).getEditedEntityRecord(
'postType',
postType,
Expand All @@ -929,7 +931,10 @@ export const getEditedPostContent = createRegistrySelector(
'postType',
postType
);
const objectId = entityConfig.getSyncObjectId( record.id );
const objectId = entityConfig.getSyncObjectId?.( record.id );
if ( objectId === null ) {
return __unstableSerializeAndClean( record.blocks );
}
return __unstableSerializeAndCleanWithYdoc(
record.blocks,
entityConfig.syncObjectType,
Expand Down
6 changes: 6 additions & 0 deletions packages/editor/src/store/test/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ selectorNames.forEach( ( name ) => {
);
},

getEntityConfig() {
return {
getSyncObjectId: () => '0',
};
},

getEntityRecordEdits() {
const present = state.editor && state.editor.present;
let edits = present && present.edits;
Expand Down
2 changes: 1 addition & 1 deletion packages/sync/src/create-webrtc-connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export function createWebRTCConnection( { signaling, password } ) {
// @ts-ignore
password,
} );
} catch (err) {
} catch ( err ) {
// nop
}
return Promise.resolve( () => true );
Expand Down
23 changes: 13 additions & 10 deletions packages/sync/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,19 +62,22 @@ let syncProvider;

export function getSyncProvider() {
if ( ! syncProvider ) {
// @ts-ignore
const connectionProvider = window?.__experimentalEnableWebrtcSync
? createWebRTCConnection( {
signaling: [
// @ts-ignore
window?.wp?.ajax?.settings?.url,
//'ws://localhost:4444',
],
// @ts-ignore
password: window?.__experimentalCollaborativeEditingSecret,
} )
: null;
syncProvider = createSyncProvider(
// connectIndexDb,
null,
// @ts-ignore
window?.__experimentalEnableWebrtcSync ? createWebRTCConnection( {
signaling: [
// @ts-ignore
window?.wp?.ajax?.settings?.url,
//'ws://localhost:4444',
],
// @ts-ignore
password: window?.__experimentalCollaborativeEditingSecret,
} ) : null
connectionProvider
);
}
return syncProvider;
Expand Down
5 changes: 3 additions & 2 deletions packages/sync/src/provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export const createSyncProvider = ( connectLocal, connectRemote ) => {
const docs = {};

// @ts-ignore
if (window.__experimentalEnableHeartbeatSync) {
if ( window.__experimentalEnableHeartbeatSync ) {
addAction( 'heartbeat.tick', 'y-sync', ( data ) => {
if ( ! data[ 'y-sync' ] ) {
return;
Expand All @@ -46,7 +46,8 @@ export const createSyncProvider = ( connectLocal, connectRemote ) => {
Object.entries( objectDocs ).forEach(
( [ objectId, remoteDocDef ] ) => {
const localDocDef =
( docs[ objectType ] || {} )[ objectId ] || null;
( docs[ objectType ] || {} )[ objectId ] ||
null;
if ( localDocDef ) {
Y.applyUpdateV2(
localDocDef.ydoc,
Expand Down
5 changes: 4 additions & 1 deletion packages/sync/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,8 @@
"compilerOptions": {
"types": [ "node" ]
},
"references": [ { "path": "../url" } ]
"references": [
{ "path": "../url" },
{ "path": "../hooks" }
]
}

0 comments on commit 54dda4d

Please sign in to comment.