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 = {}) ->
if fragment?
promise = promise.catch ->
Promise.resolve(FragmentAnchor.fromSelector(fragment, options))
.then((a) -> a.toRange(options))
.then(assertQuote)
anchor = FragmentAnchor.fromSelector(fragment, options)
range = anchor.toRange(options)
assertQuote(range)
return range
if range?
promise = promise.catch ->
Promise.resolve(RangeAnchor.fromSelector(range, options))
.then((a) -> a.toRange(options))
.then(assertQuote)
anchor = RangeAnchor.fromSelector(range, options)
range = anchor.toRange(options)
assertQuote(range)
return range
if position?
promise = promise.catch ->
Promise.resolve(TextPositionAnchor.fromSelector(position, options))
.then((a) -> a.toRange(options))
.then(assertQuote)
anchor = TextPositionAnchor.fromSelector(position, options)
range = anchor.toRange(options)
assertQuote(range)
return range
if quote?
promise = promise.catch ->
Promise.resolve(TextQuoteAnchor.fromSelector(quote, options))
.then((a) -> a.toRange(options))
anchor = TextQuoteAnchor.fromSelector(quote, options)
return anchor.toRange(options)
return promise
exports.describe = (range, options = {}) ->
maybeDescribeWith = (type) ->
return Promise.resolve(type)
.then((t) -> t.fromRange(range, options))
.then((a) -> a.toSelector(options))
.catch(-> null)
types = [FragmentAnchor, RangeAnchor, TextPositionAnchor, TextQuoteAnchor]
selectors = (maybeDescribeWith(type) for type in [
FragmentAnchor
RangeAnchor
TextPositionAnchor
TextQuoteAnchor
])
selectors = for type in types
try
anchor = type.fromRange(range, options)
selector = anchor.toSelector(options)
catch
continue
return Promise.all(selectors)
.then((selectors) -> (s for s in selectors when s?))
return selectors
......@@ -225,7 +225,6 @@ exports.describe = (range, options = {}) ->
r.setEndAfter(endRange.end)
pageOptions = {root: startTextLayer}
quote = Promise.resolve(TextQuoteAnchor.fromRange(r, pageOptions))
.then((a) -> a.toSelector())
quote = TextQuoteAnchor.fromRange(r, pageOptions).toSelector()
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