Skip to content

Commit b043d7f

Browse files
committed
Support only one provider per document for now
1 parent ff25e22 commit b043d7f

File tree

2 files changed

+19
-38
lines changed

2 files changed

+19
-38
lines changed

javascript/src/api.ts

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -91,14 +91,14 @@ export interface IDocumentProvider extends IDisposable {
9191
readonly ready: Promise<void>;
9292

9393
/**
94-
* Request to fork the room in the backend, and connect the shared document to the forked room.
94+
* Request to fork the room in the backend, returns the fork ID.
9595
*/
96-
fork(): Promise<void>;
96+
fork(): Promise<string>;
9797

9898
/**
99-
* Connect a shared document to a forked room with forkId and return a document provider.
99+
* Connect the shared document to a forked room with forkId (disconnect from previous room).
100100
*/
101-
connectFork(forkId: string, sharedDocument: ISharedDocument): IDocumentProvider;
101+
connectFork(forkId: string): void;
102102
}
103103

104104
/**
@@ -137,19 +137,9 @@ export interface ISharedDocument extends ISharedBase {
137137
readonly changed: ISignal<this, DocumentChange>;
138138

139139
/**
140-
* Get a provider with a given ID
141-
*
142-
* @param providerId The provider ID
143-
*/
144-
getProvider(providerId: string, sharedModel?: ISharedDocument): IDocumentProvider;
145-
146-
/**
147-
* Set a provider for this document
148-
*
149-
* @param providerId Provider ID (either 'root' or a UUID)
150-
* @param provider The document provider
140+
* The document's provider.
151141
*/
152-
setProvider(providerId: string, provider: IDocumentProvider): void;
142+
provider: IDocumentProvider;
153143

154144
/**
155145
* Add a fork ID to the document's ystate, as a new key 'fork_{forkId}'
@@ -159,9 +149,9 @@ export interface ISharedDocument extends ISharedBase {
159149
addFork(forkId: string): void;
160150

161151
/**
162-
* The document fork ID
152+
* The document room ID
163153
*/
164-
forkId: string;
154+
roomId: string;
165155
}
166156

167157
/**

javascript/src/ydocument.ts

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export abstract class YDocument<T extends DocumentChange>
1717
{
1818
constructor(options?: YDocument.IOptions) {
1919
this._ydoc = options?.ydoc ?? new Y.Doc();
20-
this.forkId = options?.forkId ?? 'root';
20+
this.roomId = options?.roomId ?? '';
2121

2222
this._ystate = this._ydoc.getMap('state');
2323

@@ -29,8 +29,6 @@ export abstract class YDocument<T extends DocumentChange>
2929
this._awareness = new Awareness(this._ydoc);
3030

3131
this._ystate.observe(this.onStateChanged);
32-
33-
this._providers = {};
3432
}
3533

3634
/**
@@ -42,22 +40,15 @@ export abstract class YDocument<T extends DocumentChange>
4240
this.ystate.set(`fork_${forkId}`, 'new');
4341
}
4442

45-
getProvider(providerId: string, sharedModel?: ISharedDocument): IDocumentProvider {
46-
if (!(providerId in this._providers)) {
47-
if (providerId === 'root') {
48-
throw new Error('Cannot get a new provider for root document');
49-
}
50-
if (sharedModel === undefined) {
51-
throw new Error('New provider needs a shared document');
52-
}
53-
const root_provider = this._providers['root'];
54-
this._providers[providerId] = root_provider.connectFork(providerId, sharedModel!);
43+
get provider(): IDocumentProvider {
44+
if (this._provider === undefined) {
45+
throw new Error('YDocument has no provider');
5546
}
56-
return this._providers[providerId];
47+
return this._provider;
5748
}
5849

59-
setProvider(providerId: string, provider: IDocumentProvider) {
60-
this._providers[providerId] = provider;
50+
set provider(provider: IDocumentProvider) {
51+
this._provider = provider;
6152
}
6253

6354
/**
@@ -225,8 +216,8 @@ export abstract class YDocument<T extends DocumentChange>
225216
private _awareness: Awareness;
226217
private _isDisposed = false;
227218
private _disposed = new Signal<this, void>(this);
228-
private _providers: { [key: string]: IDocumentProvider };
229-
public forkId: string;
219+
private _provider: IDocumentProvider;
220+
public roomId: string;
230221
}
231222

232223
/**
@@ -243,8 +234,8 @@ export namespace YDocument {
243234
ydoc?: Y.Doc;
244235

245236
/**
246-
* The document fork ID, defaults to 'root'.
237+
* The document room ID, defaults to ''.
247238
*/
248-
forkId?: string;
239+
roomId?: string;
249240
}
250241
}

0 commit comments

Comments
 (0)