Module: ActionCableNotifications::Channel::Cache

Included in:
ActionCableNotifications::Channel
Defined in:
lib/action_cable_notifications/channel_cache.rb

Instance Method Summary collapse

Instance Method Details

#initialize(*args) ⇒ Object



7
8
9
10
# File 'lib/action_cable_notifications/channel_cache.rb', line 7

def initialize(*args)
  super
  @cache = HashDB::Base.new()
end

#update_cache(packet) ⇒ Object

Updates server side cache of client side collections XXX compute cache diff before sending to clients



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/action_cable_notifications/channel_cache.rb', line 53

def update_cache(packet)
  updated = false

  # Check if collection already exists
  new_collection = false
  if @collections[packet[:collection]].nil?
    @collections[packet[:collection]] = []
    new_collection = true
  end

  collection = @collections[packet[:collection]]

  case packet[:msg]
  when 'update_many'
    if !new_collection
      packet[:data].each do |record|
        current_record = collection.find{|c| c[:id]==record[:id]}
        if current_record
          new_record = current_record.merge(record)
          if new_record != current_record
            current_record.merge!(record)
            updated = true
          end
        end
      end
    end

  when 'upsert_many'
    if new_collection
      packet[:data].each do |record|
        collection.push record
      end
      updated = true
    else
      packet[:data].each do |record|
        current_record = collection.find{|c| c[:id]==record[:id]}
        if current_record
          new_record = current_record.merge(record)
          if new_record != current_record
            current_record.merge!(record)
            updated = true
          end
        else
          collection.push record
          updated = true
        end
      end
    end

  when 'create'
    record = collection.find{|c| c[:id]==packet[:id]}
    if !record
      @collections[packet[:collection]].push packet[:data]
      updated = true
    end

  when 'update'
    record = @collections[packet[:collection]].find{|c| c[:id]==packet[:id]}
    if record
      record.merge!(packet[:data])
      updated = true
    end

  when 'destroy'
    index = @collections[packet[:collection]].find_index{|c| c[:id]==packet[:id]}
    if index
      @collections[packet[:collection]].delete_at(index)
      updated = true
    end

  else
    updated = true
  end

  updated
end

#validate_packet(packet, options = {}) ⇒ Boolean

Validates packet before transmitting the message

Parameters:

  • packet (Hash)

    Packet to be transmitted

  • options (Hash) (defaults to: {})

    Channels options used to validate the packet

Returns:

  • (Boolean)

    <description>



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/action_cable_notifications/channel_cache.rb', line 20

def validate_packet(packet, options = {})
  options = {
  }.merge(options)

  if packet[:msg].in? ['upsert_many', 'create', 'update', 'destroy']
    if packet[:msg].in? ['upsert_many']
      data = packet[:data]
    else
      data = [(packet[:data] || {}).merge({id: packet[:id]})]
    end

    packet_validator = HashDB::Base.new(data)
    data = packet_validator.scoped_collection(options[:scope]).data
    if data.present?
      if packet[:msg].in? ['upsert_many']
        packet[:data] = data
      else
        packet[:data] = data.first
      end
      true
    else
      false
    end
  else
    true
  end

end