Sha256: e0d73844b18daa4e1cfcbfbb50795a466bf61f5c4cba6805a4f190324a5032b4

Contents?: true

Size: 953 Bytes

Versions: 15

Compression:

Stored size: 953 Bytes

Contents

module ReactiveRuby
  class ComponentLoader
    attr_reader :v8_context
    private :v8_context

    def initialize(v8_context)
      unless v8_context
        raise ArgumentError.new('Could not obtain ExecJS runtime context')
      end
      @v8_context = v8_context
    end

    def load(file = components)
      return true if loaded?
      !!v8_context.eval(opal(file))
    end

    def load!(file = components)
      return true if loaded?
      self.load(file)
    ensure
      raise "No HyperReact components found in #{components}" unless loaded?
    end

    def loaded?
      !!v8_context.eval('Opal.React !== undefined')
    rescue ::ExecJS::Error
      false
    end

    private

    def components
      opts = ::Rails.configuration.react.server_renderer_options
      return opts[:files].first.gsub(/.js$/,'') if opts && opts[:files]
      'components'
    end

    def opal(file)
      Opal::Sprockets.load_asset(file)
    end
  end
end

Version data entries

15 entries across 15 versions & 2 rubygems

Version Path
hyper-component-0.99.6 lib/reactive-ruby/component_loader.rb
hyper-component-0.99.5 lib/reactive-ruby/component_loader.rb
hyper-component-0.99.4 lib/reactive-ruby/component_loader.rb
hyper-component-0.99.3 lib/reactive-ruby/component_loader.rb
hyper-component-0.99.2 lib/reactive-ruby/component_loader.rb
hyper-component-0.99.1 lib/reactive-ruby/component_loader.rb
hyper-component-0.99.0 lib/reactive-ruby/component_loader.rb
hyper-react-1.0.0.lap28 lib/reactive-ruby/component_loader.rb
hyper-react-1.0.0.lap27 lib/reactive-ruby/component_loader.rb
hyper-react-1.0.0.lap26 lib/reactive-ruby/component_loader.rb
hyper-react-1.0.0.lap25 lib/reactive-ruby/component_loader.rb
hyper-react-1.0.0.lap24 lib/reactive-ruby/component_loader.rb
hyper-react-1.0.0.lap23 lib/reactive-ruby/component_loader.rb
hyper-react-1.0.0.lap22 lib/reactive-ruby/component_loader.rb
hyper-react-1.0.0.lap21 lib/reactive-ruby/component_loader.rb