Skip to content

Commit

Permalink
Improve feedback display when tracing events
Browse files Browse the repository at this point in the history
  • Loading branch information
Alhadis committed May 6, 2020
1 parent bf351a8 commit 57f5aa7
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 23 deletions.
1 change: 1 addition & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
56 changes: 56 additions & 0 deletions lib/utils/diagnostics.js
Original file line number Diff line number Diff line change
@@ -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<void>}
*/
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<void>}
*/
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;
}
}
24 changes: 1 addition & 23 deletions lib/utils/global.js
Original file line number Diff line number Diff line change
Expand Up @@ -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("");
Expand Down

0 comments on commit 57f5aa7

Please sign in to comment.