Skip to content

Commit

Permalink
fix: reset yTextMap when file updated by system (#2260)
Browse files Browse the repository at this point in the history
  • Loading branch information
pipiiiiii authored Feb 14, 2023
1 parent cc8d168 commit a5d663c
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
11 changes: 9 additions & 2 deletions packages/collaboration/src/browser/collaboration.contribution.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Autowired } from '@opensumi/di';
import {
ClientAppContribution,
FsProviderContribution,
KeybindingContribution,
KeybindingRegistry,
KeybindingWeight,
Expand All @@ -10,8 +11,10 @@ import { CommandContribution, CommandRegistry, ContributionProvider, Domain } fr
import { ICollaborationService, CollaborationModuleContribution } from '../common';
import { REDO, UNDO } from '../common/commands';

@Domain(ClientAppContribution, KeybindingContribution, CommandContribution)
export class CollaborationContribution implements ClientAppContribution, KeybindingContribution, CommandContribution {
@Domain(ClientAppContribution, KeybindingContribution, CommandContribution, FsProviderContribution)
export class CollaborationContribution
implements ClientAppContribution, KeybindingContribution, CommandContribution, FsProviderContribution
{
@Autowired(ICollaborationService)
private collaborationService: ICollaborationService;

Expand Down Expand Up @@ -64,4 +67,8 @@ export class CollaborationContribution implements ClientAppContribution, Keybind
},
});
}

onFileServiceReady() {
this.collaborationService.initFileWatch();
}
}
27 changes: 26 additions & 1 deletion packages/collaboration/src/browser/collaboration.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { WebsocketProvider } from 'y-websocket';
import { Doc as YDoc, Map as YMap, YMapEvent, Text as YText } from 'yjs';

import { Injectable, Autowired, Inject, INJECTOR_TOKEN, Injector } from '@opensumi/di';
import { AppConfig } from '@opensumi/ide-core-browser';
import { AppConfig, DisposableCollection } from '@opensumi/ide-core-browser';
import { Deferred, ILogger, OnEvent, uuid, WithEventBus } from '@opensumi/ide-core-common';
import { WorkbenchEditorService } from '@opensumi/ide-editor';
import {
Expand All @@ -16,6 +16,7 @@ import {
IEditorDocumentModelService,
} from '@opensumi/ide-editor/lib/browser';
import { WorkbenchEditorServiceImpl } from '@opensumi/ide-editor/lib/browser/workbench-editor.service';
import { IFileServiceClient, FileChangeEvent, FileChangeType } from '@opensumi/ide-file-service/lib/common';
import { ITextModel, ICodeEditor } from '@opensumi/ide-monaco';
import { ICSSStyleService } from '@opensumi/ide-theme';

Expand Down Expand Up @@ -54,6 +55,9 @@ export class CollaborationService extends WithEventBus implements ICollaboration
@Autowired(IEditorDocumentModelService)
private docModelManager: IEditorDocumentModelService;

@Autowired(IFileServiceClient)
protected readonly fileServiceClient: IFileServiceClient;

@Autowired(AppConfig)
private appConfig: AppConfig;

Expand All @@ -75,6 +79,8 @@ export class CollaborationService extends WithEventBus implements ICollaboration

private bindingReadyMap: Map<string, Deferred<void>> = new Map();

protected readonly toDisposableCollection: DisposableCollection = new DisposableCollection();

private yMapObserver = (event: YMapEvent<YText>) => {
const changes = event.changes.keys;
changes.forEach((change, key) => {
Expand Down Expand Up @@ -133,6 +139,14 @@ export class CollaborationService extends WithEventBus implements ICollaboration
this.yWebSocketProvider.awareness.setLocalStateField('user-info', this.userInfo);
}

initFileWatch() {
this.toDisposableCollection.push(
this.fileServiceClient.onFilesChanged((e) => {
this.handleFileChange(e);
}),
);
}

destroy() {
this.yWebSocketProvider.awareness.off('update', this.updateCSSManagerWhenAwarenessUpdated);
this.clientIDStyleAddedSet.forEach((clientID) => {
Expand All @@ -143,6 +157,7 @@ export class CollaborationService extends WithEventBus implements ICollaboration
this.yTextMap.unobserve(this.yMapObserver);
this.yWebSocketProvider.disconnect();
this.bindingMap.forEach((binding) => binding.destroy());
this.toDisposableCollection.dispose();
}

registerContribution(contribution: CollaborationModuleContribution) {
Expand Down Expand Up @@ -268,6 +283,16 @@ export class CollaborationService extends WithEventBus implements ICollaboration
}
};

private handleFileChange(e: FileChangeEvent) {
e.forEach((change) => {
// 只有从文件系统更新,并且窗口未打开情况,才重置 yTextMap
if (change.type === FileChangeType.UPDATED && !this.bindingMap.get(change.uri) && this.yTextMap.get(change.uri)) {
this.yTextMap.delete(change.uri);
this.resetDeferredYMapKey(change.uri);
}
});
}

@OnEvent(EditorDocumentModelCreationEvent)
private async editorDocumentModelCreationHandler(e: EditorDocumentModelCreationEvent) {
if (e.payload.uri.scheme !== 'file') {
Expand Down
1 change: 1 addition & 0 deletions packages/collaboration/src/common/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export const ICollaborationService = Symbol('ICollaborationService');

export interface ICollaborationService {
initialize(): void;
initFileWatch(): void;
destroy(): void;
undoOnFocusedTextModel(): void;
redoOnFocusedTextModel(): void;
Expand Down

0 comments on commit a5d663c

Please sign in to comment.