mirror of
https://github.com/node-red/node-red.git
synced 2023-10-10 11:36:53 +00:00
Automatically clear timeouts/intervals set in Function node
closes #744
This commit is contained in:
parent
20b321f928
commit
9c3be40fbe
@ -63,6 +63,8 @@ module.exports = function(RED) {
|
|||||||
this.func+"\n"+
|
this.func+"\n"+
|
||||||
"})(msg);";
|
"})(msg);";
|
||||||
this.topic = n.topic;
|
this.topic = n.topic;
|
||||||
|
this.outstandingTimers = [];
|
||||||
|
this.outstandingIntervals = [];
|
||||||
var sandbox = {
|
var sandbox = {
|
||||||
console:console,
|
console:console,
|
||||||
util:util,
|
util:util,
|
||||||
@ -90,8 +92,31 @@ module.exports = function(RED) {
|
|||||||
context: {
|
context: {
|
||||||
global:RED.settings.functionGlobalContext || {}
|
global:RED.settings.functionGlobalContext || {}
|
||||||
},
|
},
|
||||||
setTimeout: setTimeout,
|
setTimeout: function (func,delay) {
|
||||||
clearTimeout: clearTimeout
|
var timerId = setTimeout(function() {
|
||||||
|
sandbox.clearTimeout(timerId);
|
||||||
|
func();
|
||||||
|
},delay);
|
||||||
|
node.outstandingTimers.push(timerId);
|
||||||
|
},
|
||||||
|
clearTimeout: function(id) {
|
||||||
|
clearTimeout(id);
|
||||||
|
var index = node.outstandingTimers.indexOf(id);
|
||||||
|
if (index > -1) {
|
||||||
|
node.outstandingTimers.splice(index,1);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
setInterval: function(func,delay) {
|
||||||
|
var timerId = setInterval(func,delay);
|
||||||
|
node.outstandingIntervals.push(timerId);
|
||||||
|
},
|
||||||
|
clearInterval: function(id) {
|
||||||
|
clearInterval(id);
|
||||||
|
var index = node.outstandingIntervals.indexOf(id);
|
||||||
|
if (index > -1) {
|
||||||
|
node.outstandingIntervals.splice(index,1);
|
||||||
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
var context = vm.createContext(sandbox);
|
var context = vm.createContext(sandbox);
|
||||||
try {
|
try {
|
||||||
@ -135,6 +160,14 @@ module.exports = function(RED) {
|
|||||||
this.error(errorMessage, msg);
|
this.error(errorMessage, msg);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
this.on("close", function() {
|
||||||
|
while(node.outstandingTimers.length > 0) {
|
||||||
|
clearTimeout(node.outstandingTimers.pop())
|
||||||
|
}
|
||||||
|
while(node.outstandingIntervals.length > 0) {
|
||||||
|
clearInterval(node.outstandingIntervals.pop())
|
||||||
|
}
|
||||||
|
})
|
||||||
} catch(err) {
|
} catch(err) {
|
||||||
// eg SyntaxError - which v8 doesn't include line number information
|
// eg SyntaxError - which v8 doesn't include line number information
|
||||||
// so we can't do better than this
|
// so we can't do better than this
|
||||||
|
Loading…
x
Reference in New Issue
Block a user