cabbie-async
Version:
An asynchronous webdriver client
116 lines (85 loc) • 3.87 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
var _regenerator = require('babel-runtime/regenerator');
var _regenerator2 = _interopRequireDefault(_regenerator);
var _debug = require('../debug');
var _thenSleep = require('./then-sleep');
var _thenSleep2 = _interopRequireDefault(_thenSleep);
var _flowRuntime = require('flow-runtime');
var _flowRuntime2 = _interopRequireDefault(_flowRuntime);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
// fool flow runtime checks
var T = _flowRuntime2.default.type('T', _flowRuntime2.default.any());
/**
* Retry a function until it stops returning null/undefined, up to a default timeout of 5 seconds.
*/
/**
*
* This file is generated automatically, run npm run build to re-generate.
**/
exports.default = _flowRuntime2.default.annotate(function waitFor /*:: <T>*/(fn /*: () => Promise<T>*/) /*: Promise<T>*/ {
var timeout /*: number*/ = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 5000;
var T, _fnType, _timeoutType, _returnType, timeoutEnd, count, value;
return _regenerator2.default.async(function waitFor$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
T = _flowRuntime2.default.typeParameter('T');
_fnType = _flowRuntime2.default.function(_flowRuntime2.default.return(_flowRuntime2.default.ref('Promise', T)));
_timeoutType = _flowRuntime2.default.number();
_returnType = _flowRuntime2.default.return(_flowRuntime2.default.union(T, _flowRuntime2.default.ref('Promise', T)));
_flowRuntime2.default.param('fn', _fnType).assert(fn);
_flowRuntime2.default.param('timeout', _timeoutType).assert(timeout);
timeoutEnd = Date.now() + timeout;
count = 0;
case 8:
if (!(Date.now() < timeoutEnd)) {
_context.next = 30;
break;
}
_context.prev = 9;
(0, _debug.startBufferingLogs)();
_context.next = 13;
return _regenerator2.default.awrap(fn());
case 13:
value = _context.sent;
if (!(value !== null && value !== undefined && _flowRuntime2.default.any().assert(value) !== false)) {
_context.next = 17;
break;
}
(0, _debug.printBufferedLogs)();
return _context.abrupt('return', _returnType.assert(value));
case 17:
_context.next = 24;
break;
case 19:
_context.prev = 19;
_context.t0 = _context['catch'](9);
if (_context.t0.code === 'NoSuchElement' || _context.t0.code === 'ElementNotVisible' || _context.t0.code === 'ElementIsNotSelectable' || _context.t0.code === 'NoAlertOpenError' || _context.t0.code === 'ERR_ASSERTION') {
_context.next = 24;
break;
}
(0, _debug.printBufferedLogs)();
throw _context.t0;
case 24:
(0, _debug.discardBufferedLogs)();
_context.next = 27;
return _regenerator2.default.awrap((0, _thenSleep2.default)(count * 20));
case 27:
count++;
_context.next = 8;
break;
case 30:
return _context.abrupt('return', _returnType.assert(fn()));
case 31:
case 'end':
return _context.stop();
}
}
}, null, this, [[9, 19]]);
}, _flowRuntime2.default.function(function (_fn) {
var T = _fn.typeParameter('T');
return [_flowRuntime2.default.param('fn', _flowRuntime2.default.function(_flowRuntime2.default.return(_flowRuntime2.default.ref('Promise', _flowRuntime2.default.flowInto(T))))), _flowRuntime2.default.param('timeout', _flowRuntime2.default.number()), _flowRuntime2.default.return(_flowRuntime2.default.ref('Promise', T))];
}));