Commit 6f24518a authored by Randall Leeds's avatar Randall Leeds

Remove excess asynchrony in anchoring

All of the anchoring types are actually synchronous, for now. Having
promise callbacks between the selector->anchor and anchor->range
conversions sometimes creates race conditions when multiple
annotations anchor concurrently. Punt on this for now.
parent 8469c220
...@@ -48,43 +48,41 @@ exports.anchor = (selectors, options = {}) -> ...@@ -48,43 +48,41 @@ exports.anchor = (selectors, options = {}) ->
if fragment? if fragment?
promise = promise.catch -> promise = promise.catch ->
Promise.resolve(FragmentAnchor.fromSelector(fragment, options)) anchor = FragmentAnchor.fromSelector(fragment, options)
.then((a) -> a.toRange(options)) range = anchor.toRange(options)
.then(assertQuote) assertQuote(range)
return range
if range? if range?
promise = promise.catch -> promise = promise.catch ->
Promise.resolve(RangeAnchor.fromSelector(range, options)) anchor = RangeAnchor.fromSelector(range, options)
.then((a) -> a.toRange(options)) range = anchor.toRange(options)
.then(assertQuote) assertQuote(range)
return range
if position? if position?
promise = promise.catch -> promise = promise.catch ->
Promise.resolve(TextPositionAnchor.fromSelector(position, options)) anchor = TextPositionAnchor.fromSelector(position, options)
.then((a) -> a.toRange(options)) range = anchor.toRange(options)
.then(assertQuote) assertQuote(range)
return range
if quote? if quote?
promise = promise.catch -> promise = promise.catch ->
Promise.resolve(TextQuoteAnchor.fromSelector(quote, options)) anchor = TextQuoteAnchor.fromSelector(quote, options)
.then((a) -> a.toRange(options)) return anchor.toRange(options)
return promise return promise
exports.describe = (range, options = {}) -> exports.describe = (range, options = {}) ->
maybeDescribeWith = (type) -> types = [FragmentAnchor, RangeAnchor, TextPositionAnchor, TextQuoteAnchor]
return Promise.resolve(type)
.then((t) -> t.fromRange(range, options))
.then((a) -> a.toSelector(options))
.catch(-> null)
selectors = (maybeDescribeWith(type) for type in [ selectors = for type in types
FragmentAnchor try
RangeAnchor anchor = type.fromRange(range, options)
TextPositionAnchor selector = anchor.toSelector(options)
TextQuoteAnchor catch
]) continue
return Promise.all(selectors) return selectors
.then((selectors) -> (s for s in selectors when s?))
...@@ -225,7 +225,6 @@ exports.describe = (range, options = {}) -> ...@@ -225,7 +225,6 @@ exports.describe = (range, options = {}) ->
r.setEndAfter(endRange.end) r.setEndAfter(endRange.end)
pageOptions = {root: startTextLayer} pageOptions = {root: startTextLayer}
quote = Promise.resolve(TextQuoteAnchor.fromRange(r, pageOptions)) quote = TextQuoteAnchor.fromRange(r, pageOptions).toSelector()
.then((a) -> a.toSelector())
return Promise.all([position, quote]) return Promise.all([position, quote])
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