import { Task , CancellablePromise } from '@theintern/common' ; import statusCodes from './statusCodes' ; import Session from '../Session' ; import Element from '../Element' ; import { Strategy } from './Locator' ; param session param locator param using param value returns export default function waitForDeleted ( session , locator , using , value ) { let originalTimeout ; return session . getTimeout ( 'implicit' ) . then ( value => { originalTimeout = value ; session . setTimeout ( 'implicit' , 0 ) ; } ) . then ( function ( ) { return new Task ( ( resolve , reject ) => { const startTime = Date . now ( ) ; ( function poll ( ) { if ( Date . now ( ) - startTime > originalTimeout ) { const always = function ( ) { const error = new Error ( ) ; error . status = 21 ; const [ name , message ] = ( < any > statusCodes ) [ error . status ] ; error . name = name ; error . message = message ; reject ( error ) ; } ; session . setTimeout ( 'implicit' , originalTimeout ) . then ( always , always ) ; return ; } locator . find ( using , value ) . then ( poll , function ( error ) { const always = function ( ) { if ( error . name === 'NoSuchElement' ) { resolve ( ) ; } else { reject ( error ) ; } } ; session . setTimeout ( 'implicit' , originalTimeout ) . then ( always , always ) ; } ) ; } ) ( ) ; } ) ; } ) . then ( ( ) => { } ) ; }