Skip to content

Commit

Permalink
Support only one provider per document for now
Browse files Browse the repository at this point in the history
  • Loading branch information
davidbrochart committed Feb 28, 2024
1 parent 20be1d2 commit 7f6d9d4
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 38 deletions.
26 changes: 8 additions & 18 deletions javascript/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,14 @@ export interface IDocumentProvider extends IDisposable {
readonly ready: Promise<void>;

/**
* Request to fork the room in the backend, and connect the shared document to the forked room.
* Request to fork the room in the backend, returns the fork ID.
*/
fork(): Promise<void>;
fork(): Promise<string>;

/**
* Connect a shared document to a forked room with forkId and return a document provider.
* Connect the shared document to a forked room with forkId (disconnect from previous room).
*/
connectFork(forkId: string, sharedDocument: ISharedDocument): IDocumentProvider;
connectFork(forkId: string): void;
}

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

/**
* Get a provider with a given ID
*
* @param providerId The provider ID
*/
getProvider(providerId: string, sharedModel?: ISharedDocument): IDocumentProvider;

/**
* Set a provider for this document
*
* @param providerId Provider ID (either 'root' or a UUID)
* @param provider The document provider
* The document's provider.
*/
setProvider(providerId: string, provider: IDocumentProvider): void;
provider: IDocumentProvider;

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

/**
* The document fork ID
* The document room ID
*/
forkId: string;
roomId: string;
}

/**
Expand Down
31 changes: 11 additions & 20 deletions javascript/src/ydocument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export abstract class YDocument<T extends DocumentChange>
{
constructor(options?: YDocument.IOptions) {
this._ydoc = options?.ydoc ?? new Y.Doc();
this.forkId = options?.forkId ?? 'root';
this.roomId = options?.roomId ?? '';

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

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

this._ystate.observe(this.onStateChanged);

this._providers = {};
}

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

getProvider(providerId: string, sharedModel?: ISharedDocument): IDocumentProvider {
if (!(providerId in this._providers)) {
if (providerId === 'root') {
throw new Error('Cannot get a new provider for root document');
}
if (sharedModel === undefined) {
throw new Error('New provider needs a shared document');
}
const root_provider = this._providers['root'];
this._providers[providerId] = root_provider.connectFork(providerId, sharedModel!);
get provider(): IDocumentProvider {
if (this._provider === undefined) {
throw new Error('YDocument has no provider');
}
return this._providers[providerId];
return this._provider;
}

setProvider(providerId: string, provider: IDocumentProvider) {
this._providers[providerId] = provider;
set provider(provider: IDocumentProvider) {
this._provider = provider;
}

/**
Expand Down Expand Up @@ -225,8 +216,8 @@ export abstract class YDocument<T extends DocumentChange>
private _awareness: Awareness;
private _isDisposed = false;
private _disposed = new Signal<this, void>(this);
private _providers: { [key: string]: IDocumentProvider };
public forkId: string;
private _provider: IDocumentProvider;
public roomId: string;
}

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

/**
* The document fork ID, defaults to 'root'.
* The document room ID, defaults to ''.
*/
forkId?: string;
roomId?: string;
}
}

0 comments on commit 7f6d9d4

Please sign in to comment.