react-diff-view icon indicating copy to clipboard operation
react-diff-view copied to clipboard

数据过大,卡顿怎么解决,怎么能加loadMore状态

Open lunalusy opened this issue 7 years ago • 8 comments

lunalusy avatar Mar 14 '19 12:03 lunalusy

我不是很清理你具体卡在哪里,总的来说有几个方向:

  1. 如果是因为开了代码高亮或者行内标记而卡顿,2.0版本已经使用异步来处理这几个环节,可以有效缓解
  2. 如果是因为diff文本太大,parse就卡,那么parse本身是个纯函数,放进WebWorker里异步处理
  3. 如果确实是因为数据量大,Diff的渲染卡顿的话,Diff组件事实上和普通的Table或者List很相似,它的本质是对hunks这个属性进行一次map操作转为最终的渲染元素,所以你可以使用任何列表上使用的手段,控制好hunks的数量,通过loadMore等慢慢追加hunks数组的内容就行。这里有一种情况是,hunks只有一个,但里面的changes超大,此时可以把changes给拆开来变成多个hunks,需要一些额外的代码,你是这种情况的话我可以写个工具函数出来

如果你可以确定卡顿的原因并大致了解我上面说的,可以尝试一下各方向的优化。如果不确定的话,提供给我一个实际有问题的codesandbox示例,我可以研究一下

otakustay avatar Mar 15 '19 03:03 otakustay

感谢你的解答 1.升到2.0.1最新版本,我发现会抛出错误 Invalid prop children of type array supplied to Diff, expected function.在1.8.1版本的时候不会有这个错误 2、我这边调试,发现应该就是您说的hunks只有一个,但是changes超大,有7千多个

lunalusy avatar Mar 17 '19 06:03 lunalusy

第一个问题是,2.0和1.x的API不一样,2.0的children是一个函数,你看一下README能找到相关说明

第二个,你需要一个把单个hunk里很多changes拆成若干个hunk的函数,我来研究一下这个怎么实现,其实挺简单的

如果你能提供一下你这边具体测试的diff文本,我能够实际地进行验证,会更好一些

otakustay avatar Mar 17 '19 06:03 otakustay

再次感谢,非常抱歉,我这边数据比较敏感,不能提供给您测试

lunalusy avatar Mar 17 '19 06:03 lunalusy

我简单实现了一个能拆hunk的方法:

export const chunkHunk = (hunk, maxChangesPerHunk) => {
    const sliceChunk = ({changes}, startIndex, output = []) => {
        if (startIndex >= changes.length) {
            return output;
        }

        const nextStartIndex = startIndex + maxChangesPerHunk;
        const changesInChunk = changes.slice(startIndex, nextStartIndex);
        const lastChange = changesInChunk[changesInChunk.length - 1];
        const nextChange = changes[nextStartIndex];
        // When a delete change is followed by an insert change, they should be placed side by side,
        // therefore we cannot split them into different hunks, the insert one must be lifted to this hunk
        const requireAdditionInsertChange = nextChange && lastChange.isDelete && nextChange.isInsert;

        if (requireAdditionInsertChange) {
            changesInChunk.push(nextChange);
        }
        output.push(createHunkFromChanges(changesInChunk));

        return sliceChunk(
            hunk,
            requireAdditionInsertChange ? nextStartIndex + 1 : nextStartIndex,
            output
        );
    };
    return sliceChunk(hunk, 0);
};

当你给它一个hunk对象,并指定拆完后每一个hunk最多能有maxChangesPerHunk个change的时候,它会给你返回多个hunk并保留change信息

简单地过了一下单测,应该是能用的。但是由于拆成了多个hunk,所以如果你有在hunk的头部加一些其它元素的话,可能会影响美观,得自己再加点逻辑

otakustay avatar Mar 17 '19 07:03 otakustay

image

上面的方法并不是完全正确的,因为对hunk的oldStart等数据处理有问题,只适用于你没有给hunk加头部的情况,具体详细的我慢慢去研究怎么实现

otakustay avatar Mar 17 '19 08:03 otakustay

您好,能再提供一下 createHunkFromChanges方法嘛?

lunalusy avatar Mar 17 '19 11:03 lunalusy

这个方法在util/insertHunk.js中,你可以原样抄过来

为了更好的使用chunkHunk,建议你parseDiff时打开nearbySequences: zip的功能,同时不可以和minCollapsed的hoc一起用,不然可能会有比较奇怪的情况出现,具体细节我需要时间调整

在 2019年3月17日,下午7:31,lunalusy [email protected] 写道:

您好,能再提供一下 createHunkFromChanges方法嘛?

— You are receiving this because you commented. Reply to this email directly, view it on GitHub, or mute the thread.

otakustay avatar Mar 17 '19 11:03 otakustay