Skip to content

Commit

Permalink
fix: only handle scheme file on collaborative mode (#1709)
Browse files Browse the repository at this point in the history
* fix: only handle scheme `file` on collaborative mode

* fix: use `codeUri.scheme` instead

* fix: should break all awaiting init content request before removal

* refactor: rename variables
  • Loading branch information
situ2001 authored Sep 23, 2022
1 parent 6663c19 commit fb474f8
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
8 changes: 8 additions & 0 deletions packages/collaboration/src/browser/collaboration.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,10 @@ export class CollaborationService extends WithEventBus implements ICollaboration

@OnEvent(EditorDocumentModelCreationEvent)
private async editorDocumentModelCreationHandler(e: EditorDocumentModelCreationEvent) {
if (e.payload.uri.scheme !== 'file') {
return;
}

const uriString = e.payload.uri.toString();
const { bindingReady, yMapReady } = this.getDeferred(uriString);
await this.backService.requestInitContent(uriString);
Expand All @@ -271,6 +275,10 @@ export class CollaborationService extends WithEventBus implements ICollaboration

@OnEvent(EditorDocumentModelRemovalEvent)
private async editorDocumentModelRemovalHandler(e: EditorDocumentModelRemovalEvent) {
if (e.payload.codeUri.scheme !== 'file') {
return;
}

const uriString = e.payload.codeUri.toString();
const { bindingReady } = this.getDeferred(uriString);
await bindingReady.promise;
Expand Down
25 changes: 24 additions & 1 deletion packages/collaboration/src/node/y-websocket-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ export class YWebsocketServerImpl implements IYWebsocketServer {

private server: http.Server;

private rejectsOfRequestingPromises: Map<string, Set<(reason: string) => void>> = new Map();

initialize() {
this.logger.debug('init y-websocket server');

Expand Down Expand Up @@ -84,13 +86,21 @@ export class YWebsocketServerImpl implements IYWebsocketServer {

removeYText(uri: string) {
this.logger.debug('trying to remove uri', uri);

// break all still-awaiting promise
const rejects = this.rejectsOfRequestingPromises.get(uri);
if (rejects) {
rejects.forEach((reject) => reject('Remove unresolved promise of this uri'));
rejects.clear();
}

if (this.yMap.has(uri)) {
this.yMap.delete(uri);
this.logger.debug('removed', uri);
}
}

async requestInitContent(uri: string): Promise<void> {
private async doRequestInitContent(uri: string): Promise<void> {
try {
// load content from disk, not client
const { content } = await this.fileService.resolveContent(uri);
Expand All @@ -104,6 +114,19 @@ export class YWebsocketServerImpl implements IYWebsocketServer {
}
}

async requestInitContent(uri: string): Promise<void> {
const promise = new Promise<void>((resolve, reject) => {
this.doRequestInitContent(uri).then(() => resolve());

if (!this.rejectsOfRequestingPromises.has(uri)) {
this.rejectsOfRequestingPromises.set(uri, new Set());
}
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
this.rejectsOfRequestingPromises.get(uri)!.add(reject);
});
await promise;
}

destroy() {
this.websocketServer.close();
this.server.close();
Expand Down

0 comments on commit fb474f8

Please sign in to comment.