Skip to content

Commit

Permalink
fix: 修复只有一行数据时异步导出数据为空 close #2681 (#2682)
Browse files Browse the repository at this point in the history
* fix: 修复只有一行数据时导出数据为空 close #2681

* fix: 增加超时判断
  • Loading branch information
lijinke666 authored Apr 26, 2024
1 parent ae9add9 commit fecd455
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,19 @@ Array [
"自定义节点 a-2 ",
]
`;

exports[`PivotSheet Export Test should export correctly data for single row data by { isAsyncExport: false } 1`] = `
Array [
" type 笔 笔",
"province city price cost",
"浙江 义乌 1 2",
]
`;

exports[`PivotSheet Export Test should export correctly data for single row data by { isAsyncExport: true } 1`] = `
Array [
" type 笔 笔",
"province city price cost",
"浙江 义乌 1 2",
]
`;
Original file line number Diff line number Diff line change
Expand Up @@ -224,3 +224,17 @@ exports[`TableSheet Export Test should export correct data with totals 1`] = `
浙江省-province 家具-type 桌子 4342
浙江省-province 家具-type 沙发 5343"
`;

exports[`TableSheet Export Test should export correctly data for single row data by { isAsyncExport: false } 1`] = `
Array [
"province city type price cost",
"浙江 杭州 笔 1 ",
]
`;

exports[`TableSheet Export Test should export correctly data for single row data by { isAsyncExport: true } 1`] = `
Array [
"province city type price cost",
"浙江 杭州 笔 1 ",
]
`;
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { NewLine, NewTab, PivotSheet, asyncGetAllPlainData } from '@antv/s2';
import { map, omit } from 'lodash';
import { data as originData } from 'tests/data/mock-dataset.json';
import { assembleDataCfg, assembleOptions } from 'tests/util';
import { getContainer } from 'tests/util/helpers';
import { createPivotSheet, getContainer } from 'tests/util/helpers';
import {
customColGridSimpleFields,
customRowGridSimpleFields,
Expand Down Expand Up @@ -601,4 +601,25 @@ describe('PivotSheet Export Test', () => {

expect(data.split(NewLine)).toMatchSnapshot();
});

// https://github.com/antvis/S2/issues/2681
it.each([{ isAsyncExport: false }, { isAsyncExport: true }])(
'should export correctly data for single row data by %o',
async (options) => {
const sheet = createPivotSheet({ width: 600, height: 400 });

sheet.setDataCfg({
data: sheet.dataCfg.data.slice(0, 1),
});

await sheet.render();
const data = await asyncGetAllPlainData({
sheetInstance: sheet,
split: '\t',
...options,
});

expect(data.split(NewLine)).toMatchSnapshot();
},
);
});
30 changes: 27 additions & 3 deletions packages/s2-core/__tests__/unit/utils/export/export-table-spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { slice } from 'lodash';
import { data as originData } from 'tests/data/mock-dataset.json';
import { assembleDataCfg, assembleOptions } from '../../../util';
import { getContainer } from '../../../util/helpers';
import { createTableSheet, getContainer } from '../../../util/helpers';
import { NewLine, NewTab } from '@/common';
import { CopyMIMEType } from '@/common/interface/export';
import { TableSheet } from '@/sheet-type';
import { asyncGetAllPlainData } from '@/utils';
import { NewTab, NewLine } from '@/common';
import { CopyMIMEType } from '@/common/interface/export';

describe('TableSheet Export Test', () => {
it('should export correct data with series number', async () => {
Expand Down Expand Up @@ -363,4 +363,28 @@ describe('TableSheet Export Test', () => {
expect(data.split(NewLine)).toMatchSnapshot();
},
);

// https://github.com/antvis/S2/issues/2681
it.each([{ isAsyncExport: false }, { isAsyncExport: true }])(
'should export correctly data for single row data by %o',
async (options) => {
const tableSheet = createTableSheet({ width: 600, height: 400 });

tableSheet.setDataCfg({
fields: {
columns: ['province', 'city', 'type', 'price', 'cost'],
},
data: [{ province: '浙江', city: '杭州', type: '笔', price: 1 }],
});

await tableSheet.render();
const data = await asyncGetAllPlainData({
sheetInstance: tableSheet,
split: '\t',
...options,
});

expect(data.split(NewLine)).toMatchSnapshot();
},
);
});
16 changes: 10 additions & 6 deletions packages/s2-core/src/utils/export/copy/pivot-data-cell-copy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,15 +144,19 @@ export class PivotDataCellCopy extends BaseDataCellCopy {
try {
// 因为每次 requestIdleCallback 执行的时间不一样,所以需要记录下当前执行到的 this.leafRowNodes 和 this.leafColNodes
const dataMatrixIdleCallback = (deadline: IdleDeadline) => {
let count = AsyncRenderThreshold;
const rowLen: number = this.leafRowNodes.length;
const rowLength: number = this.leafRowNodes.length;
// requestIdleCallback 浏览器空闲时会多次执行, 只有一行数据时执行一次即可, 避免生成重复数据
let count =
rowLength >= AsyncRenderThreshold
? AsyncRenderThreshold
: rowLength;

while (
deadline.timeRemaining() > 0 &&
rowIndex < rowLen - 1 &&
(deadline.timeRemaining() > 0 || deadline.didTimeout) &&
rowIndex <= rowLength - 1 &&
count > 0
) {
for (let j = rowIndex; j < rowLen && count > 0; j++) {
for (let j = rowIndex; j < rowLength && count > 0; j++) {
const row: DataItem[] = [];
const rowNode = this.leafRowNodes[j];

Expand All @@ -177,7 +181,7 @@ export class PivotDataCellCopy extends BaseDataCellCopy {
}
}

if (rowIndex === rowLen - 1) {
if (rowIndex === rowLength - 1) {
resolve(matrix);
} else {
requestIdleCallback(dataMatrixIdleCallback);
Expand Down
12 changes: 8 additions & 4 deletions packages/s2-core/src/utils/export/copy/table-copy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,17 @@ class TableDataCellCopy extends BaseDataCellCopy {
return new Promise((resolve, reject) => {
try {
const dataMatrixIdleCallback = (deadline: IdleDeadline) => {
let count = AsyncRenderThreshold;
const rowLength = this.displayData.length;
// requestIdleCallback 浏览器空闲时会多次执行, 只有一行数据时执行一次即可, 避免生成重复数据
let count =
rowLength >= AsyncRenderThreshold
? AsyncRenderThreshold
: rowLength;

while (
deadline.timeRemaining() > 0 &&
count > 0 &&
rowIndex < rowLength - 1
(deadline.timeRemaining() > 0 || deadline.didTimeout) &&
rowIndex <= rowLength - 1 &&
count > 0
) {
for (let j = rowIndex; j < rowLength && count > 0; j++) {
const rowData = this.displayData[j];
Expand Down

0 comments on commit fecd455

Please sign in to comment.