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.



125
126
127
128
129
# File 'lib/reactive-ruby/isomorphic_helpers.rb', line 125

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

#resultObject



121
122
123
# File 'lib/reactive-ruby/isomorphic_helpers.rb', line 121

def result
  @result.first if @result
end

#send_to_server(*args) ⇒ Object



135
136
137
138
139
140
# File 'lib/reactive-ruby/isomorphic_helpers.rb', line 135

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

#when_on_client(&block) ⇒ Object



131
132
133
# File 'lib/reactive-ruby/isomorphic_helpers.rb', line 131

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

#when_on_server(&block) ⇒ Object



142
143
144
# File 'lib/reactive-ruby/isomorphic_helpers.rb', line 142

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