Skip to content

Commit

Permalink
fix: fire properties change on time (#4014)
Browse files Browse the repository at this point in the history
  • Loading branch information
bytemain authored Sep 13, 2024
1 parent 3ae00da commit c60c9ce
Showing 1 changed file with 25 additions and 61 deletions.
86 changes: 25 additions & 61 deletions packages/extension/src/browser/vscode/api/main.thread.editor.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import debounce from 'lodash/debounce';
import merge from 'lodash/merge';
import throttle from 'lodash/throttle';

import { Autowired, Injectable, Optional } from '@opensumi/di';
import { IRPCProtocol } from '@opensumi/ide-connection';
Expand Down Expand Up @@ -229,7 +229,7 @@ export class MainThreadEditorService extends WithEventBus implements IMainThread

protected propertiesChangeCache = new Map<string, IEditorStatusChangeDTO>();

triggerPropertiesChange = debounce(
triggerPropertiesChange = throttle(
() => {
const changes: IEditorStatusChangeDTO[] = [];
this.propertiesChangeCache.forEach((change) => {
Expand All @@ -239,9 +239,8 @@ export class MainThreadEditorService extends WithEventBus implements IMainThread

this.proxy.$acceptPropertiesChanges(changes);
},
300,
16,
{
maxWait: 500,
leading: true,
trailing: true,
},
Expand All @@ -265,7 +264,7 @@ export class MainThreadEditorService extends WithEventBus implements IMainThread
}

startEvents() {
this.addDispose(
this.addDispose([
this.eventBus.on(EditorGroupChangeEvent, (event) => {
const payload = event.payload;
if (
Expand Down Expand Up @@ -355,9 +354,6 @@ export class MainThreadEditorService extends WithEventBus implements IMainThread
this.proxy.$acceptChange(change);
}
}),
);

this.addDispose(
this.editorService.onActiveEditorUriChange((uri) => {
if (
uri &&
Expand All @@ -371,56 +367,6 @@ export class MainThreadEditorService extends WithEventBus implements IMainThread
});
}
}),
);

const selectionChange = (e: EditorSelectionChangeEvent) => {
const editorId = getTextEditorId(e.payload.group, e.payload.editorUri, e.payload.side);

this.batchPropertiesChanges({
id: editorId,
selections: {
selections: e.payload.selections,
source: e.payload.source,
},
});

this.acceptCurrentEditor(e.payload.editorUri);
};

const debouncedSelectionChange = debounce((e) => selectionChange(e), 50, {
maxWait: 200,
leading: true,
trailing: true,
});

this.addDispose(
this.eventBus.on(EditorSelectionChangeEvent, (e) => {
if (e.payload.source === 'mouse') {
debouncedSelectionChange(e);
} else {
debouncedSelectionChange.cancel();
selectionChange(e);
}
}),
);

this.addDispose(
this.eventBus.on(
EditorVisibleChangeEvent,
debounce(
(e: EditorVisibleChangeEvent) => {
const editorId = getTextEditorId(e.payload.group, e.payload.resource.uri);
this.batchPropertiesChanges({
id: editorId,
visibleRanges: e.payload.visibleRanges,
});
},
50,
{ maxWait: 200, leading: true, trailing: true },
),
),
);
this.addDispose(
this.eventBus.on(EditorConfigurationChangedEvent, (e: EditorConfigurationChangedEvent) => {
const editorId = getTextEditorId(e.payload.group, e.payload.resource.uri);
if (e.payload.group.currentEditor && (e.payload.group.currentEditor as ISumiEditor).monacoEditor.getModel()) {
Expand All @@ -430,8 +376,26 @@ export class MainThreadEditorService extends WithEventBus implements IMainThread
});
}
}),
);
this.addDispose(
this.eventBus.on(EditorSelectionChangeEvent, (e) => {
const editorId = getTextEditorId(e.payload.group, e.payload.editorUri, e.payload.side);

this.batchPropertiesChanges({
id: editorId,
selections: {
selections: e.payload.selections,
source: e.payload.source,
},
});

this.acceptCurrentEditor(e.payload.editorUri);
}),
this.eventBus.on(EditorVisibleChangeEvent, (e: EditorVisibleChangeEvent) => {
const editorId = getTextEditorId(e.payload.group, e.payload.resource.uri);
this.batchPropertiesChanges({
id: editorId,
visibleRanges: e.payload.visibleRanges,
});
}),
this.eventBus.on(EditorGroupIndexChangedEvent, (e) => {
if (isGroupEditorState(e.payload.group)) {
const editorId = getTextEditorId(e.payload.group, e.payload.group.currentResource!.uri);
Expand All @@ -441,7 +405,7 @@ export class MainThreadEditorService extends WithEventBus implements IMainThread
});
}
}),
);
]);
}

private acceptCurrentEditor(uri: URI) {
Expand Down

1 comment on commit c60c9ce

@opensumi
Copy link
Contributor

@opensumi opensumi bot commented on c60c9ce Sep 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉 Next publish successful!

3.3.4-next-1726209145.0

Please sign in to comment.