Module: Collab::Channel

Included in:
CollabDocumentChannel
Defined in:
lib/collab/channel.rb

Instance Method Summary collapse

Instance Method Details

#commit(data) ⇒ Object



24
25
26
27
# File 'lib/collab/channel.rb', line 24

def commit(data)
  authorize_commit!(data)
  document.commit_later(data)
end

#documentObject



3
# File 'lib/collab/channel.rb', line 3

def document; @document end

#subscribedObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/collab/channel.rb', line 5

def subscribed
  @document = find_document

  starting_version = params[:startingVersion]&.to_i
  raise "missing startingVersion" if starting_version.nil?

  stream_for document
  
  commits = document.commits
                    .where("document_version > ?", starting_version)
                    .order(document_version: :asc)
                    .load
          
  unless commits.empty?
    raise "invalid version" unless commits.first.document_version == (starting_version + 1)
    commits.lazy.map(&:as_json).each(method(:transmit))
  end
end

#unsubscribedObject



29
30
31
# File 'lib/collab/channel.rb', line 29

def unsubscribed
  stop_all_streams # this may not be needed
end