Skip to content

Commit 59a251c

Browse files
authored
chore: revert send data by chunk in websocket (#3988) (#4477)
1 parent 82ddf9a commit 59a251c

File tree

15 files changed

+150
-659
lines changed

15 files changed

+150
-659
lines changed

packages/components/src/recycle-tree/tree/TreeNode.ts

+5-11
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,9 @@ const { Path } = path;
3939
* @param items 插入的数组
4040
*/
4141
export function spliceArray(arr: number[], start: number, deleteCount = 0, items?: number[] | null) {
42-
// 如果没有修改操作,直接返回原数组
43-
if (deleteCount === 0 && (!items || items.length === 0)) {
44-
return arr;
45-
}
46-
47-
// 直接使用 slice + concat 避免 spread operator
48-
const before = arr.slice(0, start);
49-
const after = arr.slice(start + deleteCount);
50-
return before.concat(items || []).concat(after);
42+
const a = arr.slice(0);
43+
a.splice(start, deleteCount, ...(items || []));
44+
return a;
5145
}
5246

5347
export enum BranchOperatorStatus {
@@ -576,10 +570,10 @@ export class CompositeTreeNode extends TreeNode implements ICompositeTreeNode {
576570
}
577571

578572
/**
579-
* 确保此"目录"的子级已加载(不影响"展开"状态)
573+
* 确保此“目录”的子级已加载(不影响“展开”状态)
580574
* 如果子级已经加载,则返回的Promise将立即解决
581575
* 否则,将发出重新加载请求并返回Promise
582-
* 一旦返回的Promise.resolve,"CompositeTreeNode#children" 便可以访问到对于节点
576+
* 一旦返回的Promise.resolve,CompositeTreeNode#children 便可以访问到对于节点
583577
*/
584578
public async ensureLoaded(token?: CancellationToken) {
585579
if (this._children) {

packages/connection/__test__/browser/index.test.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { WSWebSocketConnection, furySerializer } from '@opensumi/ide-connection';
1+
import { furySerializer } from '@opensumi/ide-connection';
22
import { ReconnectingWebSocketConnection } from '@opensumi/ide-connection/lib/common/connection/drivers/reconnecting-websocket';
33
import { sleep } from '@opensumi/ide-core-common';
44
import { Server, WebSocket } from '@opensumi/mock-socket';
@@ -21,11 +21,10 @@ describe('connection browser', () => {
2121
let data2Received = false;
2222

2323
mockServer.on('connection', (socket) => {
24-
const connection = new WSWebSocketConnection(socket as any);
25-
connection.onMessage((msg) => {
24+
socket.on('message', (msg) => {
2625
const msgObj = furySerializer.deserialize(msg as Uint8Array);
2726
if (msgObj.kind === 'open') {
28-
connection.send(
27+
socket.send(
2928
furySerializer.serialize({
3029
id: msgObj.id,
3130
kind: 'server-ready',

packages/connection/__test__/common/buffers.test.ts

-165
Original file line numberDiff line numberDiff line change
@@ -253,158 +253,6 @@ describe('Buffers', () => {
253253
expect(cursor.lineOffset).toEqual(1);
254254
expect(list.pos(cursor.offset)).toEqual({ buf: cursor.line, offset: cursor.lineOffset });
255255
});
256-
257-
// 测试空缓冲区行为
258-
describe('Empty Buffer Handling', () => {
259-
it('should handle empty buffer operations', () => {
260-
const empty = new Buffers();
261-
262-
expect(empty.byteLength).toBe(0);
263-
expect(empty.slice()).toEqual(new Uint8Array(0));
264-
expect(() => empty.pos(0)).toThrow('out of range');
265-
expect(empty.splice(0, 0)).toEqual(expect.any(Buffers));
266-
});
267-
});
268-
269-
// 测试边界slice操作
270-
describe('Edge Case Slicing', () => {
271-
const buffer = create([0, 1, 2, 3, 4, 5], [3, 3]);
272-
273-
it('should handle start at chunk boundary', () => {
274-
expect(buffer.slice(3, 5)).toEqual(new Uint8Array([3, 4]));
275-
});
276-
277-
it('should handle end at chunk boundary', () => {
278-
expect(buffer.slice(2, 3)).toEqual(new Uint8Array([2]));
279-
});
280-
});
281-
it('should handle splice at exact chunk boundary', () => {
282-
const buffer = createEnhanced([0, 1, 2, 3, 4, 5], [3, 3]);
283-
buffer.splice(3, 2, new Uint8Array([99]));
284-
expect(buffer.slice()).toEqual(new Uint8Array([0, 1, 2, 99, 5]));
285-
});
286-
// 测试非法索引访问
287-
describe('Invalid Access Handling', () => {
288-
const buffer = create([1, 2, 3], [3]);
289-
290-
it('should throw on negative index', () => {
291-
expect(() => buffer.pos(-1)).toThrow('out of range');
292-
});
293-
294-
it('should throw on overflow index', () => {
295-
expect(() => buffer.pos(4)).toThrow('out of range');
296-
});
297-
});
298-
299-
// 测试超大缓冲区
300-
describe('Large Buffer Handling', () => {
301-
const MB1 = new Uint8Array(1024 * 1024);
302-
const buffer = new Buffers();
303-
304-
beforeAll(() => {
305-
// 填充1MB数据
306-
for (let i = 0; i < 1024; i++) {
307-
buffer.push(MB1.subarray(0, 1024));
308-
}
309-
});
310-
311-
it('should handle 1GB data slicing', () => {
312-
const slice = buffer.slice(1024 * 512, 1024 * 512 + 100);
313-
expect(slice.byteLength).toBe(100);
314-
});
315-
});
316-
317-
// 测试跨chunk的splice操作
318-
describe('Cross-Chunk Splicing', () => {
319-
const buffer = create([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [3, 3, 4]);
320-
321-
it('should splice across multiple chunks', () => {
322-
const removed = buffer.splice(2, 5, new Uint8Array([99]));
323-
324-
expect(buffer.slice()).toEqual(new Uint8Array([0, 1, 99, 7, 8, 9]));
325-
expect(removed.slice()).toEqual(new Uint8Array([2, 3, 4, 5, 6]));
326-
});
327-
});
328-
329-
// 测试slice4特殊方法
330-
describe('slice4 Special Cases', () => {
331-
it('should handle partial slice4', () => {
332-
const buffer = create([1, 2, 3], [3]);
333-
expect(buffer.slice4(2)).toEqual(new Uint8Array([3, 0, 0, 0]));
334-
});
335-
336-
it('should handle edge slice4', () => {
337-
const buffer = create([1, 2, 3, 4, 5], [5]);
338-
expect(buffer.slice4(1)).toEqual(new Uint8Array([2, 3, 4, 5]));
339-
});
340-
});
341-
342-
// 测试Cursor高级功能
343-
describe('Cursor Advanced Operations', () => {
344-
const buffer = create([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [3, 3, 4]);
345-
346-
it('should handle moveTo across chunks', () => {
347-
const cursor = buffer.cursor();
348-
cursor.moveTo(5);
349-
expect(cursor.line).toBe(1);
350-
expect(cursor.lineOffset).toBe(2);
351-
});
352-
353-
it('should reset correctly', () => {
354-
const cursor = buffer.cursor(5);
355-
cursor.reset();
356-
expect(cursor.offset).toBe(0);
357-
expect(cursor.line).toBe(0);
358-
});
359-
});
360-
361-
// 测试Dispose行为
362-
describe('Resource Management', () => {
363-
it('should clear resources on dispose', () => {
364-
const buffer = create([1, 2, 3], [3]);
365-
buffer.dispose();
366-
367-
expect(buffer.buffers).toEqual([]);
368-
expect(buffer.byteLength).toBe(0);
369-
});
370-
});
371-
372-
// 性能测试
373-
describe('Performance Tests', () => {
374-
let largeBuffer: Buffers;
375-
376-
beforeAll(() => {
377-
largeBuffer = new Buffers();
378-
// 创建包含1000个10KB chunk的缓冲区
379-
for (let i = 0; i < 1000; i++) {
380-
largeBuffer.push(new Uint8Array(10 * 1024));
381-
}
382-
});
383-
384-
it('should handle slicing 1MB data under 50ms', () => {
385-
const start = performance.now();
386-
const slice = largeBuffer.slice(0, 1024 * 1024);
387-
const duration = performance.now() - start;
388-
389-
expect(duration).toBeLessThan(50);
390-
expect(slice.byteLength).toBe(1024 * 1024);
391-
});
392-
393-
it('should handle 1000 splices under 1s', () => {
394-
const buf = createEnhanced(
395-
new Array(10000).fill(0).map((_, i) => i),
396-
[100, 900, 9000],
397-
);
398-
399-
const start = performance.now();
400-
for (let i = 0; i < 1000; i++) {
401-
buf.splice(i % 100, 5, new Uint8Array([i]));
402-
}
403-
const duration = performance.now() - start;
404-
405-
expect(duration).toBeLessThan(1000);
406-
});
407-
});
408256
});
409257

410258
function create(xs: number[], split: number[]) {
@@ -416,16 +264,3 @@ function create(xs: number[], split: number[]) {
416264
});
417265
return bufs;
418266
}
419-
420-
function createEnhanced(xs: number[], split: number[]): Buffers {
421-
const bufs = new Buffers();
422-
let offset = 0;
423-
split.forEach((chunkSize) => {
424-
if (chunkSize > 0) {
425-
const chunk = new Uint8Array(xs.slice(offset, offset + chunkSize));
426-
bufs.push(chunk);
427-
offset += chunkSize;
428-
}
429-
});
430-
return bufs;
431-
}

0 commit comments

Comments
 (0)