File tree 3 files changed +23
-3
lines changed
3 files changed +23
-3
lines changed Original file line number Diff line number Diff line change @@ -17,4 +17,20 @@ function getDocument() {
17
17
return window . document
18
18
}
19
19
20
- export { getDocument , newMutationObserver }
20
+ /*
21
+ * There are browsers for which `setImmediate` is not available. This
22
+ * serves as a polyfill of sorts, adopting `setTimeout` as the closest
23
+ * equivalent
24
+ */
25
+ function getSetImmediate ( ) {
26
+ /* istanbul ignore else */
27
+ if ( typeof setImmediate === 'function' ) {
28
+ return setImmediate
29
+ } else {
30
+ return function setImmediate ( fn ) {
31
+ return setTimeout ( fn , 0 )
32
+ }
33
+ }
34
+ }
35
+
36
+ export { getDocument , newMutationObserver , getSetImmediate }
Original file line number Diff line number Diff line change 1
- import { newMutationObserver , getDocument } from './helpers'
1
+ import { newMutationObserver , getDocument , getSetImmediate } from './helpers'
2
2
3
3
function waitForDomChange ( {
4
4
container = getDocument ( ) ,
@@ -11,6 +11,7 @@ function waitForDomChange({
11
11
} ,
12
12
} = { } ) {
13
13
return new Promise ( ( resolve , reject ) => {
14
+ const setImmediate = getSetImmediate ( )
14
15
const timer = setTimeout ( onTimeout , timeout )
15
16
const observer = newMutationObserver ( onMutation )
16
17
observer . observe ( container , mutationObserverOptions )
@@ -24,9 +25,11 @@ function waitForDomChange({
24
25
resolve ( result )
25
26
}
26
27
}
28
+
27
29
function onMutation ( mutationsList ) {
28
30
onDone ( null , mutationsList )
29
31
}
32
+
30
33
function onTimeout ( ) {
31
34
onDone ( new Error ( 'Timed out in waitForDomChange.' ) , null )
32
35
}
Original file line number Diff line number Diff line change 1
- import { newMutationObserver , getDocument } from './helpers'
1
+ import { newMutationObserver , getDocument , getSetImmediate } from './helpers'
2
2
3
3
function waitForElement (
4
4
callback ,
@@ -22,6 +22,7 @@ function waitForElement(
22
22
const observer = newMutationObserver ( onMutation )
23
23
observer . observe ( container , mutationObserverOptions )
24
24
function onDone ( error , result ) {
25
+ const setImmediate = getSetImmediate ( ) ;
25
26
clearTimeout ( timer )
26
27
setImmediate ( ( ) => observer . disconnect ( ) )
27
28
if ( error ) {
You can’t perform that action at this time.
0 commit comments