@@ -13,6 +13,11 @@ import {
1313 CRDT_RECORD_METADATA_MAP_KEY as RECORD_METADATA_KEY ,
1414 CRDT_RECORD_METADATA_SAVED_AT_KEY as SAVED_AT_KEY ,
1515} from './config' ;
16+ import {
17+ logPerformanceTiming ,
18+ passThru ,
19+ yieldToEventLoop ,
20+ } from './performance' ;
1621import { createPersistedCRDTDoc , getPersistedCrdtDoc } from './persistence' ;
1722import { getProviderCreators } from './providers' ;
1823import type {
@@ -50,12 +55,28 @@ interface EntityState {
5055 ydoc : CRDTDoc ;
5156}
5257
58+ /**
59+ * Get the entity ID for the given object type and object ID.
60+ *
61+ * @param {ObjectType } objectType Object type.
62+ * @param {ObjectID|null } objectId Object ID.
63+ */
64+ function getEntityId (
65+ objectType : ObjectType ,
66+ objectId : ObjectID | null
67+ ) : EntityID {
68+ return `${ objectType } _${ objectId } ` ;
69+ }
70+
5371/**
5472 * The sync manager orchestrates the lifecycle of syncing entity records. It
5573 * creates Yjs documents, connects to providers, creates awareness instances,
5674 * and coordinates with the `core-data` store.
75+ *
76+ * @param debug Whether to enable performance and debug logging.
5777 */
58- export function createSyncManager ( ) : SyncManager {
78+ export function createSyncManager ( debug = false ) : SyncManager {
79+ const debugWrap = debug ? logPerformanceTiming : passThru ;
5980 const collectionStates : Map < ObjectType , CollectionState > = new Map ( ) ;
6081 const entityStates : Map < EntityID , EntityState > = new Map ( ) ;
6182
@@ -118,6 +139,15 @@ export function createSyncManager(): SyncManager {
118139 return ; // Already bootstrapped.
119140 }
120141
142+ handlers = {
143+ addUndoMeta : debugWrap ( handlers . addUndoMeta ) ,
144+ editRecord : debugWrap ( handlers . editRecord ) ,
145+ getEditedRecord : debugWrap ( handlers . getEditedRecord ) ,
146+ refetchRecord : debugWrap ( handlers . refetchRecord ) ,
147+ restoreUndoMeta : debugWrap ( handlers . restoreUndoMeta ) ,
148+ saveRecord : debugWrap ( handlers . saveRecord ) ,
149+ } ;
150+
121151 const ydoc = createYjsDoc ( { objectType } ) ;
122152 const recordMap = ydoc . getMap ( RECORD_KEY ) ;
123153 const recordMetaMap = ydoc . getMap ( RECORD_METADATA_KEY ) ;
@@ -148,7 +178,7 @@ export function createSyncManager(): SyncManager {
148178 return ;
149179 }
150180
151- void updateEntityRecord ( objectType , objectId ) ;
181+ void internal . updateEntityRecord ( objectType , objectId ) ;
152182 } ;
153183
154184 const onRecordMetaUpdate = (
@@ -208,7 +238,7 @@ export function createSyncManager(): SyncManager {
208238 recordMetaMap . observe ( onRecordMetaUpdate ) ;
209239
210240 // Get and apply the persisted CRDT document, if it exists.
211- applyPersistedCrdtDoc ( objectType , objectId , record ) ;
241+ internal . applyPersistedCrdtDoc ( objectType , objectId , record ) ;
212242 }
213243
214244 /**
@@ -308,19 +338,6 @@ export function createSyncManager(): SyncManager {
308338 updateCRDTDoc ( objectType , null , { } , origin , { isSave : true } ) ;
309339 }
310340
311- /**
312- * Get the entity ID for the given object type and object ID.
313- *
314- * @param {ObjectType } objectType Object type.
315- * @param {ObjectID|null } objectId Object ID.
316- */
317- function getEntityId (
318- objectType : ObjectType ,
319- objectId : ObjectID | null
320- ) : EntityID {
321- return `${ objectType } _${ objectId } ` ;
322- }
323-
324341 /**
325342 * Get the awareness instance for the given object type and object ID, if supported.
326343 *
@@ -352,7 +369,7 @@ export function createSyncManager(): SyncManager {
352369 * @param {ObjectID } objectId Object ID.
353370 * @param {ObjectData } record Entity record representing this object type.
354371 */
355- function applyPersistedCrdtDoc (
372+ function _applyPersistedCrdtDoc (
356373 objectType : ObjectType ,
357374 objectId : ObjectID ,
358375 record : ObjectData
@@ -505,7 +522,7 @@ export function createSyncManager(): SyncManager {
505522 * @param {ObjectType } objectType Object type of record to update.
506523 * @param {ObjectID } objectId Object ID of record to update.
507524 */
508- async function updateEntityRecord (
525+ async function _updateEntityRecord (
509526 objectType : ObjectType ,
510527 objectId : ObjectID
511528 ) : Promise < void > {
@@ -556,16 +573,23 @@ export function createSyncManager(): SyncManager {
556573 return createPersistedCRDTDoc ( entityState . ydoc ) ;
557574 }
558575
576+ // Collect internal functions so that they can be wrapped before calling.
577+ const internal = {
578+ applyPersistedCrdtDoc : debugWrap ( _applyPersistedCrdtDoc ) ,
579+ updateEntityRecord : debugWrap ( _updateEntityRecord ) ,
580+ } ;
581+
582+ // Wrap and return the public API.
559583 return {
560- createMeta : createEntityMeta ,
584+ createMeta : debugWrap ( createEntityMeta ) ,
561585 getAwareness,
562- load : loadEntity ,
563- loadCollection,
586+ load : debugWrap ( loadEntity ) ,
587+ loadCollection : debugWrap ( loadCollection ) ,
564588 // Use getter to ensure we always return the current value of `undoManager`.
565589 get undoManager ( ) : SyncUndoManager | undefined {
566590 return undoManager ;
567591 } ,
568- unload : unloadEntity ,
569- update : updateCRDTDoc ,
592+ unload : debugWrap ( unloadEntity ) ,
593+ update : debugWrap ( yieldToEventLoop ( updateCRDTDoc ) ) ,
570594 } ;
571595}
0 commit comments