Class: React::IsomorphicHelpers::IsomorphicProcCall

Inherits:
Object
  • Object
show all
Defined in:
lib/reactive-ruby/isomorphic_helpers.rb

Instance Method Summary collapse

Constructor Details

#initialize(name, block, *args) ⇒ IsomorphicProcCall

Returns a new instance of IsomorphicProcCall.



143
144
145
146
147
# File 'lib/reactive-ruby/isomorphic_helpers.rb', line 143

def initialize(name, block, *args)
  @name = name
  block.call(self, *args)
  @result ||= send_to_server(*args) if IsomorphicHelpers.on_opal_server?
end

Instance Method Details

#result ⇒ Object



139
140
141
# File 'lib/reactive-ruby/isomorphic_helpers.rb', line 139

def result
  @result.first if @result
end

#send_to_server(*args) ⇒ Object



153
154
155
156
157
158
# File 'lib/reactive-ruby/isomorphic_helpers.rb', line 153

def send_to_server(*args)
  if IsomorphicHelpers.on_opal_server?
    args_as_json = args.to_json
    @result = [JSON.parse(`Opal.global.ServerSideIsomorphicMethods[#{@name}](#{args_as_json})`)]
  end
end

#when_on_client(&block) ⇒ Object



149
150
151
# File 'lib/reactive-ruby/isomorphic_helpers.rb', line 149

def when_on_client(&block)
  @result = [block.call] if IsomorphicHelpers.on_opal_client?
end

#when_on_server(&block) ⇒ Object



160
161
162
# File 'lib/reactive-ruby/isomorphic_helpers.rb', line 160

def when_on_server(&block)
  @result = [block.call.to_json] unless IsomorphicHelpers.on_opal_client? || IsomorphicHelpers.on_opal_server?
end