Sha256: d2274edf473bf59ef99e064e3286197c461edc6d68a43fd1e92df0b2aeeb6ce5

Contents?: true

Size: 1.39 KB

Versions: 2

Compression:

Stored size: 1.39 KB

Contents

require 'deepstream/constants'
require 'deepstream/record'
require 'deepstream/list'

module Deepstream
  class RecordHandler
    def initialize(client)
      @client = client
      @records = {}
    end

    def on_message(message)
      case message.action
      when ACTION::ACK then nil
      when ACTION::PATCH then patch(message)
      when ACTION::READ then read(message)
      when ACTION::UPDATE then update(message)
      else @client.on_error(message)
      end
    end

    def get(name, list: nil)
      if list
        name.prepend("#{list}/")
        @records[list] ||= List.new(@client, list)
        @records[list].add(name)
      end
      @records[name] ||= Record.new(@client, name)
    end

    def get_list(name)
      @records[name] ||= List.new(@client, name)
    end

    def set(name, *args)
      @records[name]&.set(*args)
    end

    def unsubscribe(name)
      @records[name]&.unsubscribe
    end

    def discard(name)
      unsubscribe(name)
    end

    def delete(name)
      @client.send_message(TOPIC::RECORD, ACTION::DELETE, name) if @records.delete(name)
    end

    private

    def read(message)
      name, *data = message.data
      @records[name]&.read(*data)
    end

    def update(message)
      name, *data = message.data
      @records[name]&.update(*data)
    end

    def patch(message)
      name, *data = message.data
      @records[name]&.patch(*data)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
deepstream-0.2.1 lib/deepstream/record_handler.rb
deepstream-0.2.0 lib/deepstream/record_handler.rb