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) -> ...@@ -27,18 +27,15 @@ getPageTextContent = (pageIndex) ->
.then((textContent) -> (item.str for item in textContent.items).join('')) .then((textContent) -> (item.str for item in textContent.items).join(''))
# XXX: This will break if the viewer changes documents getPageOffset = (pageIndex, cache = {}) ->
_pageOffsetCache = {}
getPageOffset = (pageIndex) ->
index = -1 index = -1
if _pageOffsetCache[pageIndex]? if cache[pageIndex]?
return Promise.resolve(_pageOffsetCache[pageIndex]) return Promise.resolve(cache[pageIndex])
next = (offset) -> next = (offset) ->
if ++index is pageIndex if ++index is pageIndex
_pageOffsetCache[pageIndex] = offset cache[pageIndex] = offset
return Promise.resolve(offset) return Promise.resolve(offset)
return getPageTextContent(index) return getPageTextContent(index)
...@@ -137,7 +134,7 @@ exports.anchor = (selectors, options = {}) -> ...@@ -137,7 +134,7 @@ exports.anchor = (selectors, options = {}) ->
pageSearches = for pageIndex in [0...pagesCount] pageSearches = for pageIndex in [0...pagesCount]
page = getPage(pageIndex) page = getPage(pageIndex)
content = getPageTextContent(pageIndex) content = getPageTextContent(pageIndex)
offset = getPageOffset(pageIndex) offset = getPageOffset(pageIndex, options.cache)
Promise.all([content, offset, page]).then (results) -> Promise.all([content, offset, page]).then (results) ->
[content, offset, page] = results [content, offset, page] = results
pageOptions = {root: {textContent: content}} pageOptions = {root: {textContent: content}}
...@@ -181,7 +178,7 @@ exports.describe = (range, options = {}) -> ...@@ -181,7 +178,7 @@ exports.describe = (range, options = {}) ->
start = seek(iter, range.start) start = seek(iter, range.start)
end = seek(iter, range.end) + start + range.end.textContent.length 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 # XXX: range covers only one page
start += pageOffset start += pageOffset
end += pageOffset end += pageOffset
......
...@@ -40,6 +40,7 @@ module.exports = class Guest extends Annotator ...@@ -40,6 +40,7 @@ module.exports = class Guest extends Annotator
super super
this.anchors = [] this.anchors = []
this.anchoringCache = {}
cfOptions = cfOptions =
on: (event, handler) => on: (event, handler) =>
...@@ -159,7 +160,10 @@ module.exports = class Guest extends Annotator ...@@ -159,7 +160,10 @@ module.exports = class Guest extends Annotator
annotation.target ?= [] annotation.target ?= []
locate = (target) -> locate = (target) ->
options = {ignoreSelector: '[class^="annotator-"]'} options = {
cache: self.anchoringCache
ignoreSelector: '[class^="annotator-"]'
}
return new Promise(raf) return new Promise(raf)
.then(-> anchoring.anchor(target.selector, options)) .then(-> anchoring.anchor(target.selector, options))
.then((range) -> {annotation, target, range}) .then((range) -> {annotation, target, range})
...@@ -214,11 +218,16 @@ module.exports = class Guest extends Annotator ...@@ -214,11 +218,16 @@ module.exports = class Guest extends Annotator
return annotation return annotation
createAnnotation: (annotation = {}) -> createAnnotation: (annotation = {}) ->
self = this
ranges = @selectedRanges ? [] ranges = @selectedRanges ? []
@selectedRanges = null @selectedRanges = null
getSelectors = (range) -> getSelectors = (range) ->
options = {ignoreSelector: '[class^="annotator-"]'} options = {
cache: self.anchoringCache
ignoreSelector: '[class^="annotator-"]'
}
return anchoring.describe(range, options) return anchoring.describe(range, options)
setDocumentInfo = ({metadata, uri}) -> 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