Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Move from using the pre post save fcn to an action run post save
  • Loading branch information
ingeniumed committed Oct 29, 2025
commit 6175b5ff1227c1246f18b095628a70b0e1d400c3
9 changes: 1 addition & 8 deletions packages/core-data/src/entities.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { __ } from '@wordpress/i18n';
/**
* Internal dependencies
*/
import { LOCAL_EDITOR_ORIGIN, getSyncManager } from './sync';
import { getSyncManager } from './sync';
import {
applyPostChangesToCRDTDoc,
defaultApplyChangesToCRDTDoc,
Expand Down Expand Up @@ -280,13 +280,6 @@ export const prePersistPostType = (
...edits.meta,
...meta,
};

// Mark the doc as having been persisted.
getSyncManager()?.markPersisted(
objectType,
objectId,
LOCAL_EDITOR_ORIGIN
);
}
}

Expand Down
20 changes: 20 additions & 0 deletions packages/sync/src/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
*/
import * as Y from 'yjs';

/**
* WordPress dependencies
*/
import { addAction } from '@wordpress/hooks';

/**
* Internal dependencies
*/
Expand All @@ -11,6 +16,7 @@ import {
LOCAL_SYNC_MANAGER_ORIGIN,
CRDT_STATE_PERSISTED_AT_KEY as PERSISTED_AT_KEY,
CRDT_STATE_PERSISTED_BY_KEY as PERSISTED_BY_KEY,
LOCAL_EDITOR_ORIGIN,
} from './config';
import { createPersistedCRDTDoc, getPersistedCrdtDoc } from './persistence';
import { getProviderCreators } from './providers';
Expand Down Expand Up @@ -147,6 +153,20 @@ export function createSyncManager(): SyncManager {
handlers.editRecord( { meta } );
handlers.saveRecord();
}

addAction(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@chriszarate - lemme know what you think about this new way to track a post being saved. This action is fired async at the very end of a post save, so we know it's been persisted.

Now, it's possible to actually use this key to track if a refetch needs to happen or stick to just notifications.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I kept this here as it felt like the right place, since it's where the ydoc is bootstrapped. We aren't changing anything about the post just tracking it really.

'editor.savePost',
'sync.markCRDTDocPersisted',
async ( post, options ) => {
// Skip if the post's id doesn't match (including different ID types), or if it's an autosave.
if ( post.id !== objectId || options.isAutosave ) {
Copy link
Contributor Author

@ingeniumed ingeniumed Oct 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I skipped auto saves at the moment, to keep it similar to Gutenberg where a notice would be sent if you hit the save yourself.

The post ID check is for safety, and would account for even the ID format not being supported.

Is it worth adding an extra post type check too?

return;
}

// Mark the CRDT document as having been persisted.
recordPersistence( objectType, objectId, LOCAL_EDITOR_ORIGIN );
}
);
}

/**
Expand Down
Loading