diff --git a/javascript/src/api.ts b/javascript/src/api.ts index e73be97..3942941 100644 --- a/javascript/src/api.ts +++ b/javascript/src/api.ts @@ -91,14 +91,14 @@ export interface IDocumentProvider extends IDisposable { readonly ready: Promise; /** - * 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; + fork(): Promise; /** - * 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; } /** @@ -137,19 +137,9 @@ export interface ISharedDocument extends ISharedBase { readonly changed: ISignal; /** - * 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}' @@ -159,9 +149,9 @@ export interface ISharedDocument extends ISharedBase { addFork(forkId: string): void; /** - * The document fork ID + * The document room ID */ - forkId: string; + roomId: string; } /** diff --git a/javascript/src/ydocument.ts b/javascript/src/ydocument.ts index 000cc69..501e11e 100644 --- a/javascript/src/ydocument.ts +++ b/javascript/src/ydocument.ts @@ -17,7 +17,7 @@ export abstract class YDocument { 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'); @@ -29,8 +29,6 @@ export abstract class YDocument this._awareness = new Awareness(this._ydoc); this._ystate.observe(this.onStateChanged); - - this._providers = {}; } /** @@ -42,22 +40,15 @@ export abstract class YDocument 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; } /** @@ -225,8 +216,8 @@ export abstract class YDocument private _awareness: Awareness; private _isDisposed = false; private _disposed = new Signal(this); - private _providers: { [key: string]: IDocumentProvider }; - public forkId: string; + private _provider: IDocumentProvider; + public roomId: string; } /** @@ -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; } }