Completed
Push — master ( 8cb594...ba50eb )
by Yoh
01:01
created

Component.component_will_unmount()   A

Complexity

Conditions 1

Size

Total Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 2
rs 10
cc 1
1
require 'hyalite/short_hand'
2
3
module Hyalite
4
  module Component
5
    TAGS = %w(
6
      a abbr address area article aside audio b base bdi bdo blockquote body br button button button button canvas caption
7
      cite code col colgroup command datalist dd del details dfn div dl dt em embed fieldset figcaption figure footer form
8
      h1 h2 h3 h4 h5 h6 head header hgroup hr html i iframe img input ins kbd keygen label legend li link map mark menu meta
9
      meter nav noscript object ol optgroup option output p param pre progress q rp rt ruby s samp script section select small
10
      source span strong style sub summary sup table tbody td textarea tfoot th thead time title tr track u ul var video wbr
11
    )
12
13
    def pp(obj)
14
      puts obj.inspect
15
    end
16
17
    attr_accessor :props, :context, :refs
18
19
    def init_component(props, context, updator)
20
      @props = props
21
      @context = context
22
      @updator = updator
23
      @state = State.new(self, updator, initial_state)
24
      @refs = nil
25
    end
26
27
    def self.included(klass)
28
      klass.instance_eval do
29
        define_singleton_method(:state) do |key, initial_value|
30
          (@initial_state ||= {})[key] = initial_value
31
        end
32
33
        define_singleton_method(:initial_state) { @initial_state || {} }
34
35
        define_singleton_method(:before_mount) do |&block|
36
          if block
37
            @before_mount = block
38
          else
39
            @before_mount
40
          end
41
        end
42
43
        define_singleton_method(:after_mount) do |&block|
44
          if block
45
            @after_mount = block
46
          else
47
            @after_mount
48
          end
49
        end
50
51
        define_singleton_method(:before_update) do |&block|
52
          if block
53
            @before_update = block
54
          else
55
            @before_update
56
          end
57
        end
58
59
        define_singleton_method(:after_update) do |&block|
60
          if block
61
            @after_update = block
62
          else
63
            @after_update
64
          end
65
        end
66
      end
67
68
      TAGS.each do |tag|
69
        define_method(tag) do |props, *children, &block|
70
          children << ChildrenRenderer.new.instance_eval(&block) if block
71
          Hyalite.create_element(tag, props, *children)
72
        end
73
      end
74
75
      klass.extend ClassMethods
76
    end
77
78
    class ChildrenRenderer
79
      def initialize
80
        @children = []
81
        TAGS.each do |tag|
82
          define_singleton_method(tag) do |props, *children, &block|
83
            @children << Hyalite.create_element(tag, props, *children)
84
            @children
85
          end
86
        end
87
      end
88
    end
89
90
    module ClassMethods
91
      def el(props, *children)
92
        Hyalite.create_element(self, props, *children)
93
      end
94
    end
95
96
    def initial_state
97
      self.class.initial_state
98
    end
99
100
    def state
101
      @state.to_h
102
    end
103
104
    def state=(state)
105
      @state.set(state)
106
    end
107
108
    def child_context
109
      {}
110
    end
111
112
    def component_will_mount
113
      self.instance_eval(&self.class.before_mount) if self.class.before_mount
114
    end
115
116
    def component_did_mount
117
      self.instance_eval(&self.class.after_mount) if self.class.after_mount
118
    end
119
120
    def component_will_update(props, state, context)
121
      self.instance_eval(&self.class.before_update) if self.class.before_update
122
    end
123
124
    def component_did_update(props, state, context)
125
      self.instance_eval(&self.class.after_update) if self.class.after_update
126
    end
127
128
    def should_component_update(props, state, context)
129
      true
130
    end
131
132
    def force_update(&block)
133
      @updator.enqueue_force_update(self);
134
      if block_given?
135
        @updator.enqueue_callback(self, &block)
136
      end
137
    end
138
139
    def set_state(states, &block)
140
      @updator.enqueue_set_state(self, states)
141
      if block_given?
142
        @updator.enqueue_callback(self, &block)
143
      end
144
    end
145
146
    def render
147
    end
148
149
    class State
150
      def initialize(component, updator, initial_state)
151
        @component = component
152
        @updator = updator
153
        @state = initial_state.clone
154
        initial_state.each do |key, value|
155
          define_singleton_method(key) do
156
            @state[key]
157
          end
158
          define_singleton_method(key + '=') do |value|
159
            @updator.enqueue_set_state(@component, key => value)
160
          end
161
        end
162
      end
163
164
      def [](key)
165
        @state[key]
166
      end
167
168
      def set(state)
169
        @state = state.clone
170
      end
171
172
      def to_h
173
        @state
174
      end
175
    end
176
  end
177
178
  class EmptyComponent
179
    include Component
180
181
    def self.empty_element
182
      @instance ||= ElementObject.new(EmptyComponent, nil, nil, nil, nil)
183
    end
184
185
    def render
186
      Hyalite.create_element("noscript", nil, nil)
187
    end
188
  end
189
190
  class TopLevelWrapper
191
    include Component
192
193
    def render
194
      @props
195
    end
196
  end
197
end
198