Sha256: 6159834a7e3da000e49119e9ee09ab397735cbc2811eb7f43485f50b888be154

Contents?: true

Size: 1.77 KB

Versions: 16

Compression:

Stored size: 1.77 KB

Contents

require_relative 'live_query/data_store'
require_relative 'live_query/live_query_pool'

class QueryTasks < Volt::TaskHandler
  @@live_query_pool = LiveQueryPool.new(DataStore.new)
  @@channel_live_queries = {}

  def self.live_query_pool
    @@live_query_pool
  end

  # The dispatcher passes its self in
  def initialize(channel, dispatcher = nil)
    @channel = channel
    @dispatcher = dispatcher
  end

  def add_listener(collection, query)
    live_query = @@live_query_pool.lookup(collection, query)
    track_channel_in_live_query(live_query)

    # puts "Load data on #{collection.inspect} - #{query.inspect}"
    live_query.add_channel(@channel)

    errors = {}

    begin
      # Get the initial data
      initial_data = live_query.initial_data
    rescue => exception
      # Capture and pass up any exceptions
      error = { error: exception.message }
    end

    [initial_data, error]
  end

  def initial_data
    data = live_query.initial_data
    data[:_id] = data[:_id].to_s

    data
  end

  # Remove a listening channel, the LiveQuery will automatically remove
  # itsself from the pool when there are no channels.
  def remove_listener(collection, query)
    live_query = @@live_query_pool.lookup(collection, query)
    live_query.remove_channel(@channel)
  end

  # Removes a channel from all associated live queries
  def close!
    live_queries = @@channel_live_queries[@channel]

    if live_queries
      live_queries.each do |live_query|
        live_query.remove_channel(@channel)
      end
    end

    @@channel_live_queries.delete(@channel)
  end

  private

  # Tracks that this channel will be notified from the live query.
  def track_channel_in_live_query(live_query)
    @@channel_live_queries[@channel] ||= []
    @@channel_live_queries[@channel] << live_query
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
volt-0.8.27.beta3 app/volt/tasks/query_tasks.rb
volt-0.8.27.beta2 app/volt/tasks/query_tasks.rb
volt-0.8.27.beta1 app/volt/tasks/query_tasks.rb
volt-0.8.26.beta1 app/volt/tasks/query_tasks.rb
volt-0.8.26 app/volt/tasks/query_tasks.rb
volt-0.8.24 app/volt/tasks/query_tasks.rb
volt-0.8.23 app/volt/tasks/query_tasks.rb
volt-0.8.22 app/volt/tasks/query_tasks.rb
volt-0.8.22.beta2 app/volt/tasks/query_tasks.rb
volt-0.8.22.beta1 app/volt/tasks/query_tasks.rb
volt-0.8.21 app/volt/tasks/query_tasks.rb
volt-0.8.20 app/volt/tasks/query_tasks.rb
volt-0.8.19 app/volt/tasks/query_tasks.rb
volt-0.8.18 app/volt/tasks/query_tasks.rb
volt-0.8.17 app/volt/tasks/query_tasks.rb
volt-0.8.16 app/volt/tasks/query_tasks.rb