Skip to content

Instantly share code, notes, and snippets.

@weizman
Created September 1, 2021 05:27
Show Gist options
  • Save weizman/554099094409d6f67a4618586c6d10bf to your computer and use it in GitHub Desktop.
Save weizman/554099094409d6f67a4618586c6d10bf to your computer and use it in GitHub Desktop.
// callback to be called when function is debugged
const onFunctionDebugged = (function (info) {
alert(`
Function ${info.function_name} is being debugged!
It was called with the following arguments: ${JSON.stringify(info.function_arguments)}
First time it happened: ${info.first_timestamp}
Last time it happened: ${info.last_timestamp}
`);
});
const initTracker = (function() {
let callstack = [], callargs = [];
setTimeout(() => {
// reset callstack when synchronized debugging is over
callstack = [];
callargs = [];
});
return (function(name, args = []) {
const dummy = /./;
const firstDebugTime = new Date();
args = Array.prototype.slice.call(args);
// register the callback to be called when the function is being debugged
dummy.toString = function () {
// avoid being double called for the same debugged function
if (callstack[callstack.length - 1] === name) {
let same = true;
for (let i = 0; i < args.length; i++) {
if (args[i] !== callargs[callargs.length - 1][i]) {
same = false;
break;
}
}
if (same) return;
}
// fire "function is being debugged" event
callargs.push(args);
callstack.push(name);
onFunctionDebugged({
function_name: name,
function_arguments: args,
first_timestamp: firstDebugTime,
last_timestamp: new Date()
});
};
return dummy;
});
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment