From f23efa570558833342201f9827ef63cd5de8a797 Mon Sep 17 00:00:00 2001 From: chriszarate Date: Sun, 22 Feb 2026 22:55:39 -0500 Subject: [PATCH] Merge document meta into state map --- packages/core-data/src/sync.ts | 2 -- packages/sync/src/config.ts | 19 +++++++------------ packages/sync/src/manager.ts | 24 ++++++++++++------------ packages/sync/src/private-apis.ts | 8 -------- packages/sync/src/test/manager.ts | 18 +++++++++--------- packages/sync/src/test/utils.ts | 10 +++------- packages/sync/src/utils.ts | 11 +++++------ 7 files changed, 36 insertions(+), 56 deletions(-) diff --git a/packages/core-data/src/sync.ts b/packages/core-data/src/sync.ts index 61608e01f1b19e..ad06e920851ac6 100644 --- a/packages/core-data/src/sync.ts +++ b/packages/core-data/src/sync.ts @@ -17,7 +17,6 @@ const { CRDT_DOC_META_PERSISTENCE_KEY, CRDT_RECORD_MAP_KEY, LOCAL_EDITOR_ORIGIN, - LOCAL_SYNC_MANAGER_ORIGIN, WORDPRESS_META_KEY_FOR_CRDT_DOC_PERSISTENCE, } = unlock( syncPrivateApis ); @@ -26,7 +25,6 @@ export { CRDT_DOC_META_PERSISTENCE_KEY, CRDT_RECORD_MAP_KEY, LOCAL_EDITOR_ORIGIN, - LOCAL_SYNC_MANAGER_ORIGIN, WORDPRESS_META_KEY_FOR_CRDT_DOC_PERSISTENCE, }; diff --git a/packages/sync/src/config.ts b/packages/sync/src/config.ts index a40e2816841956..c7533b3688cfe5 100644 --- a/packages/sync/src/config.ts +++ b/packages/sync/src/config.ts @@ -18,31 +18,26 @@ export const CRDT_DOC_META_PERSISTENCE_KEY = 'fromPersistence'; export const CRDT_RECORD_MAP_KEY = 'document'; /** - * Root-level key for the map that holds entity record metadata. This map should - * only contain metadata that is not represented by the entity record itself. + * Root-level key for the map that holds the state information about the CRDT + * document itself. */ -export const CRDT_RECORD_METADATA_MAP_KEY = 'documentMeta'; +export const CRDT_STATE_MAP_KEY = 'state'; /** * Y.Map key representing the timestamp of the last save operation. */ -export const CRDT_RECORD_METADATA_SAVED_AT_KEY = 'savedAt'; +export const CRDT_STATE_MAP_SAVED_AT_KEY = 'savedAt'; /** * Y.Map key representing the Y.Doc client ID of the user who performed the last * save operation. */ -export const CRDT_RECORD_METADATA_SAVED_BY_KEY = 'savedBy'; +export const CRDT_STATE_MAP_SAVED_BY_KEY = 'savedBy'; /** - * Root-level key for the map that holds the state information about the CRDT - * document itself. It should not contain information related to the entity - * record. + * Y.Map key representing the version of the CRDT document schema. */ -export const CRDT_STATE_MAP_KEY = 'state'; - -// Y.Map keys for the state map. -export const CRDT_STATE_VERSION_KEY = 'version'; +export const CRDT_STATE_MAP_VERSION_KEY = 'version'; /** * Origin string for CRDT document changes originating from the local editor. diff --git a/packages/sync/src/manager.ts b/packages/sync/src/manager.ts index cedf49bd79adf0..b42766b59434b6 100644 --- a/packages/sync/src/manager.ts +++ b/packages/sync/src/manager.ts @@ -10,8 +10,8 @@ import type { Awareness } from 'y-protocols/awareness'; import { CRDT_RECORD_MAP_KEY as RECORD_KEY, LOCAL_SYNC_MANAGER_ORIGIN, - CRDT_RECORD_METADATA_MAP_KEY as RECORD_METADATA_KEY, - CRDT_RECORD_METADATA_SAVED_AT_KEY as SAVED_AT_KEY, + CRDT_STATE_MAP_KEY, + CRDT_STATE_MAP_SAVED_AT_KEY as SAVED_AT_KEY, } from './config'; import { logPerformanceTiming, @@ -151,7 +151,7 @@ export function createSyncManager( debug = false ): SyncManager { const ydoc = createYjsDoc( { objectType } ); const recordMap = ydoc.getMap( RECORD_KEY ); - const recordMetaMap = ydoc.getMap( RECORD_METADATA_KEY ); + const stateMap = ydoc.getMap( CRDT_STATE_MAP_KEY ); const now = Date.now(); // Clean up providers and in-memory state when the entity is unloaded. @@ -159,7 +159,7 @@ export function createSyncManager( debug = false ): SyncManager { providerResults.forEach( ( result ) => result.destroy() ); handlers.onStatusChange( null ); recordMap.unobserveDeep( onRecordUpdate ); - recordMetaMap.unobserve( onRecordMetaUpdate ); + stateMap.unobserve( onStateMapUpdate ); ydoc.destroy(); entityStates.delete( entityId ); }; @@ -183,7 +183,7 @@ export function createSyncManager( debug = false ): SyncManager { void internal.updateEntityRecord( objectType, objectId ); }; - const onRecordMetaUpdate = ( + const onStateMapUpdate = ( event: Y.YMapEvent< unknown >, transaction: Y.Transaction ) => { @@ -194,7 +194,7 @@ export function createSyncManager( debug = false ): SyncManager { event.keysChanged.forEach( ( key ) => { switch ( key ) { case SAVED_AT_KEY: - const newValue = recordMetaMap.get( SAVED_AT_KEY ); + const newValue = stateMap.get( SAVED_AT_KEY ); if ( 'number' === typeof newValue && newValue > now ) { // Another peer has saved the record. Refetch it so that we have // a correct understanding of our own unsaved edits. @@ -247,7 +247,7 @@ export function createSyncManager( debug = false ): SyncManager { // Attach observers. recordMap.observeDeep( onRecordUpdate ); - recordMetaMap.observe( onRecordMetaUpdate ); + stateMap.observe( onStateMapUpdate ); // Get and apply the persisted CRDT document, if it exists. internal.applyPersistedCrdtDoc( objectType, objectId, record ); @@ -276,19 +276,19 @@ export function createSyncManager( debug = false ): SyncManager { } const ydoc = createYjsDoc( { collection: true, objectType } ); - const recordMetaMap = ydoc.getMap( RECORD_METADATA_KEY ); + const stateMap = ydoc.getMap( CRDT_STATE_MAP_KEY ); const now = Date.now(); // Clean up providers and in-memory state when the entity is unloaded. const unload = (): void => { providerResults.forEach( ( result ) => result.destroy() ); handlers.onStatusChange( null ); - recordMetaMap.unobserve( onRecordMetaUpdate ); + stateMap.unobserve( onStateMapUpdate ); ydoc.destroy(); collectionStates.delete( objectType ); }; - const onRecordMetaUpdate = ( + const onStateMapUpdate = ( event: Y.YMapEvent< unknown >, transaction: Y.Transaction ) => { @@ -299,7 +299,7 @@ export function createSyncManager( debug = false ): SyncManager { event.keysChanged.forEach( ( key ) => { switch ( key ) { case SAVED_AT_KEY: - const newValue = recordMetaMap.get( SAVED_AT_KEY ); + const newValue = stateMap.get( SAVED_AT_KEY ); if ( 'number' === typeof newValue && newValue > now ) { // Another peer has mutated the collection. Refetch it so that we // obtain the updated records. @@ -341,7 +341,7 @@ export function createSyncManager( debug = false ): SyncManager { ); // Attach observers. - recordMetaMap.observe( onRecordMetaUpdate ); + stateMap.observe( onStateMapUpdate ); } /** diff --git a/packages/sync/src/private-apis.ts b/packages/sync/src/private-apis.ts index cccd6d807d66d4..c78d2e84101eb8 100644 --- a/packages/sync/src/private-apis.ts +++ b/packages/sync/src/private-apis.ts @@ -4,11 +4,7 @@ import { CRDT_DOC_META_PERSISTENCE_KEY, CRDT_RECORD_MAP_KEY, - CRDT_RECORD_METADATA_MAP_KEY, - CRDT_RECORD_METADATA_SAVED_AT_KEY, - CRDT_RECORD_METADATA_SAVED_BY_KEY, LOCAL_EDITOR_ORIGIN, - LOCAL_SYNC_MANAGER_ORIGIN, WORDPRESS_META_KEY_FOR_CRDT_DOC_PERSISTENCE, } from './config'; import { lock } from './lock-unlock'; @@ -22,10 +18,6 @@ lock( privateApis, { Delta, CRDT_DOC_META_PERSISTENCE_KEY, CRDT_RECORD_MAP_KEY, - CRDT_RECORD_METADATA_MAP_KEY, - CRDT_RECORD_METADATA_SAVED_AT_KEY, - CRDT_RECORD_METADATA_SAVED_BY_KEY, LOCAL_EDITOR_ORIGIN, - LOCAL_SYNC_MANAGER_ORIGIN, WORDPRESS_META_KEY_FOR_CRDT_DOC_PERSISTENCE, } ); diff --git a/packages/sync/src/test/manager.ts b/packages/sync/src/test/manager.ts index 45074993cb9f78..e65d07475a30a0 100644 --- a/packages/sync/src/test/manager.ts +++ b/packages/sync/src/test/manager.ts @@ -19,9 +19,9 @@ import { import { createSyncManager } from '../manager'; import { CRDT_RECORD_MAP_KEY, - CRDT_RECORD_METADATA_MAP_KEY as RECORD_METADATA_MAP_KEY, - CRDT_RECORD_METADATA_SAVED_AT_KEY as SAVED_AT_KEY, - CRDT_RECORD_METADATA_SAVED_BY_KEY as SAVED_BY_KEY, + CRDT_STATE_MAP_KEY, + CRDT_STATE_MAP_SAVED_AT_KEY as SAVED_AT_KEY, + CRDT_STATE_MAP_SAVED_BY_KEY as SAVED_BY_KEY, } from '../config'; import { createPersistedCRDTDoc } from '../persistence'; import { getProviderCreators } from '../providers'; @@ -524,9 +524,9 @@ describe( 'SyncManager', () => { // Verify that the record metadata was not updated. const ydoc = capturedDoc as unknown as Y.Doc; - const metadataMap = ydoc.getMap( RECORD_METADATA_MAP_KEY ); - expect( metadataMap.get( SAVED_AT_KEY ) ).toBeUndefined(); - expect( metadataMap.get( SAVED_BY_KEY ) ).toBeUndefined(); + const stateMap = ydoc.getMap( CRDT_STATE_MAP_KEY ); + expect( stateMap.get( SAVED_AT_KEY ) ).toBeUndefined(); + expect( stateMap.get( SAVED_BY_KEY ) ).toBeUndefined(); } ); it( 'does not update when entity is not loaded', async () => { @@ -622,11 +622,11 @@ describe( 'SyncManager', () => { // Verify that the record metadata was updated. const ydoc = capturedDoc as unknown as Y.Doc; - const metadataMap = ydoc.getMap( RECORD_METADATA_MAP_KEY ); - expect( metadataMap.get( SAVED_AT_KEY ) ).toBeGreaterThanOrEqual( + const stateMap = ydoc.getMap( CRDT_STATE_MAP_KEY ); + expect( stateMap.get( SAVED_AT_KEY ) ).toBeGreaterThanOrEqual( now ); - expect( metadataMap.get( SAVED_BY_KEY ) ).toBe( ydoc.clientID ); + expect( stateMap.get( SAVED_BY_KEY ) ).toBe( ydoc.clientID ); } ); } ); diff --git a/packages/sync/src/test/utils.ts b/packages/sync/src/test/utils.ts index 886fcdba994840..ae6e6652822f8d 100644 --- a/packages/sync/src/test/utils.ts +++ b/packages/sync/src/test/utils.ts @@ -13,7 +13,7 @@ import { CRDT_DOC_META_PERSISTENCE_KEY, CRDT_DOC_VERSION, CRDT_STATE_MAP_KEY, - CRDT_STATE_VERSION_KEY, + CRDT_STATE_MAP_VERSION_KEY as VERSION_KEY, } from '../config'; describe( 'utils', () => { @@ -44,9 +44,7 @@ describe( 'utils', () => { const ydoc = createYjsDoc( {} ); const stateMap = ydoc.getMap( CRDT_STATE_MAP_KEY ); - expect( stateMap.get( CRDT_STATE_VERSION_KEY ) ).toBe( - CRDT_DOC_VERSION - ); + expect( stateMap.get( VERSION_KEY ) ).toBe( CRDT_DOC_VERSION ); } ); } ); @@ -142,9 +140,7 @@ describe( 'utils', () => { expect( deserialized ).toBeInstanceOf( Y.Doc ); const stateMap = deserialized!.getMap( CRDT_STATE_MAP_KEY ); - expect( stateMap.get( CRDT_STATE_VERSION_KEY ) ).toBe( - CRDT_DOC_VERSION - ); + expect( stateMap.get( VERSION_KEY ) ).toBe( CRDT_DOC_VERSION ); } ); } ); diff --git a/packages/sync/src/utils.ts b/packages/sync/src/utils.ts index 223fe8a8df6a63..a1b20b471f3b33 100644 --- a/packages/sync/src/utils.ts +++ b/packages/sync/src/utils.ts @@ -10,11 +10,10 @@ import * as buffer from 'lib0/buffer'; import { CRDT_DOC_META_PERSISTENCE_KEY, CRDT_DOC_VERSION, - CRDT_RECORD_METADATA_MAP_KEY as RECORD_METADATA_KEY, - CRDT_RECORD_METADATA_SAVED_AT_KEY as SAVED_AT_KEY, - CRDT_RECORD_METADATA_SAVED_BY_KEY as SAVED_BY_KEY, CRDT_STATE_MAP_KEY, - CRDT_STATE_VERSION_KEY, + CRDT_STATE_MAP_SAVED_AT_KEY as SAVED_AT_KEY, + CRDT_STATE_MAP_SAVED_BY_KEY as SAVED_BY_KEY, + CRDT_STATE_MAP_VERSION_KEY as VERSION_KEY, } from './config'; import type { CRDTDoc } from './types'; @@ -34,7 +33,7 @@ export function createYjsDoc( documentMeta: DocumentMeta = {} ): Y.Doc { const ydoc = new Y.Doc( { meta: metaMap } ); const stateMap = ydoc.getMap( CRDT_STATE_MAP_KEY ); - stateMap.set( CRDT_STATE_VERSION_KEY, CRDT_DOC_VERSION ); + stateMap.set( VERSION_KEY, CRDT_DOC_VERSION ); return ydoc; } @@ -46,7 +45,7 @@ export function createYjsDoc( documentMeta: DocumentMeta = {} ): Y.Doc { * @param {CRDTDoc} ydoc CRDT document. */ export function markEntityAsSaved( ydoc: CRDTDoc ): void { - const recordMeta = ydoc.getMap( RECORD_METADATA_KEY ); + const recordMeta = ydoc.getMap( CRDT_STATE_MAP_KEY ); recordMeta.set( SAVED_AT_KEY, Date.now() ); recordMeta.set( SAVED_BY_KEY, ydoc.clientID ); }