Skip to content

Instantly share code, notes, and snippets.

@aaronmccall
Last active August 29, 2015 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aaronmccall/ee951b71fe1714226e58 to your computer and use it in GitHub Desktop.
Save aaronmccall/ee951b71fe1714226e58 to your computer and use it in GitHub Desktop.
some useful &.js debugging utils
var each = require('amp-each');
var isFunction = require('amp-is-function');
function debugMethods(prototype, handler) {
each(prototype, function (prop, name) {
if (isFunction(handler)) return handler(prop, name);
if (isFunction(prop)) {
prototype[name] = addLogger(prop, name);
}
});
}
exports.debugMethods = debugMethods;
function addLogger(fn, name) {
return function () {
console.log('[Function] %s: %o', name, arguments);
return fn.apply(this, arguments);
};
}
exports.addLogger = addLogger;
function debugDerived(prototype) {
debugMethods(prototype._derived, function (prop, name) {
if (isFunction(prop.fn)) {
prop.fn = addLogger(prop.fn, name + ':derived');
}
});
}
exports.debugDerived = debugDerived;
function mixin(constructor) {
constructor.debug = function (debugDerived) {
if (this._debug) return this;
this._debug = true;
debugMethods(this.prototype);
if (debugDerived) debugDerived(this.prototype);
return this;
};
}
exports.mixin = mixin;
var State = require('ampersand-state');
var debugMixin = require('./debug').mixin;
var MyState = module.exports = State.extend({/* my defs here */});
debugMixin(MyState);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment