Completed
Push — master ( c6a0b1...4e0284 )
by Yoh
01:16
created

Component.state()   A

Complexity

Conditions 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
require 'hyalite/short_hand'
2
3
module Hyalite
4
  module Component
5
6
    attr_accessor :props, :context, :refs
7
8
    def init_component(props, context, updator)
9
      @props = props
10
      @context = context
11
      @updator = updator
12
      @state = State.new(self, updator, initial_state)
13
      @refs = nil
14
    end
15
16
    def self.included(clazz)
17
      clazz.instance_eval do
18
        define_singleton_method(:state) do |key, initial_value|
19
          (@initial_state ||= {})[key] = initial_value
20
        end
21
22
        define_singleton_method(:initial_state) { @initial_state || {} }
23
      end
24
    end
25
26
    def initial_state
27
      self.class.initial_state
28
    end
29
30
    def state
31
      @state.to_h
32
    end
33
34
    def state=(state)
35
      @state.set(state)
36
    end
37
38
    def context_types
39
      {}
40
    end
41
42
    def child_context
43
      {}
44
    end
45
46
    def component_did_mount
47
    end
48
49
    def component_will_mount
50
    end
51
52
    def component_did_mount
0 ignored issues
show
Unused Code Bug introduced by
The method named Hyalite::Component#component_did_mount has been defined twice. The first time in file /home/scrutinizer/build/package/client/hyalite/component.rb at line 46, the second time in file /home/scrutinizer/build/package/client/hyalite/component.rb at line 52. Please rename either of them, if you need them both.
Loading history...
53
    end
54
55
    def component_will_unmount
56
    end
57
58
    def component_will_update(props, state, context)
59
    end
60
61
    def component_did_update(props, state, context)
62
    end
63
64
    def should_component_update(props, state, context)
65
      true
66
    end
67
68
    def force_update(&block)
69
      @updator.enqueue_force_update(self);
70
      if block_given?
71
        @updator.enqueue_callback(self, &block)
72
      end
73
    end
74
75
    def set_state(states, &block)
76
      @updator.enqueue_set_state(self, states)
77
      if block_given?
78
        @updator.enqueue_callback(self, &block)
79
      end
80
    end
81
82
    def render
83
    end
84
85
    class State
86
      def initialize(component, updator, initial_state)
87
        @component = component
88
        @updator = updator
89
        @state = initial_state.clone
90
        initial_state.each do |key, value|
91
          define_singleton_method(key) do
92
            @state[key]
93
          end
94
          define_singleton_method(key + '=') do |value|
95
            @updator.enqueue_set_state(@component, key => value)
96
          end
97
        end
98
      end
99
100
      def [](key)
101
        @state[key]
102
      end
103
104
      def set(state)
105
        @state = state.clone
106
      end
107
108
      def to_h
109
        @state
110
      end
111
    end
112
  end
113
114
  class EmptyComponent
115
    include Component
116
117
    def self.empty_element
118
      @instance ||= ElementObject.new(EmptyComponent, nil, nil, nil, nil)
119
    end
120
121
    def render
122
      Hyalite.create_element("noscript", nil, nil)
123
    end
124
  end
125
126
  class TopLevelWrapper
127
    include Component
128
129
    def render
130
      @props
131
    end
132
  end
133
end
134