UNPKG

cabbie-sync

Version:
69 lines (52 loc) 2.4 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); 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 /*: () => T*/) /*: T*/ { var timeout /*: number*/ = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 5000; var T = _flowRuntime2.default.typeParameter('T'); var _fnType = _flowRuntime2.default.function(_flowRuntime2.default.return(T)); var _timeoutType = _flowRuntime2.default.number(); var _returnType = _flowRuntime2.default.return(T); _flowRuntime2.default.param('fn', _fnType).assert(fn); _flowRuntime2.default.param('timeout', _timeoutType).assert(timeout); var timeoutEnd = Date.now() + timeout; var count = 0; while (Date.now() < timeoutEnd) { try { (0, _debug.startBufferingLogs)(); var value = fn(); if (value !== null && value !== undefined && _flowRuntime2.default.any().assert(value) !== false) { (0, _debug.printBufferedLogs)(); return _returnType.assert(value); } } catch (ex) { if (!(ex.code === 'NoSuchElement' || ex.code === 'ElementNotVisible' || ex.code === 'ElementIsNotSelectable' || ex.code === 'NoAlertOpenError' || ex.code === 'ERR_ASSERTION')) { (0, _debug.printBufferedLogs)(); throw ex; } } (0, _debug.discardBufferedLogs)(); (0, _thenSleep2.default)(count * 20); count++; } return _returnType.assert(fn()); }, _flowRuntime2.default.function(function (_fn) { var T = _fn.typeParameter('T'); return [_flowRuntime2.default.param('fn', _flowRuntime2.default.function(_flowRuntime2.default.return(_flowRuntime2.default.flowInto(T)))), _flowRuntime2.default.param('timeout', _flowRuntime2.default.number()), _flowRuntime2.default.return(T)]; }));