postal.diagnostics
Version:
Wiretap add-on for postal.js allowing configurable console.logging (or other) output of messages being published through postal's message bus.
8 lines • 2.05 kB
JavaScript
/**
* postal.diagnostics - Wiretap add-on for postal.js allowing configurable console.logging (or other) output of messages being published through postal's message bus.
* Author: Jim Cowart (http://freshbrewedcode.com/jimcowart)
* Version: v0.7.4
* Url: https://github.com/postaljs/postal.diagnostics
* License(s): MIT, GPL
*/
(function(t,i){"function"==typeof define&&define.amd?define(["lodash","postal"],function(e,n){return i(e,n,t)}):"object"==typeof module&&module.exports?module.exports=function(t){return i(require("lodash"),t)}:i(t._,t.postal,t)})("undefined"!=typeof window?window:this,function(t,i){i.diagnostics={};var e=function(e){e=e||{},t.extend(this,{active:!0,filters:[],name:t.uniqueId("wiretap_"),includeNesting:!1},e),this.removeWireTap=i.addWireTap(t.bind(this.wiretapFn,this)),i.diagnostics[this.name]&&i.diagnostics[this.name].removeWireTap(),i.diagnostics[this.name]=this};return e.prototype.wiretapFn=function(i,e,n){if(this.active&&(!this.filters.length||t.any(this.filters,function(t){return this.applyFilter(t,e)},this)))try{this.writer(this.serialize(e),n)}catch(r){this.writer("Unable to serialize envelope: "+r)}},e.prototype.applyFilter=function(i,e){var n=0,r=0;return t.each(i,function(i,s){e[s]&&(r++,t.isRegExp(i)&&i.test(e[s])?n++:t.isObject(e[s])&&!t.isArray(e[s])?this.applyFilter(i,e[s])&&n++:t.isEqual(e[s],i)&&n++)},this),n===r},e.prototype.clearFilters=function(){this.filters=[]},e.prototype.removeFilter=function(i){this.filters=t.filter(this.filters,function(e){return!t.isEqual(e,i)})},e.prototype.addFilter=function(i){t.isArray(i)||(i=[i]),t.each(i,function(i){0!==this.filters.length&&t.any(this.filters,function(e){return t.isEqual(e,i)})||this.filters.push(i)},this)},e.prototype.serialize=function(t){if("undefined"==typeof JSON)throw new Error("This browser or environment does not provide JSON support");return JSON.stringify(t,null,4)},e.prototype.writer=function(t,i){this.includeNesting?console.log(t," (nesting level: ",i,")"):console.log(t)},i.diagnostics.DiagnosticsWireTap=e,e});