@@ -8,17 +8,16 @@ import type { Awareness } from 'y-protocols/awareness';
88 * Internal dependencies
99 */
1010import {
11- CRDT_RECORD_MAP_KEY as RECORD_KEY ,
12- LOCAL_SYNC_MANAGER_ORIGIN ,
11+ CRDT_RECORD_MAP_KEY ,
1312 CRDT_STATE_MAP_KEY ,
1413 CRDT_STATE_MAP_SAVED_AT_KEY as SAVED_AT_KEY ,
14+ LOCAL_SYNC_MANAGER_ORIGIN ,
1515} from './config' ;
1616import {
1717 logPerformanceTiming ,
1818 passThru ,
1919 yieldToEventLoop ,
2020} from './performance' ;
21- import { createPersistedCRDTDoc , getPersistedCrdtDoc } from './persistence' ;
2221import { getProviderCreators } from './providers' ;
2322import type {
2423 CollectionHandlers ,
@@ -35,7 +34,12 @@ import type {
3534 SyncUndoManager ,
3635} from './types' ;
3736import { createUndoManager } from './undo-manager' ;
38- import { createYjsDoc , markEntityAsSaved } from './utils' ;
37+ import {
38+ createYjsDoc ,
39+ deserializeCrdtDoc ,
40+ markEntityAsSaved ,
41+ serializeCrdtDoc ,
42+ } from './utils' ;
3943
4044interface CollectionState {
4145 awareness ?: Awareness ;
@@ -150,7 +154,7 @@ export function createSyncManager( debug = false ): SyncManager {
150154 } ;
151155
152156 const ydoc = createYjsDoc ( { objectType } ) ;
153- const recordMap = ydoc . getMap ( RECORD_KEY ) ;
157+ const recordMap = ydoc . getMap ( CRDT_RECORD_MAP_KEY ) ;
154158 const stateMap = ydoc . getMap ( CRDT_STATE_MAP_KEY ) ;
155159 const now = Date . now ( ) ;
156160
@@ -404,32 +408,22 @@ export function createSyncManager( debug = false ): SyncManager {
404408 syncConfig : {
405409 applyChangesToCRDTDoc,
406410 getChangesFromCRDTDoc,
407- supports ,
411+ getPersistedCrdtDoc ,
408412 } ,
409413 ydoc : targetDoc ,
410414 } = entityState ;
411415
412- if ( ! supports ?. crdtPersistence ) {
413- // Apply the current record as changes.
414- targetDoc . transact ( ( ) => {
415- applyChangesToCRDTDoc ( targetDoc , record ) ;
416- } , LOCAL_SYNC_MANAGER_ORIGIN ) ;
417- return ;
418- }
419-
420416 // Get the persisted CRDT document, if it exists.
421- const tempDoc = getPersistedCrdtDoc ( record ) ;
417+ const serialized = getPersistedCrdtDoc ?.( record ) ;
418+ const tempDoc = serialized ? deserializeCrdtDoc ( serialized ) : null ;
422419
423420 if ( ! tempDoc ) {
424421 // Apply the current record as changes and trigger a save, which will
425- // persist the CRDT document. (The entity should call `createEntityMeta `
422+ // persist the CRDT document. (The entity should call `createPersistedCRDTDoc `
426423 // via its pre-persist hook.)
427424 targetDoc . transact ( ( ) => {
428425 applyChangesToCRDTDoc ( targetDoc , record ) ;
429-
430- if ( 'auto-draft' !== record . status ) {
431- handlers . saveRecord ( ) ;
432- }
426+ handlers . saveRecord ( ) ;
433427 } , LOCAL_SYNC_MANAGER_ORIGIN ) ;
434428 return ;
435429 }
@@ -477,8 +471,8 @@ export function createSyncManager( debug = false ): SyncManager {
477471 ) ;
478472
479473 // Apply the changes and trigger a save, which will persist the CRDT
480- // document. (The entity should call `createEntityMeta ` via its pre-persist
481- // hook.)
474+ // document. (The entity should call `createPersistedCRDTDoc ` via its
475+ // pre-persist hook.)
482476 targetDoc . transact ( ( ) => {
483477 applyChangesToCRDTDoc ( targetDoc , changes ) ;
484478 handlers . saveRecord ( ) ;
@@ -580,18 +574,18 @@ export function createSyncManager( debug = false ): SyncManager {
580574 * @param {ObjectType } objectType Object type.
581575 * @param {ObjectID } objectId Object ID.
582576 */
583- function createEntityMeta (
577+ function createPersistedCRDTDoc (
584578 objectType : ObjectType ,
585579 objectId : ObjectID
586- ) : Record < string , string > {
580+ ) : string | null {
587581 const entityId = getEntityId ( objectType , objectId ) ;
588582 const entityState = entityStates . get ( entityId ) ;
589583
590- if ( ! entityState ?. syncConfig . supports ?. crdtPersistence ) {
591- return { } ;
584+ if ( ! entityState ?. ydoc ) {
585+ return null ;
592586 }
593587
594- return createPersistedCRDTDoc ( entityState . ydoc ) ;
588+ return serializeCrdtDoc ( entityState . ydoc ) ;
595589 }
596590
597591 // Collect internal functions so that they can be wrapped before calling.
@@ -602,7 +596,7 @@ export function createSyncManager( debug = false ): SyncManager {
602596
603597 // Wrap and return the public API.
604598 return {
605- createMeta : debugWrap ( createEntityMeta ) ,
599+ createPersistedCRDTDoc : debugWrap ( createPersistedCRDTDoc ) ,
606600 getAwareness,
607601 load : debugWrap ( loadEntity ) ,
608602 loadCollection : debugWrap ( loadCollection ) ,
0 commit comments