Blame view

node_modules/when/lib/decorators/unhandledRejection.js 2.11 KB
ce4c83ff   wxy   初始提交
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
  /** @license MIT License (c) copyright 2010-2014 original author or authors */
  /** @author Brian Cavalier */
  /** @author John Hann */
  
  (function(define) { 'use strict';
  define(function(require) {
  
  	var setTimer = require('../env').setTimer;
  	var format = require('../format');
  
  	return function unhandledRejection(Promise) {
  		var logError = noop;
  		var logInfo = noop;
  		var localConsole;
  
  		if(typeof console !== 'undefined') {
  			// Alias console to prevent things like uglify's drop_console option from
  			// removing console.log/error. Unhandled rejections fall into the same
  			// category as uncaught exceptions, and build tools shouldn't silence them.
  			localConsole = console;
  			logError = typeof localConsole.error !== 'undefined'
  				? function (e) { localConsole.error(e); }
  				: function (e) { localConsole.log(e); };
  
  			logInfo = typeof localConsole.info !== 'undefined'
  				? function (e) { localConsole.info(e); }
  				: function (e) { localConsole.log(e); };
  		}
  
  		Promise.onPotentiallyUnhandledRejection = function(rejection) {
  			enqueue(report, rejection);
  		};
  
  		Promise.onPotentiallyUnhandledRejectionHandled = function(rejection) {
  			enqueue(unreport, rejection);
  		};
  
  		Promise.onFatalRejection = function(rejection) {
  			enqueue(throwit, rejection.value);
  		};
  
  		var tasks = [];
  		var reported = [];
  		var running = null;
  
  		function report(r) {
  			if(!r.handled) {
  				reported.push(r);
  				logError('Potentially unhandled rejection [' + r.id + '] ' + format.formatError(r.value));
  			}
  		}
  
  		function unreport(r) {
  			var i = reported.indexOf(r);
  			if(i >= 0) {
  				reported.splice(i, 1);
  				logInfo('Handled previous rejection [' + r.id + '] ' + format.formatObject(r.value));
  			}
  		}
  
  		function enqueue(f, x) {
  			tasks.push(f, x);
  			if(running === null) {
  				running = setTimer(flush, 0);
  			}
  		}
  
  		function flush() {
  			running = null;
  			while(tasks.length > 0) {
  				tasks.shift()(tasks.shift());
  			}
  		}
  
  		return Promise;
  	};
  
  	function throwit(e) {
  		throw e;
  	}
  
  	function noop() {}
  
  });
  }(typeof define === 'function' && define.amd ? define : function(factory) { module.exports = factory(require); }));