Skip to content

Commit 19bd1d1

Browse files
committed
Use a private SyncManager instead of a SyncProvider class
1 parent 0ed0198 commit 19bd1d1

File tree

12 files changed

+347
-337
lines changed

12 files changed

+347
-337
lines changed

packages/core-data/src/actions.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { v4 as uuid } from 'uuid';
1010
import apiFetch from '@wordpress/api-fetch';
1111
import { addQueryArgs } from '@wordpress/url';
1212
import deprecated from '@wordpress/deprecated';
13-
import { LOCAL_EDITOR_ORIGIN } from '@wordpress/sync';
13+
import { LOCAL_EDITOR_ORIGIN, syncManager } from '@wordpress/sync';
1414

1515
/**
1616
* Internal dependencies
@@ -20,7 +20,6 @@ import { receiveItems, removeItems, receiveQueriedItems } from './queried-data';
2020
import { DEFAULT_ENTITY_KEY } from './entities';
2121
import { createBatch } from './batch';
2222
import { STORE_NAME } from './name';
23-
import { getSyncProvider } from './sync';
2423
import logEntityDeprecation from './utils/log-entity-deprecation';
2524

2625
/**
@@ -414,7 +413,7 @@ export const editEntityRecord =
414413
const objectType = `${ kind }/${ name }`;
415414
const objectId = recordId;
416415

417-
getSyncProvider().updateCRDTDoc(
416+
syncManager.update(
418417
objectType,
419418
objectId,
420419
edit.edits,

packages/core-data/src/resolvers.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { camelCase } from 'change-case';
99
import { addQueryArgs } from '@wordpress/url';
1010
import { decodeEntities } from '@wordpress/html-entities';
1111
import apiFetch from '@wordpress/api-fetch';
12+
import { syncManager } from '@wordpress/sync';
1213

1314
/**
1415
* Internal dependencies
@@ -23,7 +24,6 @@ import {
2324
ALLOWED_RESOURCE_ACTIONS,
2425
RECEIVE_INTERMEDIATE_RESULTS,
2526
} from './utils';
26-
import { getSyncProvider } from './sync';
2727
import { fetchBlockPatterns } from './fetch';
2828

2929
/**
@@ -185,7 +185,7 @@ export const getEntityRecord =
185185
} );
186186

187187
// Bootstraps the edited document (and load from peers).
188-
await getSyncProvider().bootstrap(
188+
await syncManager.load(
189189
entityConfig.syncConfig,
190190
objectType,
191191
objectId,

packages/core-data/src/sync.js

Lines changed: 0 additions & 27 deletions
This file was deleted.

packages/core-data/src/test/resolvers.js

Lines changed: 17 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22
* WordPress dependencies
33
*/
44
import triggerFetch from '@wordpress/api-fetch';
5+
import { syncManager } from '@wordpress/sync';
56

67
jest.mock( '@wordpress/api-fetch' );
7-
8-
// Mock the sync provider
9-
jest.mock( '../sync', () => ( {
10-
getSyncProvider: jest.fn(),
8+
jest.mock( '@wordpress/sync', () => ( {
9+
syncManager: {
10+
load: jest.fn(),
11+
},
1112
} ) );
1213

1314
/**
@@ -21,7 +22,6 @@ import {
2122
getAutosaves,
2223
getCurrentUser,
2324
} from '../resolvers';
24-
import { getSyncProvider } from '../sync';
2525

2626
describe( 'getEntityRecord', () => {
2727
const POST_TYPE = { slug: 'post' };
@@ -47,7 +47,7 @@ describe( 'getEntityRecord', () => {
4747
finishResolutions: jest.fn(),
4848
} );
4949
triggerFetch.mockReset();
50-
getSyncProvider.mockClear();
50+
syncManager.load.mockClear();
5151
} );
5252

5353
afterEach( () => {
@@ -123,7 +123,7 @@ describe( 'getEntityRecord', () => {
123123
);
124124
} );
125125

126-
it( 'bootstraps entity with sync provider when __experimentalEnableSync is true', async () => {
126+
it( 'loads entity with sync provider when __experimentalEnableSync is true', async () => {
127127
const POST_RECORD = { id: 1, title: 'Test Post' };
128128
const POST_RESPONSE = {
129129
json: () => Promise.resolve( POST_RECORD ),
@@ -140,12 +140,6 @@ describe( 'getEntityRecord', () => {
140140

141141
window.__experimentalEnableSync = true;
142142

143-
const mockBootstrap = jest.fn();
144-
getSyncProvider.mockReturnValue( {
145-
bootstrap: mockBootstrap,
146-
register: jest.fn(),
147-
} );
148-
149143
const resolveSelectWithSync = {
150144
getEntitiesConfig: jest.fn( () => ENTITIES_WITH_SYNC ),
151145
getEditedEntityRecord: jest.fn(),
@@ -163,10 +157,9 @@ describe( 'getEntityRecord', () => {
163157
resolveSelect: resolveSelectWithSync,
164158
} );
165159

166-
// Verify bootstrap was called with correct arguments.
167-
expect( getSyncProvider ).toHaveBeenCalled();
168-
expect( mockBootstrap ).toHaveBeenCalledTimes( 1 );
169-
expect( mockBootstrap ).toHaveBeenCalledWith(
160+
// Verify load was called with correct arguments.
161+
expect( syncManager.load ).toHaveBeenCalledTimes( 1 );
162+
expect( syncManager.load ).toHaveBeenCalledWith(
170163
{},
171164
'postType/post',
172165
1,
@@ -197,12 +190,6 @@ describe( 'getEntityRecord', () => {
197190

198191
window.__experimentalEnableSync = true;
199192

200-
const mockBootstrap = jest.fn();
201-
getSyncProvider.mockReturnValue( {
202-
bootstrap: mockBootstrap,
203-
register: jest.fn(),
204-
} );
205-
206193
const resolveSelectWithSync = {
207194
getEntitiesConfig: jest.fn( () => ENTITIES_WITH_SYNC ),
208195
getEditedEntityRecord: jest.fn(),
@@ -220,10 +207,9 @@ describe( 'getEntityRecord', () => {
220207
resolveSelect: resolveSelectWithSync,
221208
} );
222209

223-
// Verify bootstrap was called with correct arguments.
224-
expect( getSyncProvider ).toHaveBeenCalled();
225-
expect( mockBootstrap ).toHaveBeenCalledTimes( 1 );
226-
expect( mockBootstrap ).toHaveBeenCalledWith(
210+
// Verify load was called with correct arguments.
211+
expect( syncManager.load ).toHaveBeenCalledTimes( 1 );
212+
expect( syncManager.load ).toHaveBeenCalledWith(
227213
{},
228214
'postType/post',
229215
1,
@@ -232,7 +218,7 @@ describe( 'getEntityRecord', () => {
232218
);
233219
} );
234220

235-
it( 'does not bootstrap entity when query is present', async () => {
221+
it( 'does not load entity when query is present', async () => {
236222
const POST_RECORD = { id: 1, title: 'Test Post' };
237223
const POST_RESPONSE = {
238224
json: () => Promise.resolve( POST_RECORD ),
@@ -262,10 +248,10 @@ describe( 'getEntityRecord', () => {
262248
resolveSelect: resolveSelectWithSync,
263249
} );
264250

265-
expect( getSyncProvider ).not.toHaveBeenCalled();
251+
expect( syncManager.load ).not.toHaveBeenCalled();
266252
} );
267253

268-
it( 'does not bootstrap entity when __experimentalEnableSync is undefined', async () => {
254+
it( 'does not load entity when __experimentalEnableSync is undefined', async () => {
269255
const POST_RECORD = { id: 1, title: 'Test Post' };
270256
const POST_RESPONSE = {
271257
json: () => Promise.resolve( POST_RECORD ),
@@ -296,7 +282,7 @@ describe( 'getEntityRecord', () => {
296282
resolveSelect: resolveSelectWithSync,
297283
} );
298284

299-
expect( getSyncProvider ).not.toHaveBeenCalled();
285+
expect( syncManager.load ).not.toHaveBeenCalled();
300286
} );
301287
} );
302288

packages/sync/README.md

Lines changed: 3 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -14,45 +14,17 @@ npm install @wordpress/sync --save
1414

1515
<!-- START TOKEN(Autogenerated API docs) -->
1616

17-
### connectIndexDb
18-
19-
Connect function to the IndexedDB persistence provider.
20-
21-
_Parameters_
22-
23-
- _objectId_ `ObjectID`: The object ID.
24-
- _objectType_ `ObjectType`: The object type.
25-
- _doc_ `CRDTDoc`: The CRDT document.
26-
27-
_Returns_
28-
29-
- `Promise< ConnectDocResult >`: Promise that resolves when the connection is established.
30-
3117
### CRDT_RECORD_MAP_KEY
3218

3319
Root-level key for the CRDT document that holds the entity record data.
3420

35-
### createWebRTCConnection
36-
37-
Function that creates a new WebRTC Connection.
38-
39-
_Parameters_
40-
41-
- _config_ `Object`:
42-
- _config.signaling_ `Array<string>`:
43-
- _config.password_ `string|undefined`:
44-
45-
_Returns_
46-
47-
- `ConnectDoc`: Promise that resolves when the connection is established.
48-
49-
### SyncProvider
21+
### syncManager
5022

51-
SyncProvider manages the lifecycle of syncing entity records. It establishes connections, creates the awareness instance, and coordinates with the local store.
23+
The sync manager orchestrates the lifecycle of syncing entity records. It creates Yjs documents, connects to providers, creates awareness instances, and coordinates with the `core-data` store.
5224

5325
_Type_
5426

55-
- `SyncProvider`
27+
- `SyncManager`
5628

5729
### Y
5830

packages/sync/src/connect-indexdb.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,19 @@ import { IndexeddbPersistence } from 'y-indexeddb';
77
/** @typedef {import('./types').ObjectType} ObjectType */
88
/** @typedef {import('./types').ObjectID} ObjectID */
99
/** @typedef {import('./types').CRDTDoc} CRDTDoc */
10-
/** @typedef {import('./types').ConnectDoc} ConnectDoc */
11-
/** @typedef {import('./types').ConnectDocResult} ConnectDocResult */
10+
/** @typedef {import('./types').ProviderCreator} ProviderCreator */
11+
/** @typedef {import('./types').ProviderCreatorResult} ProviderCreatorResult */
1212

1313
/**
1414
* Connect function to the IndexedDB persistence provider.
1515
*
16-
* @param {ObjectID} objectId The object ID.
1716
* @param {ObjectType} objectType The object type.
17+
* @param {ObjectID} objectId The object ID.
1818
* @param {CRDTDoc} doc The CRDT document.
1919
*
20-
* @return {Promise< ConnectDocResult >} Promise that resolves when the connection is established.
20+
* @return {Promise< ProviderCreatorResult >} Promise that resolves when the connection is established.
2121
*/
22-
export function connectIndexDb( objectId, objectType, doc ) {
22+
export function connectIndexDb( objectType, objectId, doc ) {
2323
const roomName = `${ objectType }-${ objectId }`;
2424
const provider = new IndexeddbPersistence( roomName, doc );
2525

packages/sync/src/create-webrtc-connection.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,21 @@ import { WebrtcProviderWithHttpSignaling } from './webrtc-http-stream-signaling'
1010

1111
/** @typedef {import('./types').ObjectType} ObjectType */
1212
/** @typedef {import('./types').ObjectID} ObjectID */
13-
/** @typedef {import('./types').ConnectDoc} ConnectDoc */
1413
/** @typedef {import('./types').CRDTDoc} CRDTDoc */
14+
/** @typedef {import('./types').ProviderCreator} ProviderCreator */
1515

1616
/**
1717
* Function that creates a new WebRTC Connection.
1818
*
1919
* @param {Object} config
2020
* @param {Array<string>} config.signaling
2121
* @param {string|undefined} config.password
22-
* @return {ConnectDoc} Promise that resolves when the connection is established.
22+
* @return {ProviderCreator} Promise that resolves when the connection is established.
2323
*/
2424
export function createWebRTCConnection( { signaling, password } ) {
2525
return function (
26-
/** @type {ObjectID} */ objectId,
2726
/** @type {ObjectType} */ objectType,
27+
/** @type {ObjectID} */ objectId,
2828
/** @type {CRDTDoc} */ doc
2929
) {
3030
const roomName = `${ objectType }-${ objectId }`;

packages/sync/src/index.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
*/
1212
export * as Y from 'yjs';
1313

14-
export { CRDT_RECORD_MAP_KEY } from './config';
15-
export { connectIndexDb } from './connect-indexdb';
16-
export { createWebRTCConnection } from './create-webrtc-connection';
17-
export { SyncProvider } from './provider';
14+
export { CRDT_RECORD_MAP_KEY, LOCAL_EDITOR_ORIGIN } from './config';
15+
export { syncManager } from './manager';
1816
export type * from './types';

0 commit comments

Comments
 (0)