Advertisement
Guest User

AllErrorHandler

a guest
Dec 5th, 2017
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. "use strict";
  2.  
  3. (function () {
  4.     var root = this
  5.     var previous_mymodule = root.AllErrorHandler
  6.  
  7.     /**
  8.      * Class providing objects to listen for uncaught errors.
  9.      */
  10.     class AllErrorHandler {
  11.         /**
  12.          * Create a allErrorHandler object.
  13.          * @param {Function} callback - The callback which is called after an occured error event.
  14.          * @param {Boolean} startListening - Chose if the object should start listening immediately.
  15.          */
  16.         constructor(callback, startListening = true) {
  17.             if (!callback) {
  18.                 throw new Error("Missing callback function");
  19.             }
  20.  
  21.             this._callback = callback;
  22.             this._listening = startListening;
  23.             this._setupEvents();
  24.         }
  25.  
  26.         /**
  27.          * Start listening for error events.
  28.          * @return {void}
  29.          */
  30.         startListening() {
  31.             if (!this._listening) {
  32.                 this._setupEvents();
  33.             }
  34.         }
  35.  
  36.         /**
  37.          * Stop listening for error events.
  38.          * @return {void} The x value.
  39.          */
  40.         stopListening() {
  41.             if (this._listening) {
  42.                 this._setupEvents(false);
  43.             }
  44.         }
  45.  
  46.         /**
  47.          * Call noConflict to avoid conflicts over the AllErrorHandler name
  48.          * @return {AllErrorHandler}  Returns a AllErrorHandler constructor.
  49.          */
  50.         noConflict() {
  51.             root.AllErrorHandler = previous_mymodule
  52.             return AllErrorHandler
  53.         }
  54.  
  55.         /**
  56.          * Disposing the class so there would be no memory leak.
  57.          * @return {void}
  58.          */
  59.         dispose() {
  60.             if (this._listening) {
  61.                 this._setupEvents(false);
  62.             }
  63.  
  64.             this._callback = null;
  65.         }
  66.  
  67.         _handler() {
  68.             if(this._listening) {
  69.                 this._callback.apply(null, arguments);
  70.             }
  71.         }
  72.  
  73.         /**
  74.          * It will attatch or detatch the listening event based on the given parametter;
  75.          * @param {Boolean} attatch - if true the object will start listening.
  76.          * @return {void}
  77.          */
  78.         _setupEvents(attatch = true) {
  79.             this._listening = attatch ? true : false;
  80.  
  81.             if (attatch) {
  82.                 if (typeof window !== "undefined") {
  83.                     window.addEventListener("error", this._callback);
  84.                 } else {
  85.                     process.removeAllListeners('uncaughtException');
  86.                     process.addListener('uncaughtException', this._handler.bind(this));
  87.                 }
  88.             } else {
  89.                 if (typeof window !== "undefined") {
  90.                     window.removeEventListener("error", this._callback);
  91.                 } else {
  92.                     process.removeListener('uncaughtException', this._callback);
  93.                 }
  94.             }
  95.         }
  96.     }
  97.     if (typeof define === 'function' && define.amd) {
  98.         define([], AllErrorHandler);
  99.     } else if (typeof exports !== 'undefined') {
  100.         if (typeof module !== 'undefined' && module.exports) {
  101.             exports = module.exports = AllErrorHandler
  102.         }
  103.         exports.AllErrorHandler = AllErrorHandler
  104.     } else {
  105.         root.AllErrorHandler = AllErrorHandler
  106.     }
  107. }).call(this);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement