fix(scheduled-tasks): avoid clearing toastr auto-dismiss timers in taskjs sandbox
This commit is contained in:
@@ -679,6 +679,9 @@ async function executeTaskJS(jsCode, taskName = 'AnonymousTask') {
|
|||||||
const listeners = new Set();
|
const listeners = new Set();
|
||||||
const createdNodes = new Set();
|
const createdNodes = new Set();
|
||||||
const waiters = new Set();
|
const waiters = new Set();
|
||||||
|
let suppressTimerTracking = false;
|
||||||
|
const originalToastrFns = {};
|
||||||
|
const toastrMethods = ['info', 'success', 'warning', 'error'];
|
||||||
|
|
||||||
const notifyActivityChange = () => {
|
const notifyActivityChange = () => {
|
||||||
if (waiters.size === 0) return;
|
if (waiters.size === 0) return;
|
||||||
@@ -689,16 +692,36 @@ async function executeTaskJS(jsCode, taskName = 'AnonymousTask') {
|
|||||||
|
|
||||||
window.setTimeout = function(fn, t, ...args) {
|
window.setTimeout = function(fn, t, ...args) {
|
||||||
const id = originals.setTimeout(function(...inner) {
|
const id = originals.setTimeout(function(...inner) {
|
||||||
try { fn?.(...inner); } finally { timeouts.delete(id); notifyActivityChange(); }
|
try { fn?.(...inner); }
|
||||||
|
finally {
|
||||||
|
if (timeouts.delete(id)) notifyActivityChange();
|
||||||
|
}
|
||||||
}, t, ...args);
|
}, t, ...args);
|
||||||
|
if (!suppressTimerTracking) {
|
||||||
timeouts.add(id);
|
timeouts.add(id);
|
||||||
notifyActivityChange();
|
notifyActivityChange();
|
||||||
|
}
|
||||||
return id;
|
return id;
|
||||||
};
|
};
|
||||||
window.clearTimeout = function(id) { originals.clearTimeout(id); timeouts.delete(id); notifyActivityChange(); };
|
window.clearTimeout = function(id) {
|
||||||
|
originals.clearTimeout(id);
|
||||||
|
if (timeouts.delete(id)) notifyActivityChange();
|
||||||
|
};
|
||||||
window.setInterval = function(fn, t, ...args) { const id = originals.setInterval(fn, t, ...args); intervals.add(id); notifyActivityChange(); return id; };
|
window.setInterval = function(fn, t, ...args) { const id = originals.setInterval(fn, t, ...args); intervals.add(id); notifyActivityChange(); return id; };
|
||||||
window.clearInterval = function(id) { originals.clearInterval(id); intervals.delete(id); notifyActivityChange(); };
|
window.clearInterval = function(id) { originals.clearInterval(id); intervals.delete(id); notifyActivityChange(); };
|
||||||
|
|
||||||
|
if (window.toastr) {
|
||||||
|
for (const method of toastrMethods) {
|
||||||
|
if (typeof window.toastr[method] !== 'function') continue;
|
||||||
|
originalToastrFns[method] = window.toastr[method];
|
||||||
|
window.toastr[method] = function(...fnArgs) {
|
||||||
|
suppressTimerTracking = true;
|
||||||
|
try { return originalToastrFns[method].apply(window.toastr, fnArgs); }
|
||||||
|
finally { suppressTimerTracking = false; }
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const addListenerEntry = (entry) => { listeners.add(entry); notifyActivityChange(); };
|
const addListenerEntry = (entry) => { listeners.add(entry); notifyActivityChange(); };
|
||||||
const removeListenerEntry = (target, type, listener, options) => {
|
const removeListenerEntry = (target, type, listener, options) => {
|
||||||
let removed = false;
|
let removed = false;
|
||||||
@@ -736,6 +759,13 @@ async function executeTaskJS(jsCode, taskName = 'AnonymousTask') {
|
|||||||
Node.prototype.appendChild = originals.appendChild;
|
Node.prototype.appendChild = originals.appendChild;
|
||||||
Node.prototype.insertBefore = originals.insertBefore;
|
Node.prototype.insertBefore = originals.insertBefore;
|
||||||
Node.prototype.replaceChild = originals.replaceChild;
|
Node.prototype.replaceChild = originals.replaceChild;
|
||||||
|
if (window.toastr) {
|
||||||
|
for (const method of toastrMethods) {
|
||||||
|
if (typeof originalToastrFns[method] === 'function') {
|
||||||
|
window.toastr[method] = originalToastrFns[method];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const hardCleanup = () => {
|
const hardCleanup = () => {
|
||||||
|
|||||||
Reference in New Issue
Block a user