From 7ced683e2c0bbc1b666e0b01799788810cebfd98 Mon Sep 17 00:00:00 2001 From: Melissa Zhang Date: Wed, 29 Jul 2020 14:38:09 -0700 Subject: keep trying to scroll until annotations have loaded & scrolling is successful --- src/client/apis/hypothesis/HypothesisUtils.ts | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/client/apis/hypothesis/HypothesisUtils.ts b/src/client/apis/hypothesis/HypothesisUtils.ts index e15620a91..e245552b1 100644 --- a/src/client/apis/hypothesis/HypothesisUtils.ts +++ b/src/client/apis/hypothesis/HypothesisUtils.ts @@ -69,9 +69,23 @@ export namespace Hypothesis { }; export const scrollToAnnotation = (annotationId: string) => { - document.dispatchEvent(new CustomEvent("scrollToAnnotation", { - detail: annotationId, - bubbles: true - })); + var success = false; + const onSuccess = () => { + console.log("scroll success!!"); + document.removeEventListener('scrollSuccess', onSuccess); + clearTimeout(interval); + success = true; + }; + + const interval = setInterval(() => { // keep trying to scroll every 200ms until annotations have loaded and scrolling is successful + console.log("send scroll"); + document.dispatchEvent(new CustomEvent('scrollToAnnotation', { + detail: annotationId, + bubbles: true + })); + }, 200); + + document.addEventListener('scrollSuccess', onSuccess); // listen for success message from client + setTimeout(() => !success && clearTimeout(interval), 10000); // give up if no success after 10s }; } \ No newline at end of file -- cgit v1.2.3-70-g09d2