class Collab::Models::TrackedPosition

Represents a position in the document which is tracked between commits

If the position is deleted through mapping, deleted_at_version will be set, the position will no longer be tracked,

Public Class Methods

resolve(document, *positions, version:) { |*positions| ... } click to toggle source

Resolves a set of positions and yields them to the block given, returning the result of the block The document will be locked IN SHARE MODE during resolution and the block execution Positions should consist of {“pos” => number, “assoc”: number} Returns false if invalid version or any position has been deleted

# File lib/collab/models/tracked_position.rb, line 22
def self.resolve(document, *positions, version:)
  raise false unless document.possibly_saved_version? version

  document.with_lock("FOR SHARE") do
    unless document.document_version == version 
      steps = document.commits.where("document_version > ?", @mapped_to).order(document_version: :asc).pluck(:steps).flatten(1)

      map_results = ::Collab::JS.map_through(steps: steps, pos: positions)["pos"]

      map_results.each_with_index do |r, i|
        return false if r["deleted"]
        positions[i]["pos"] = r["pos"]
      end
    end

    positions.map! do |p|
      document.tracked_positions.current.find_or_initialize_by(pos: p["pos"], assoc: p["assoc"])
    end

    yield(*positions)
  end
end