Javascript 违反';设置超时';汉德勒接受了<;N>;太太
我在我的页面中收到此控制台警告-Javascript 违反';设置超时';汉德勒接受了<;N>;太太,javascript,reactjs,Javascript,Reactjs,我在我的页面中收到此控制台警告- [Violation] 'setTimeout' handler took <N>ms 这是我的完整档案- import React, { useState, useEffect } from "react"; import { diffLines, formatLines } from "./unidiff"; import { parseDiff, Diff, Hunk } from "reac
[Violation] 'setTimeout' handler took <N>ms
这是我的完整档案-
import React, { useState, useEffect } from "react";
import { diffLines, formatLines } from "./unidiff";
import { parseDiff, Diff, Hunk } from "react-diff-view";
import { getComplianceIcon, getFormattedDate } from "../common";
import "./diff.less";
import "react-diff-view/style/index.css";
/**
* This constant is used for chunking.
* A value of 3000 takes ~ 15 secs to show the diff for real data
* A value of 2000 takes ~ 12 secs to show the diff for real data
*/
const THRESHOLD = 2000; //changing this is reflecting in response time, need to check
const DiffViewer = props => {
const { startUpFile, runningFile, showLoader, compareWith } = props;
const [diffArr, setDiffArr] = useState([]);
const [isLoading, setIsLoading] = useState(true);
useEffect(() => {
const timer = setTimeout(() => {
const diffArr = getDiffArray(
props.startupConfigData,
props.runningConfigData
);
if (diffArr.length > 0) {
setDiffArr(diffArr);
setIsLoading(false);
}
}, 200);
return () => clearTimeout(timer);
});
/**
* Returns an array of differences in the unidiff format
*
* @param {string} oldLongText - the old string to compare
* @param {string} newLongText - the new string to compare
* @returns {Array} - the array having the diff objects
*/
const getDiffArray = (oldLongText = "", newLongText = "") => {
if (oldLongText !== newLongText) {
const oldTextChunks = getChunkedLinesOfText(oldLongText, THRESHOLD);
const newTextChunks = getChunkedLinesOfText(newLongText, THRESHOLD);
const chunkMaxLen = Math.max(oldTextChunks.length, newTextChunks.length);
const zipChunks = (_, i) => [
oldTextChunks[i] || "",
newTextChunks[i] || ""
];
const unmodDiffContext = chunkMaxLen > 1 ? 3 : 500;
return Array.from({ length: chunkMaxLen }, zipChunks).reduce(
(res, [oldText, newText], idx) => {
const diffText = computeDiff(oldText, newText, unmodDiffContext);
if (diffText) {
const maxLenOfText =
idx * THRESHOLD +
Math.max(oldText.split("\n").length, newText.split("\n").length);
res.push({ diffText, start: idx * THRESHOLD, end: maxLenOfText });
}
return res;
},
[]
);
}
return [];
};
/**
* Splits input text into chunks of specified size
*
* @param {string} text - takes the input text and splits it into chunks
* @param {number} threshold - takes a threshold which is the chunk size
* @returns {Array} - returns an array of strings as specified by the chunk size
*/
const getChunkedLinesOfText = (text = "", threshold = 3000) => {
const totalLines = (typeof text === "string" && text.split("\n")) || [];
if (threshold > 0 && totalLines.length > threshold) {
const chunks = Math.ceil(totalLines.length / threshold);
const chunkArray = (_, idx) =>
totalLines
.slice(idx * threshold, idx * threshold + threshold)
.join("\n");
return Array.from({ length: chunks }, chunkArray);
}
return [text];
};
const computeDiff = (oldText = "", newText = "", unmodDiffContext = 3) => {
return formatLines(diffLines(oldText, newText), {
context: unmodDiffContext
});
};
const renderFile = (
{ oldRevision, newRevision, type, hunks },
start,
end,
isFirstDiff
) => {
const separator = `${i18n.diff_showing_lines} ${start} - ${end}`;
return (
<div
className="diff-div margin-left-5px"
key={oldRevision + `${separator}` - +newRevision}
>
{!isFirstDiff && <div className="diff-separator">{separator}</div>}
<Diff
key={oldRevision + `${separator}` - +newRevision}
viewType="split"
diffType={type}
hunks={hunks}
>
{hunks => hunks.map(hunk => <Hunk key={hunk.content} hunk={hunk} />)}
</Diff>
</div>
);
};
/**
* Get text based on selection - startup/running
*/
const getStartupText = () => {
if (compareWith == undefined || compareWith === "startup") {
return i18n.label_startup_configuration;
} else {
return i18n.label_running_configuration;
}
};
const statusDisplayDetails = getComplianceIcon("COMPLIANT", true);
const DiffData_Loading = () => {
let loderLabel;
let loderLabelExtraTime = i18n.take_while_text;
if (props.runningFile)
loderLabel =
props.runningFile.totalNoOfLines < 5000
? "Loading..."
: loderLabelExtraTime;
return (
<div className="loading-icon">
<DnxLoader color="#026E97" size="54" label={loderLabel} />
</div>
);
};
/**
* Function returns the DnxBanner information text for diff Position Mismatch
*/
const _diffPositionMismatchInfoText = () => {
const informationConfig = {
message: i18n.diff_postion_mismatch_info,
type: "information"
};
return (
<div className="margin-5px">
<DnxBanner name="banner-loading" config={informationConfig}></DnxBanner>
</div>
);
};
/**
* return function
*/
return (
<div className="">
<div>
{startUpFile && runningFile ? (
<div>
{showLoader ? (
DiffData_Loading()
) : (
<div>
<div className="flex margin-left-5px">
<div className="startup-text">
{getStartupText()} ({startUpFile.totalNoOfLines}{" "}
{i18n.label_lines}) -{" "}
{getFormattedDate(startUpFile.createdTime)}
</div>
<div className="running-text">
{i18n.label_running_configuration} (
{runningFile.totalNoOfLines} {i18n.label_lines}) -{" "}
{getFormattedDate(runningFile.createdTime)}
</div>
</div>
{diffArr &&
props.startupConfigData !== props.runningConfigData ? (
<div>
{isLoading && DiffData_Loading()}
{!isLoading && (
<div>
{diffArr.map(({ diffText, start, end }, idx) =>
parseDiff(diffText, { nearbySequences: "zip" }).map(
file => renderFile(file, start, end, idx === 0)
)
)}
</div>
)}
</div>
) : (
<div className="div-margin-diff">
{statusDisplayDetails}
<span>
{compareWith === "running"
? i18n.label_running_config_compliant
: i18n.label_running_startup_config_compliant}
</span>
</div>
)}
</div>
)}
</div>
) : (
<div className="div-margin-diff">
<span>{i18n.label_running_config_not_available}</span>
</div>
)}
</div>
{_diffPositionMismatchInfoText()}
</div>
);
};
export default DiffViewer;
import React,{useState,useffect}来自“React”;
从“/unidiff”导入{diffLines,formatLines};
从“react Diff view”导入{parseDiff,Diff,Hunk};
从“./common”导入{getComplianceIcon,getFormattedDate};
导入“/diff.less”;
导入“react diff view/style/index.css”;
/**
*此常量用于分块。
*3000的值需要约15秒才能显示实际数据的差异
*2000的值需要约12秒才能显示实际数据的差异
*/
常数阈值=2000//更改此选项反映的是响应时间,需要检查
const DiffViewer=props=>{
const{startUpFile,runningFile,showLoader,compareWith}=props;
const[diffArr,setDiffArr]=useState([]);
const[isLoading,setIsLoading]=useState(true);
useffect(()=>{
常量计时器=设置超时(()=>{
const diffArr=getDiffArray(
props.startupConfigData,
props.runningConfigData
);
如果(不同长度>0){
setDiffArr(diffArr);
设置加载(假);
}
}, 200);
return()=>clearTimeout(计时器);
});
/**
*返回unidiff格式的差异数组
*
*@param{string}oldLongText-要比较的旧字符串
*@param{string}newLongText-要比较的新字符串
*@returns{Array}-具有diff对象的数组
*/
const getDiffArray=(oldLongText=“”,newLongText=“”)=>{
if(oldLongText!==newLongText){
const oldTextChunks=GetChunkedLineSoftText(oldLongText,阈值);
const newTextChunks=GetChunkedLineSoftText(newLongText,阈值);
const chunkMaxLen=Math.max(oldTextChunks.length,newTextChunks.length);
const zipChunks=(u,i)=>[
oldTextChunks[i]| |“”,
新文本块[i]| |“
];
const unmodDiffContext=chunkMaxLen>1?3:500;
返回数组.from({length:chunkMaxLen},zipChunks).reduce(
(res[oldText,newText],idx)=>{
const diffText=computeDiff(旧文本、新文本、未添加上下文);
if(diffText){
常量maxLenOfText=
idx*阈值+
max(oldText.split(“\n”).length,newText.split(“\n”).length);
res.push({diffText,start:idx*THRESHOLD,end:maxLenOfText});
}
返回res;
},
[]
);
}
返回[];
};
/**
*将输入文本拆分为指定大小的块
*
*@param{string}text-获取输入文本并将其拆分为块
*@param{number}threshold-接受一个阈值,即块大小
*@returns{Array}-返回由块大小指定的字符串数组
*/
const getChunkedLinesOfText=(text=,threshold=3000)=>{
const totalines=(typeof text==“string”和&text.split(“\n”))||[];
如果(阈值>0&&Totalines.length>阈值){
const chunks=Math.ceil(totalines.length/threshold);
常量chunkArray=(\ux,idx)=>
总计
.slice(idx*阈值,idx*阈值+阈值)
.加入(“\n”);
返回Array.from({length:chunks},chunkArray);
}
返回[文本];
};
const computeDiff=(oldText=,newText=,unaddiffcontext=3)=>{
返回格式行(区分行(旧文本、新文本){
上下文:unaddiffcontext
});
};
常量渲染文件=(
{oldRevision,newRevision,type,hunks},
开始
完,,
isFirstDiff
) => {
常量分隔符=`${i18n.diff_showing_line}${start}-${end}`;
返回(
{!isFirstDiff&&{separator}}
{hunks=>hunks.map(hunk=>)}
);
};
/**
*根据选择获取文本-启动/运行
*/
常量getStartupText=()=>{
if(compareWith==未定义| | compareWith===“启动”){
返回i18n.label\u启动\u配置;
}否则{
返回i18n.label\u running\u配置;
}
};
const statusDisplayDetails=getComplianceIcon(“COMPLIANT”,true);
const DiffData_加载=()=>{
让洛德标签;
让loderLabelExtraTime=i18n.在文本时获取;
if(props.runningFile)
洛德标签=
props.runningFile.totalNoOfLines<5000
“装载…”
:LoderLabelextrame;
返回(
);
};
/**
*函数返回差异位置不匹配的DnxBanner信息文本
*/
const_diffpositionmistchinfotext=()=>{
常量信息配置={
消息:i18n.diff\u position\u mismatch\u info,
类型:“信息”
};
返回(
);
};
/**
*返回函数
*/
返回(
{startUpFile&&runningFile(
{showLoader(
DiffData_加载()
) : (
{getStartupText()}({startUpFile.totalNoOfLines}{”“}
{i18n.label_lines})-{'}
{getFormattedDate(startUpFile.createdTime)
import React, { useState, useEffect } from "react";
import { diffLines, formatLines } from "./unidiff";
import { parseDiff, Diff, Hunk } from "react-diff-view";
import { getComplianceIcon, getFormattedDate } from "../common";
import "./diff.less";
import "react-diff-view/style/index.css";
/**
* This constant is used for chunking.
* A value of 3000 takes ~ 15 secs to show the diff for real data
* A value of 2000 takes ~ 12 secs to show the diff for real data
*/
const THRESHOLD = 2000; //changing this is reflecting in response time, need to check
const DiffViewer = props => {
const { startUpFile, runningFile, showLoader, compareWith } = props;
const [diffArr, setDiffArr] = useState([]);
const [isLoading, setIsLoading] = useState(true);
useEffect(() => {
const timer = setTimeout(() => {
const diffArr = getDiffArray(
props.startupConfigData,
props.runningConfigData
);
if (diffArr.length > 0) {
setDiffArr(diffArr);
setIsLoading(false);
}
}, 200);
return () => clearTimeout(timer);
});
/**
* Returns an array of differences in the unidiff format
*
* @param {string} oldLongText - the old string to compare
* @param {string} newLongText - the new string to compare
* @returns {Array} - the array having the diff objects
*/
const getDiffArray = (oldLongText = "", newLongText = "") => {
if (oldLongText !== newLongText) {
const oldTextChunks = getChunkedLinesOfText(oldLongText, THRESHOLD);
const newTextChunks = getChunkedLinesOfText(newLongText, THRESHOLD);
const chunkMaxLen = Math.max(oldTextChunks.length, newTextChunks.length);
const zipChunks = (_, i) => [
oldTextChunks[i] || "",
newTextChunks[i] || ""
];
const unmodDiffContext = chunkMaxLen > 1 ? 3 : 500;
return Array.from({ length: chunkMaxLen }, zipChunks).reduce(
(res, [oldText, newText], idx) => {
const diffText = computeDiff(oldText, newText, unmodDiffContext);
if (diffText) {
const maxLenOfText =
idx * THRESHOLD +
Math.max(oldText.split("\n").length, newText.split("\n").length);
res.push({ diffText, start: idx * THRESHOLD, end: maxLenOfText });
}
return res;
},
[]
);
}
return [];
};
/**
* Splits input text into chunks of specified size
*
* @param {string} text - takes the input text and splits it into chunks
* @param {number} threshold - takes a threshold which is the chunk size
* @returns {Array} - returns an array of strings as specified by the chunk size
*/
const getChunkedLinesOfText = (text = "", threshold = 3000) => {
const totalLines = (typeof text === "string" && text.split("\n")) || [];
if (threshold > 0 && totalLines.length > threshold) {
const chunks = Math.ceil(totalLines.length / threshold);
const chunkArray = (_, idx) =>
totalLines
.slice(idx * threshold, idx * threshold + threshold)
.join("\n");
return Array.from({ length: chunks }, chunkArray);
}
return [text];
};
const computeDiff = (oldText = "", newText = "", unmodDiffContext = 3) => {
return formatLines(diffLines(oldText, newText), {
context: unmodDiffContext
});
};
const renderFile = (
{ oldRevision, newRevision, type, hunks },
start,
end,
isFirstDiff
) => {
const separator = `${i18n.diff_showing_lines} ${start} - ${end}`;
return (
<div
className="diff-div margin-left-5px"
key={oldRevision + `${separator}` - +newRevision}
>
{!isFirstDiff && <div className="diff-separator">{separator}</div>}
<Diff
key={oldRevision + `${separator}` - +newRevision}
viewType="split"
diffType={type}
hunks={hunks}
>
{hunks => hunks.map(hunk => <Hunk key={hunk.content} hunk={hunk} />)}
</Diff>
</div>
);
};
/**
* Get text based on selection - startup/running
*/
const getStartupText = () => {
if (compareWith == undefined || compareWith === "startup") {
return i18n.label_startup_configuration;
} else {
return i18n.label_running_configuration;
}
};
const statusDisplayDetails = getComplianceIcon("COMPLIANT", true);
const DiffData_Loading = () => {
let loderLabel;
let loderLabelExtraTime = i18n.take_while_text;
if (props.runningFile)
loderLabel =
props.runningFile.totalNoOfLines < 5000
? "Loading..."
: loderLabelExtraTime;
return (
<div className="loading-icon">
<DnxLoader color="#026E97" size="54" label={loderLabel} />
</div>
);
};
/**
* Function returns the DnxBanner information text for diff Position Mismatch
*/
const _diffPositionMismatchInfoText = () => {
const informationConfig = {
message: i18n.diff_postion_mismatch_info,
type: "information"
};
return (
<div className="margin-5px">
<DnxBanner name="banner-loading" config={informationConfig}></DnxBanner>
</div>
);
};
/**
* return function
*/
return (
<div className="">
<div>
{startUpFile && runningFile ? (
<div>
{showLoader ? (
DiffData_Loading()
) : (
<div>
<div className="flex margin-left-5px">
<div className="startup-text">
{getStartupText()} ({startUpFile.totalNoOfLines}{" "}
{i18n.label_lines}) -{" "}
{getFormattedDate(startUpFile.createdTime)}
</div>
<div className="running-text">
{i18n.label_running_configuration} (
{runningFile.totalNoOfLines} {i18n.label_lines}) -{" "}
{getFormattedDate(runningFile.createdTime)}
</div>
</div>
{diffArr &&
props.startupConfigData !== props.runningConfigData ? (
<div>
{isLoading && DiffData_Loading()}
{!isLoading && (
<div>
{diffArr.map(({ diffText, start, end }, idx) =>
parseDiff(diffText, { nearbySequences: "zip" }).map(
file => renderFile(file, start, end, idx === 0)
)
)}
</div>
)}
</div>
) : (
<div className="div-margin-diff">
{statusDisplayDetails}
<span>
{compareWith === "running"
? i18n.label_running_config_compliant
: i18n.label_running_startup_config_compliant}
</span>
</div>
)}
</div>
)}
</div>
) : (
<div className="div-margin-diff">
<span>{i18n.label_running_config_not_available}</span>
</div>
)}
</div>
{_diffPositionMismatchInfoText()}
</div>
);
};
export default DiffViewer;