Class: Fie::Commander

Inherits:
ActionCable::Channel::Base
  • Object
show all
Defined in:
lib/fie/commander.rb

Constant Summary collapse

@@pools_subjects =
Set.new
@@disable_override =
false

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.commander_name(connection_uuid) ⇒ Object



98
99
100
# File 'lib/fie/commander.rb', line 98

def commander_name(connection_uuid)
  "commander_#{ connection_uuid }"
end

.method_added(name) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/fie/commander.rb', line 102

def method_added(name)
  super_commander_method_names = [
    :subscribed,
    :refresh_view, 
    :identifier,
    :state,
    :unsubscribed,
    :modify_state_using_changelog,
    :execute_js_function,
    :initialize_pools,
    :publish
  ]

  unless @@disable_override || super_commander_method_names.include?(name)
    @@disable_override = true

    restructure_subclass_method_parameters(name)

    @@disable_override = false
  end
end

.pool(subject, &block) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/fie/commander.rb', line 80

def pool(subject, &block)
  @@pools_subjects.add(subject)

  pool_name = Fie::Pools.pool_name(subject)
  define_method("#{ pool_name }_callback") do |object:, sender_uuid: nil, lazy: false|
    @connection_uuid = self.params['identifier']

    unless @connection_uuid == sender_uuid
      @published_object = Marshal.load(object)
      instance_eval(&block)
    end

    if lazy
      Fie::Pools.publish subject, Marshal.load(object), sender_uuid: @connection_uuid
    end
  end
end

Instance Method Details

#connected?Boolean

Returns:

  • (Boolean)


63
64
65
66
# File 'lib/fie/commander.rb', line 63

def connected?
  commander_name = Commander.commander_name(params[:identifier])
  !redis.get(commander_name).nil?
end

#execute_js_function(name, *arguments) ⇒ Object



53
54
55
56
57
58
59
60
61
# File 'lib/fie/commander.rb', line 53

def execute_js_function(name, *arguments)
  ActionCable.server.broadcast \
    Fie::Commander.commander_name(params[:identifier]),
    command: 'execute_function',
    parameters: {
      name: name,
      arguments: arguments
    }
end

#initialize_poolsObject



28
29
30
31
32
33
34
35
# File 'lib/fie/commander.rb', line 28

def initialize_pools
  ActionCable.server.broadcast \
    Fie::Commander.commander_name(params[:identifier]),
    command: 'subscribe_to_pools',
    parameters: {
      subjects: @@pools_subjects.to_a
    }
end

#initialize_state(params) ⇒ Object



14
15
16
17
18
19
20
21
22
# File 'lib/fie/commander.rb', line 14

def initialize_state(params)
  self.state = State.new \
    view_variables: params['view_variables'],
    controller_name: params['controller_name'],
    action_name: params['action_name'],
    uuid: self.params[:identifier]
  
  execute_js_function('Fie.triggerFieReadyEvent')
end

#modify_state_using_changelog(params) ⇒ Object



49
50
51
# File 'lib/fie/commander.rb', line 49

def modify_state_using_changelog(params)
  state.update_object_using_changelog(params['objects_changelog'])
end

#publish(subject, object) ⇒ Object



37
38
39
# File 'lib/fie/commander.rb', line 37

def publish(subject, object)
  Fie::Pools.publish_lazy(subject, object, params[:identifier])
end

#stateObject



41
42
43
# File 'lib/fie/commander.rb', line 41

def state
  Marshal.load redis.get(Fie::Commander.commander_name params[:identifier])
end

#state=(state) ⇒ Object



45
46
47
# File 'lib/fie/commander.rb', line 45

def state=(state)
  redis.set Fie::Commander.commander_name(params[:identifier]), Marshal.dump(state)
end

#subscribedObject



10
11
12
# File 'lib/fie/commander.rb', line 10

def subscribed
  stream_from Commander.commander_name(params['identifier'])
end

#unsubscribedObject



24
25
26
# File 'lib/fie/commander.rb', line 24

def unsubscribed
  redis.del Fie::Commander.commander_name(params[:identifier])
end