Skip to content

Instantly share code, notes, and snippets.

@alanning
Created March 30, 2015 15:53
Show Gist options
  • Save alanning/b1e47e04cfb12d3b1941 to your computer and use it in GitHub Desktop.
Save alanning/b1e47e04cfb12d3b1941 to your computer and use it in GitHub Desktop.
Adds client-side logging of collection activity and template rendered events. Toggled via localStorage.
//////////////////////////////////////////////////////////////////////
// Use localStorage to toggle logging of collection activity and
// template rendering
//
// Put this file in 'client/lib/trace.js'
//
// Toggle via browser console:
// localStorage.setItem('Meteor.traceCollections', true)
// localStorage.setItem('Meteor.traceRendered', true)
//
//////////////////////////////////////////////////////////////////////
"use strict";
Meteor.traceCollections = false;
Meteor.traceRendered = false;
//////////////////////////////////////////////////////////////////////
// Check localStorage flags to see what we should log to console
//
if (supports_html5_storage()) {
Meteor.traceCollections = !!window.localStorage.getItem('Meteor.traceCollections');
Meteor.traceRendered = !!window.localStorage.getItem('Meteor.traceRendered');
}
/**
* Tests whether localstorage is supported in the user's client
* source: http://diveintohtml5.info/storage.html
*
* @method supports_html5_storage
* @returns {Boolean} true if localstorage is supported
*/
function supports_html5_storage () {
try {
return 'localStorage' in window && window['localStorage'] !== null;
} catch (e) {
return false;
}
}
//////////////////////////////////////////////////////////////////////
// traceCollections
//
if (Meteor.traceCollections) {
var Collection,
originalFn;
console.log('[trace] wrapping Collection.find');
Collection = 'undefined' === typeof Mongo ? Meteor.Collection
: Mongo.Collection;
originalFn = Collection.prototype.find;
Collection.prototype.find = function () {
var cursor = originalFn.apply(this, arguments),
collectionName = this._name;
cursor.observeChanges({
added: function (id, fields) {
console.log('[trace]', collectionName, 'added', id, fields);
},
changed: function (id, fields) {
console.log('[trace]', collectionName, 'changed', id, fields);
},
movedBefore: function (id, before) {
console.log('[trace]', collectionName, 'movedBefore', id, before);
},
removed: function (id) {
console.log('[trace]', collectionName, 'removed', id);
}
});
return cursor;
};
}
//////////////////////////////////////////////////////////////////////
// traceRendered
//
if (Meteor.traceRendered) {
Meteor.startup(function () {
var name,
template;
console.log('[trace] wrapping Template.rendered');
for (name in Template) {
template = Template[name];
if (template && template.rendered) {
wrapRendered(template, name);
}
}
})
}
function wrapRendered(template, name) {
Template[name].rendered = _.wrap(template.rendered, function (func) {
var args = Array.prototype.splice.call(arguments, 0, 1);
console.log('[trace] rendered', name);
func.apply(this, args);
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment