Skip to content

Commit 9c1f69d

Browse files
brucouKent C. Dodds
authored and
Kent C. Dodds
committed
fix(#184): replacing setImmediate by setTimeour when not defined (#188)
1 parent 82a57c4 commit 9c1f69d

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

src/helpers.js

+17-1
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,20 @@ function getDocument() {
1717
return window.document
1818
}
1919

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}

src/wait-for-dom-change.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {newMutationObserver, getDocument} from './helpers'
1+
import {newMutationObserver, getDocument, getSetImmediate} from './helpers'
22

33
function waitForDomChange({
44
container = getDocument(),
@@ -11,6 +11,7 @@ function waitForDomChange({
1111
},
1212
} = {}) {
1313
return new Promise((resolve, reject) => {
14+
const setImmediate = getSetImmediate()
1415
const timer = setTimeout(onTimeout, timeout)
1516
const observer = newMutationObserver(onMutation)
1617
observer.observe(container, mutationObserverOptions)
@@ -24,9 +25,11 @@ function waitForDomChange({
2425
resolve(result)
2526
}
2627
}
28+
2729
function onMutation(mutationsList) {
2830
onDone(null, mutationsList)
2931
}
32+
3033
function onTimeout() {
3134
onDone(new Error('Timed out in waitForDomChange.'), null)
3235
}

src/wait-for-element.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {newMutationObserver, getDocument} from './helpers'
1+
import {newMutationObserver, getDocument, getSetImmediate} from './helpers'
22

33
function waitForElement(
44
callback,
@@ -22,6 +22,7 @@ function waitForElement(
2222
const observer = newMutationObserver(onMutation)
2323
observer.observe(container, mutationObserverOptions)
2424
function onDone(error, result) {
25+
const setImmediate = getSetImmediate();
2526
clearTimeout(timer)
2627
setImmediate(() => observer.disconnect())
2728
if (error) {

0 commit comments

Comments
 (0)