Commit d5251526 authored by Randall Leeds's avatar Randall Leeds

Let the application manage an anchoring cache

This is maybe too verbose for now but at least it's explicit and
doesn't assume that anchoring implementations can be fully
responsible for cache invalidation.
parent ddb7fc4a
......@@ -27,18 +27,15 @@ getPageTextContent = (pageIndex) ->
.then((textContent) -> (item.str for item in textContent.items).join(''))
# XXX: This will break if the viewer changes documents
_pageOffsetCache = {}
getPageOffset = (pageIndex) ->
getPageOffset = (pageIndex, cache = {}) ->
index = -1
if _pageOffsetCache[pageIndex]?
return Promise.resolve(_pageOffsetCache[pageIndex])
if cache[pageIndex]?
return Promise.resolve(cache[pageIndex])
next = (offset) ->
if ++index is pageIndex
_pageOffsetCache[pageIndex] = offset
cache[pageIndex] = offset
return Promise.resolve(offset)
return getPageTextContent(index)
......@@ -137,7 +134,7 @@ exports.anchor = (selectors, options = {}) ->
pageSearches = for pageIndex in [0...pagesCount]
page = getPage(pageIndex)
content = getPageTextContent(pageIndex)
offset = getPageOffset(pageIndex)
offset = getPageOffset(pageIndex, options.cache)
Promise.all([content, offset, page]).then (results) ->
[content, offset, page] = results
pageOptions = {root: {textContent: content}}
......@@ -181,7 +178,7 @@ exports.describe = (range, options = {}) ->
start = seek(iter, range.start)
end = seek(iter, range.end) + start + range.end.textContent.length
return getPageOffset(startPageIndex).then (pageOffset) ->
return getPageOffset(startPageIndex, options.cache).then (pageOffset) ->
# XXX: range covers only one page
start += pageOffset
end += pageOffset
......
......@@ -40,6 +40,7 @@ module.exports = class Guest extends Annotator
super
this.anchors = []
this.anchoringCache = {}
cfOptions =
on: (event, handler) =>
......@@ -159,7 +160,10 @@ module.exports = class Guest extends Annotator
annotation.target ?= []
locate = (target) ->
options = {ignoreSelector: '[class^="annotator-"]'}
options = {
cache: self.anchoringCache
ignoreSelector: '[class^="annotator-"]'
}
return new Promise(raf)
.then(-> anchoring.anchor(target.selector, options))
.then((range) -> {annotation, target, range})
......@@ -214,11 +218,16 @@ module.exports = class Guest extends Annotator
return annotation
createAnnotation: (annotation = {}) ->
self = this
ranges = @selectedRanges ? []
@selectedRanges = null
getSelectors = (range) ->
options = {ignoreSelector: '[class^="annotator-"]'}
options = {
cache: self.anchoringCache
ignoreSelector: '[class^="annotator-"]'
}
return anchoring.describe(range, options)
setDocumentInfo = ({metadata, uri}) ->
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment