Skip to content

Instantly share code, notes, and snippets.

@sebv
Last active August 29, 2015 14:24
Show Gist options
  • Save sebv/20c8d08d3a71a98c21c4 to your computer and use it in GitHub Desktop.
Save sebv/20c8d08d3a71a98c21c4 to your computer and use it in GitHub Desktop.
// delay helper, for some reason they left out the clearTimeout in the B implementation
var cancellableDelay = function (ms) {
var t;
return new B(function(resolve,reject) {
t = setTimeout(function() {
resolve();
}, ms);
}).cancellable()
.catch(B.CancellationError, function(e) {
clearTimeout(t);
throw e;
});
};
let verify = async () => {
this.pageLoadDelay = cancellableDelay(timeoutMs)
try { await this.pageLoadDelay } catch(err) { if(err instanceof B.CancellationError) return; }
let ready = await this.checkPageIsReady();
// if we are ready, or we've spend too much time on this
if (ready || (this.pageLoadMs > 0 && (start + this.pageLoadMs) < Date.now())) {
log.debug('Page is ready');
this.pageLoading = false;
} else {
log.debug('Page was not ready, retrying');
await verify();
}
};
await verify();
}
cancelPageLoad () {
log.debug('Unregistering from page readiness notifications');
this.pageLoading = false;
this.pageLoadDelay.cancel();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment