Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c762978
commit b96c051
Showing
4 changed files
with
70 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// @flow | ||
|
||
type Options = { | ||
rejectTimeoutTime?: number, | ||
checkIntervalTime?: number, | ||
context?: HTMLElement, | ||
selectAll?: boolean, | ||
}; | ||
|
||
const REJECT_TIMEOUT = 10000; | ||
const CHECK_INTERVAL = 500; | ||
|
||
export const waitForExist = ( | ||
selector: string, | ||
options?: Options = {} | ||
): Promise<any> => { | ||
const { | ||
rejectTimeoutTime = REJECT_TIMEOUT, | ||
checkIntervalTime = CHECK_INTERVAL, | ||
context = document, | ||
selectAll, | ||
} = options; | ||
return new Promise((resolve, reject) => { | ||
const rejectTimeout = setTimeout(() => { | ||
clearInterval(checkInterval); | ||
reject(new Error('Element(s) not found')); | ||
}, rejectTimeoutTime); | ||
|
||
const doResolve = selection => { | ||
resolve(selection); | ||
clearInterval(checkInterval); | ||
clearTimeout(rejectTimeout); | ||
}; | ||
|
||
const checkAll = () => { | ||
const selection = context.querySelectorAll(selector); | ||
if (selection.length) doResolve(selection); | ||
}; | ||
|
||
const checkSingle = () => { | ||
const selection = context.querySelector(selector); | ||
if (selection) doResolve(selection); | ||
}; | ||
|
||
const check = selectAll ? checkAll : checkSingle; | ||
|
||
const checkInterval = setInterval(check, checkIntervalTime); | ||
check(); | ||
}); | ||
}; |