Skip to content
Merged
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
6 changes: 6 additions & 0 deletions packages/core-data/src/awareness/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,9 @@ export type EqualityFieldCheck< State, FieldName extends keyof State > = (
value1?: State[ FieldName ],
value2?: State[ FieldName ]
) => boolean;

export interface PostSaveEvent {
savedAt: number;
savedByClientId: number;
postStatus: string | undefined;
}
70 changes: 70 additions & 0 deletions packages/core-data/src/hooks/use-post-editor-awareness-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@
* External dependencies
*/
import { useEffect, useState } from '@wordpress/element';
import type { Y } from '@wordpress/sync';

/**
* Internal dependencies
*/
import { getSyncManager } from '../sync';
import type {
PostEditorAwarenessState as ActiveCollaborator,
PostSaveEvent,
YDocDebugData,
} from '../awareness/types';
import type { SelectionState } from '../types';
Expand Down Expand Up @@ -156,3 +158,71 @@ export function useIsDisconnected(
return usePostEditorAwarenessState( postId, postType )
.isCurrentCollaboratorDisconnected;
}

/**
* Hook that subscribes to the CRDT state map and returns the most recent
* save event (timestamp + client ID). The state map is updated by
* `markEntityAsSaved` in `@wordpress/sync`
*
* @param postId The ID of the post.
* @param postType The type of the post.
*/
export function useLastPostSave(
postId: number | null,
postType: string | null
): PostSaveEvent | null {
const [ lastSave, setLastSave ] = useState< PostSaveEvent | null >( null );

useEffect( () => {
if ( null === postId || null === postType ) {
setLastSave( null );
return;
}

const awareness = getSyncManager()?.getAwareness< PostEditorAwareness >(
`postType/${ postType }`,
postId.toString()
);

if ( ! awareness ) {
setLastSave( null );
return;
}

awareness.setUp();

const stateMap = awareness.doc.getMap( 'state' );
const recordMap = awareness.doc.getMap( 'document' );

// Only notify for saves that occur after the observer is
// set up. This prevents false notifications when the Y.Doc
// syncs historical state on page load or peer reconnect.
const setupTime = Date.now();

const observer = ( event: Y.YMapEvent< unknown > ) => {
if ( event.keysChanged.has( 'savedAt' ) ) {
const savedAt = stateMap.get( 'savedAt' ) as number;
const savedByClientId = stateMap.get( 'savedBy' ) as number;

if (
typeof savedAt === 'number' &&
typeof savedByClientId === 'number' &&
savedAt > setupTime
) {
const postStatus = recordMap.get( 'status' ) as
| string
| undefined;
setLastSave( { savedAt, savedByClientId, postStatus } );
}
}
};

stateMap.observe( observer );

return () => {
stateMap.unobserve( observer );
};
}, [ postId, postType ] );

return lastSave;
}
2 changes: 2 additions & 0 deletions packages/core-data/src/private-apis.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { RECEIVE_INTERMEDIATE_RESULTS } from './utils';
import {
useActiveCollaborators,
useResolvedSelection,
useLastPostSave,
} from './hooks/use-post-editor-awareness-state';
import { lock } from './lock-unlock';
import { retrySyncConnection } from './sync';
Expand All @@ -17,4 +18,5 @@ lock( privateApis, {
retrySyncConnection,
useActiveCollaborators,
useResolvedSelection,
useLastPostSave,
} );
Loading
Loading