diff --git a/lib/index.js b/lib/index.js index e39fd58..247f27e 100644 --- a/lib/index.js +++ b/lib/index.js @@ -5,6 +5,7 @@ require("./utils/other.js"); require("./utils/global.js"); require("./utils/buffer.js"); require("./utils/cursor.js"); +require("./utils/diagnostics.js"); // Custom commands require("./commands/display.js"); diff --git a/lib/utils/diagnostics.js b/lib/utils/diagnostics.js new file mode 100644 index 0000000..02d559c --- /dev/null +++ b/lib/utils/diagnostics.js @@ -0,0 +1,56 @@ +"use strict"; + +const prot = atom.emitter.constructor.prototype; +const emit = prot.emit; + +module.exports = { + countdown, + traceEmissions, +}; + + +/** + * Return a {@link Promise} that resolves after a delay, + * counting each second that passes. + * + * @example countdown(10).then(() => console.log("Happy new year!")); + * @param {Number} [ticks=5] - Countdown duration in seconds + * @return {Promise} + */ +async function countdown(ticks = 5){ + return new Promise((resolve, reject) => { + console.log(ticks--); + const timer = setInterval(() => ticks <= 0 + ? resolve(clearInterval(timer)) + : console.log(ticks--), 1000); + }); +} + + +/** + * Toggle logging of event emissions in the dev-console. + * + * @param {Boolean} active - Whether to trace emissions or not + * @param {Number} [duration=5] - Stop tracing after 𝑁 seconds + * @param {Number} [delay=5] - Wait 𝑁 seconds before tracing + * @return {Promise} + */ +async function traceEmissions(active = emit === prot.emit, duration = 0, delay = 0){ + if(duration > 0) setTimeout(() => traceEmissions(false), duration); + if(delay > 0) await countdown(delay); + if(active){ + console.groupCollapsed("Traced emissions"); + prot.emit = function(name, ...args){ + if("did-update-state" !== name){ + const trace = {context: this, arguments}; + Error.captureStackTrace(trace); + console.dir({context: this, arguments, trace, [Symbol.toStringTag]: name}); + } + emit.apply(this, arguments); + }; + } + else{ + console.groupEnd(); + prot.emit = emit; + } +} diff --git a/lib/utils/global.js b/lib/utils/global.js index ae3f65f..aee7f54 100644 --- a/lib/utils/global.js +++ b/lib/utils/global.js @@ -72,29 +72,7 @@ Object.assign(global, { util: require("util"), zlib: require("zlib"), - traceEmissions: (function(){ - const prot = atom.emitter.constructor.prototype; - const emit = prot.emit; - return function(active){ - if(undefined === active) - active = emit === prot.emit; - prot.emit = !active ? emit : function(name, ...args){ - if("did-update-state" !== name){ - const trace = {}; - Error.captureStackTrace(trace); - trace.context = this; - setTimeout(() => { - Object.defineProperty(args, "trace", { - enumerable: false, - value: trace, - }); - }, 10); - console.log(name, args); - } - emit.apply(this, arguments); - }; - }; - }()), + ...require("./diagnostics.js"), bin(value, a){ value = value.toString(2).split("");