Skip to content

Commit 1c283ff

Browse files
committed
Support only one provider per document for now
1 parent 654760a commit 1c283ff

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
@@ -90,14 +90,14 @@ export interface IDocumentProvider extends IDisposable {
9090
readonly ready: Promise<void>;
9191

9292
/**
93-
* Request to fork the room in the backend, and connect the shared document to the forked room.
93+
* Request to fork the room in the backend, returns the fork ID.
9494
*/
95-
fork(): Promise<void>;
95+
fork(): Promise<string>;
9696

9797
/**
98-
* Connect a shared document to a forked room with forkId and return a document provider.
98+
* Connect the shared document to a forked room with forkId (disconnect from previous room).
9999
*/
100-
connectFork(forkId: string, sharedDocument: ISharedDocument): IDocumentProvider;
100+
connectFork(forkId: string): void;
101101
}
102102

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

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

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

160150
/**
161-
* The document fork ID
151+
* The document room ID
162152
*/
163-
forkId: string;
153+
roomId: string;
164154
}
165155

166156
/**

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)