Module: LucidComponent::Api

Defined in:
lib/isomorfeus_react/lucid_component/api.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
48
49
50
51
52
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
# File 'lib/isomorfeus_react/lucid_component/api.rb', line 3

def self.included(base)
  base.instance_exec do
    # store
    attr_accessor :app_store
    attr_accessor :class_store
    attr_accessor :store

    def class_store
      @class_store ||= ::LucidComponent::ClassStoreProxy.new(self.to_s)
    end

    def store_updates(switch)
      case switch
      when :on then `base.store_updates = true`
      when :off then `base.store_updates = false`
      end
    end

    # styles
    def styles(styles_hash = nil, &block)
      if block_given?
        %x{
          base.jss_styles = function(theme) {
            let wrapped_theme = Opal.React.Component.Styles.$new(theme);
            var result = block.$call(wrapped_theme);
            return result.$to_n();
          }
        }
        nil
      elsif styles_hash
        `base.jss_styles = #{styles_hash.to_n}` if styles_hash
        styles_hash
      elsif `typeof base.jss_styles === 'object'`
        `Opal.Hash.$new(base.jss_styles)`
      else
        nil
      end
    end
    alias_method :styles=, :styles

    # preloading
    def preload(&block)
      `base.preload_block = block`
      component_did_mount do
        unless self.state.preloaded
          @_preload_promise.then { self.state.preloaded = true }.fail do |result|
            err_text = "#{self.class.name}: preloading failed, last result: #{result.nil? ? 'nil' : result}!"
            `console.error(err_text)`
          end
        end
      end
    end

    def while_loading(option = nil, &block)
      wl_block = proc do
        if @_preload_promise.resolved?
          instance_exec(&`base.render_block`)
        else
          instance_exec(&block)
        end
      end
      `base.while_loading_block = wl_block`
    end
  end

  # stores
  def local_store
    LocalStore
  end

  def session_store
    SessionStore
  end

  # styles
  def styles
    props.classes
  end

  def theme
    props.theme
  end

  # preloading
  def execute_preload_block
    @_preload_promise = instance_exec(&self.class.JS[:preload_block])
    @_preload_promise.resolved?
  end

  def preloaded?
    !!state.preloaded
  end

  # requires transport
  def current_user
    Isomorfeus.current_user
  end
end

Instance Method Details

#current_userObject

requires transport



97
98
99
# File 'lib/isomorfeus_react/lucid_component/api.rb', line 97

def current_user
  Isomorfeus.current_user
end

#execute_preload_blockObject

preloading



87
88
89
90
# File 'lib/isomorfeus_react/lucid_component/api.rb', line 87

def execute_preload_block
  @_preload_promise = instance_exec(&self.class.JS[:preload_block])
  @_preload_promise.resolved?
end

#local_storeObject

stores



69
70
71
# File 'lib/isomorfeus_react/lucid_component/api.rb', line 69

def local_store
  LocalStore
end

#preloaded?Boolean

Returns:

  • (Boolean)


92
93
94
# File 'lib/isomorfeus_react/lucid_component/api.rb', line 92

def preloaded?
  !!state.preloaded
end

#session_storeObject



73
74
75
# File 'lib/isomorfeus_react/lucid_component/api.rb', line 73

def session_store
  SessionStore
end

#stylesObject

styles



78
79
80
# File 'lib/isomorfeus_react/lucid_component/api.rb', line 78

def styles
  props.classes
end

#themeObject



82
83
84
# File 'lib/isomorfeus_react/lucid_component/api.rb', line 82

def theme
  props.theme
end