UNPKG

skypager-repl

Version:
81 lines (67 loc) 2.46 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _typeof2 = require('babel-runtime/helpers/typeof'); var _typeof3 = _interopRequireDefault(_typeof2); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } exports.default = function (repl, evalFn) { var realEval = evalFn || repl.eval; var promiseEval = function promiseEval(cmd, context, filename, callback) { realEval.call(repl, cmd, context, filename, function (err, res) { // Error response if (err) { return callback(err); } if (res && typeof res.value === 'function' && res.constructor.name === 'LodashWrapper') { return callback(null, res.value()); } if (!res || typeof res.then != 'function' || typeof res.then === 'function' && (0, _typeof3.default)(res.context) === 'object') { return callback(null, res); } var cancel = function cancel(chunk, key) { repl.outputStream.write('break.\n'); if (key.name === 'escape') { process.stdin.removeListener('keypress', cancel); callback(null, res); callback = function callback() {}; } }; process.stdin.on('keypress', cancel); // Start a timer indicating that escape can be used to quit var hangTimer = setTimeout(function () { repl.outputStream.write('Hit escape to stop waiting on promise\n'); }, 5000); res.then(function (val) { process.stdin.removeListener('keypress', cancel); clearTimeout(hangTimer); callback(null, val); }, function (err) { process.stdin.removeListener('keypress', cancel); clearTimeout(hangTimer); repl.outputStream.write('Promise rejected: '); callback(err); }).then(null, function (uncaught) { // Rethrow uncaught exceptions process.nextTick(function () { throw uncaught; }); }); }); }; repl.eval = promiseEval; repl.commands['promise'] = { help: 'Toggle auto-promise unwrapping', action: function action() { if (repl.eval === promiseEval) { this.outputStream.write('Promise auto-eval disabled\n'); repl.eval = realEval; } else { this.outputStream.write('Promise auto-eval enabled\n'); repl.eval = promiseEval; } this.displayPrompt(); } }; }; //# sourceMappingURL=promise.js.map