1- import B from 'bluebird' ;
2- import _ from 'lodash' ;
31import type { LongSleepOptions , WaitForConditionOptions } from './types.js' ;
42
53const LONG_SLEEP_THRESHOLD = 5000 ; // anything over 5000ms will turn into a spin
@@ -9,7 +7,7 @@ const LONG_SLEEP_THRESHOLD = 5000; // anything over 5000ms will turn into a spin
97 * @param ms - The number of milliseconds to wait
108 */
119export async function sleep ( ms : number ) : Promise < void > {
12- return await B . delay ( ms ) ;
10+ return new Promise ( ( resolve ) => setTimeout ( resolve , ms ) ) ;
1311}
1412
1513/**
@@ -40,7 +38,7 @@ export async function longSleep(
4038 const post = Date . now ( ) ;
4139 timeLeft = endAt - post ;
4240 elapsedMs = elapsedMs + ( post - pre ) ;
43- if ( _ . isFunction ( progressCb ) ) {
41+ if ( typeof progressCb === 'function' ) {
4442 progressCb ( { elapsedMs, timeLeft, progress : elapsedMs / ms } ) ;
4543 }
4644 } while ( timeLeft > 0 ) ;
@@ -176,11 +174,12 @@ export async function waitForCondition<T>(
176174 condFn : ( ) => Promise < T > | T ,
177175 options : WaitForConditionOptions = { } ,
178176) : Promise < T > {
179- const opts : WaitForConditionOptions & { waitMs : number ; intervalMs : number } = _ . defaults ( options , {
180- waitMs : 5000 ,
181- intervalMs : 500 ,
182- } ) ;
183- const debug = opts . logger ? opts . logger . debug . bind ( opts . logger ) : _ . noop ;
177+ const opts : WaitForConditionOptions & { waitMs : number ; intervalMs : number } = {
178+ ...options ,
179+ waitMs : typeof options . waitMs === 'number' ? options . waitMs : 5000 ,
180+ intervalMs : typeof options . intervalMs === 'number' ? options . intervalMs : 500 ,
181+ } ;
182+ const debug = opts . logger ? opts . logger . debug . bind ( opts . logger ) : ( ) => undefined ;
184183 const error = opts . error ;
185184 const begunAt = Date . now ( ) ;
186185 const endAt = begunAt + opts . waitMs ;
@@ -194,12 +193,12 @@ export async function waitForCondition<T>(
194193 const remainingTime = endAt - now ;
195194 if ( now < endAt ) {
196195 debug ( `Waited for ${ waited } ms so far` ) ;
197- await B . delay ( Math . min ( opts . intervalMs , remainingTime ) ) ;
196+ await sleep ( Math . min ( opts . intervalMs , remainingTime ) ) ;
198197 return await spin ( ) ;
199198 }
200199 // if there is an error option, it is either a string message or an error itself
201200 if ( error ) {
202- throw _ . isString ( error ) ? new Error ( error ) : error ;
201+ throw typeof error === 'string' ? new Error ( error ) : error ;
203202 }
204203 throw new Error ( `Condition unmet after ${ waited } ms. Timing out.` ) ;
205204 } ;
0 commit comments