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
# 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

    # 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

  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



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

def current_user
  Isomorfeus.current_user
end

#execute_preload_blockObject

preloading



60
61
62
63
# File 'lib/isomorfeus_react/lucid_component/api.rb', line 60

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

#local_storeObject

stores



47
48
49
# File 'lib/isomorfeus_react/lucid_component/api.rb', line 47

def local_store
  LocalStore
end

#preloaded?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/isomorfeus_react/lucid_component/api.rb', line 65

def preloaded?
  !!state.preloaded
end

#session_storeObject



51
52
53
# File 'lib/isomorfeus_react/lucid_component/api.rb', line 51

def session_store
  SessionStore
end

#themeObject



55
56
57
# File 'lib/isomorfeus_react/lucid_component/api.rb', line 55

def theme
  props.theme
end