diff --git a/packages/common/utils.js b/packages/common/utils.js index e4bae26b0ed2da185be88fbe795b13de56be76d6..99624b4d7b3348e7f2932dfc96aec4af018a374b 100644 --- a/packages/common/utils.js +++ b/packages/common/utils.js @@ -225,4 +225,28 @@ export const getAssetURL = (path) => { // If all fails we just fall back to relative paths and hope the // html is on the same path as the bundle return path; -}; \ No newline at end of file +}; + +/** + * Poll <fn> every <interval> ms until <timeout> ms + * + * @param fn + * @param timeout + * @param interval + */ +export const pollFunc = (fn, timeout, interval) => { + var startTime = (new Date()).getTime(); + interval = interval || 1000; + + (function p() { + // don't retry if we took longer than timeout ms + if (((new Date).getTime() - startTime ) > timeout) { + return; + } + + // retry until fn() returns true + if (!fn()) { + setTimeout(p, interval); + } + })(); +};