Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add timeout option
  • Loading branch information
ExplodingCabbage committed Jan 17, 2024
commit de211d4fff01c9e00892ac08a154bdded4d4a5fd
6 changes: 4 additions & 2 deletions src/diff/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ Diff.prototype = {
if(options.maxEditLength) {
maxEditLength = Math.min(maxEditLength, options.maxEditLength);
}
const maxExecutionTime = options.timeout ?? Infinity;
const abortAfterTimestamp = Date.now() + maxExecutionTime;

let bestPath = [{ oldPos: -1, lastComponent: undefined }];

Expand Down Expand Up @@ -128,7 +130,7 @@ Diff.prototype = {
if (callback) {
(function exec() {
setTimeout(function() {
if (editLength > maxEditLength) {
if (editLength > maxEditLength || Date.now() > abortAfterTimestamp) {
return callback();
}

Expand All @@ -138,7 +140,7 @@ Diff.prototype = {
}, 0);
}());
} else {
while (editLength <= maxEditLength) {
while (editLength <= maxEditLength && Date.now() <= abortAfterTimestamp) {
let ret = execEditLength();
if (ret) {
return ret;
Expand Down