File: monitor.js

package info (click to toggle)
node-tad 3.1.1%2B~cs11.22.49-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 3,400 kB
  • sloc: javascript: 25,549; makefile: 6
file content (39 lines) | stat: -rw-r--r-- 1,162 bytes parent folder | download | duplicates (2)
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
/* eslint no-console: "off" */

// Run if you want to monitor unresolved promises (in properly working
// application there should be no promises that are never resolved)

"use strict";

var max        = Math.max
  , isValue    = require("es5-ext/object/is-value")
  , callable   = require("es5-ext/object/valid-callable")
  , isCallable = require("es5-ext/object/is-callable")
  , toPosInt   = require("es5-ext/number/to-pos-integer")
  , deferred   = require("./deferred");

exports = module.exports = function (timeout, cb) {
	if (timeout === false) {
		// Cancel monitor
		delete deferred._monitor;
		delete exports.timeout;
		delete exports.callback;
		return;
	}
	exports.timeout = timeout = max(toPosInt(timeout) || 5000, 50);
	if (isValue(cb)) {
		callable(cb);
	} else if (typeof console !== "undefined" && console && isCallable(console.error)) {
		cb = function (e) {
			console.error(
				(e.stack && e.stack.toString()) || "Unresolved promise: no stack available"
			);
		};
	}
	exports.callback = cb;

	deferred._monitor = function () {
		var e = new Error("Unresolved promise");
		return setTimeout(function () { if (cb) cb(e); }, timeout);
	};
};