Debounce `scrolltorange` event handling.
In some cases this event could trigger while a previous event's nav request is still being fulfilled, meaning pagination has not been updated.
Without this debouncing the event handling could invoke two consecutive `reader.openSpineItemElementCfi` calls, which causes a bug in either the views or in CfiNavigationLogic.
MODIFY
js/views/external_agent_support.js
js/views/external_agent_support.js
// OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
// OF THE POSSIBILITY OF SUCH DAMAGE.
-define(["../globals"], function(Globals) {
+define(["../globals", "underscore"], function(Globals, _) {
/**
* This module helps external agents that interact with content documents from
* the level of the iframe browsing context:
@@ -88,13 +88,7 @@ define(["../globals"], function(Globals) {
}
}
- function bindBringIntoViewEvent(contentDocument) {
- // 'scrolltorange' is a non-standard event that is emitted on the content frame
- // by some external tools like Hypothes.is
- contentDocument.addEventListener('scrolltorange', function (event) {
- event.preventDefault();
-
- var range = event.detail;
+ var bringIntoViewDebounced = _.debounce(function (range) {
var target = reader.getRangeCfiFromDomRange(range);
var contentDocumentState = contentDocumentStates[target.idref];
@@ -106,6 +100,16 @@ define(["../globals"], function(Globals) {
contentCFI: target.contentCFI
};
}
+ }, 100);
+
+ function bindBringIntoViewEvent(contentDocument) {
+ // 'scrolltorange' is a non-standard event that is emitted on the content frame
+ // by some external tools like Hypothes.is
+ contentDocument.addEventListener('scrolltorange', function (event) {
+ event.preventDefault();
+
+ var range = event.detail;
+ bringIntoViewDebounced(range);
});
}

Debounce `scrolltorange` event handling.

In some cases this event could trigger while a previous event's nav request is still being fulfilled, meaning pagination has not been updated.
Without this debouncing the event handling could invoke two consecutive `reader.openSpineItemElementCfi` calls, which causes a bug in either the views or in CfiNavigationLogic.