@@ -2,113 +2,117 @@ module React
22 class RenderingContext
33 class << self
44 attr_accessor :waiting_on_resources
5- end
65
7- def self . build_only ( name , *args , &block )
8- React ::Component . deprecation_warning (
9- '..._as_node is deprecated. Render component and then use the .node method instead'
10- )
11- React ::RenderingContext . build { React ::RenderingContext . render ( name , *args , &block ) } . to_n
12- end
6+ def build_only ( name , *args , &block )
7+ React ::Component . deprecation_warning (
8+ '..._as_node is deprecated. Render component and then use the .node method instead'
9+ )
10+ React ::RenderingContext . build { React ::RenderingContext . render ( name , *args , &block ) } . to_n
11+ end
1312
14- def self . render ( name , *args , &block )
15- remove_nodes_from_args ( args )
16- @buffer ||= [ ] unless @buffer
17- if block
18- element = build do
19- saved_waiting_on_resources = waiting_on_resources
20- self . waiting_on_resources = nil
21- run_child_block ( name . nil? , &block )
22- if name
23- buffer = @buffer . dup
24- React . create_element ( name , *args ) { buffer } . tap do |element |
25- element . waiting_on_resources = saved_waiting_on_resources || !!buffer . detect { |e | e . waiting_on_resources if e . respond_to? ( :waiting_on_resources ) }
13+ def render ( name , *args , &block )
14+ remove_nodes_from_args ( args )
15+ @buffer ||= [ ] unless @buffer
16+ if block
17+ element = build do
18+ saved_waiting_on_resources = waiting_on_resources
19+ self . waiting_on_resources = nil
20+ run_child_block ( name . nil? , &block )
21+ if name
22+ buffer = @buffer . dup
23+ React . create_element ( name , *args ) { buffer } . tap do |element |
24+ element . waiting_on_resources = saved_waiting_on_resources || !!buffer . detect { |e | e . waiting_on_resources if e . respond_to? ( :waiting_on_resources ) }
25+ end
26+ elsif @buffer . last . is_a? React ::Element
27+ @buffer . last . tap { |element | element . waiting_on_resources ||= saved_waiting_on_resources }
28+ else
29+ @buffer . last . to_s . span . tap { |element | element . waiting_on_resources = saved_waiting_on_resources }
2630 end
27- elsif @buffer . last . is_a? React ::Element
28- @buffer . last . tap { |element | element . waiting_on_resources ||= saved_waiting_on_resources }
29- else
30- @buffer . last . to_s . span . tap { |element | element . waiting_on_resources = saved_waiting_on_resources }
3131 end
32+ elsif name . is_a? React ::Element
33+ element = name
34+ else
35+ element = React . create_element ( name , *args )
36+ element . waiting_on_resources = waiting_on_resources
3237 end
33- elsif name . is_a? React ::Element
34- element = name
35- else
36- element = React . create_element ( name , *args )
37- element . waiting_on_resources = waiting_on_resources
38+ @buffer << element
39+ self . waiting_on_resources = nil
40+ element
3841 end
39- @buffer << element
40- self . waiting_on_resources = nil
41- element
42- end
4342
44- def self . build
45- current = @buffer
46- @buffer = [ ]
47- return_val = yield @buffer
48- @buffer = current
49- return_val
50- end
43+ def build
44+ current = @buffer
45+ @buffer = [ ]
46+ return_val = yield @buffer
47+ @buffer = current
48+ return_val
49+ end
5150
52- def self . as_node ( element )
53- @buffer . delete ( element )
54- element
55- end
51+ def as_node ( element )
52+ @buffer . delete ( element )
53+ element
54+ end
5655
57- class << self ; alias delete as_node ; end
56+ alias delete as_node
5857
59- def self . replace ( e1 , e2 )
60- @buffer [ @buffer . index ( e1 ) ] = e2
61- end
58+ def rendered? ( element )
59+ @buffer . include? element
60+ end
6261
63- def self . remove_nodes_from_args ( args )
64- args [ 0 ] . each do |key , value |
65- value . as_node if value . is_a? ( Element ) rescue nil
66- end if args [ 0 ] && args [ 0 ] . is_a? ( Hash )
67- end
62+ def replace ( e1 , e2 )
63+ @buffer [ @buffer . index ( e1 ) ] = e2
64+ end
6865
69- # run_child_block gathers the element(s) generated by a child block.
70- # for example when rendering this div: div { "hello".span; "goodby".span }
71- # two child Elements will be generated.
72- #
73- # the final value of the block should either be
74- # 1 an object that responds to :acts_as_string?
75- # 2 a string,
76- # 3 an element that is NOT yet pushed on the rendering buffer
77- # 4 or the last element pushed on the buffer
78- #
79- # in case 1 we change the object to a string, and then it becomes case 2
80- # in case 2 we automatically push the string onto the buffer
81- # in case 3 we also push the Element onto the buffer IF the buffer is empty
82- # case 4 requires no special processing
83- #
84- # Once we have taken care of these special cases we do a check IF we are in an
85- # outer rendering scope. In this case react only allows us to generate 1 Element
86- # so we insure that is the case, and also check to make sure that element in the buffer
87- # is the element returned
88-
89- def self . run_child_block ( is_outer_scope )
90- result = yield
91- result = result . to_s if result . try :acts_as_string?
92- @buffer << result if result . is_a? ( String ) || ( result . is_a? ( Element ) && @buffer . empty? )
93- raise_render_error ( result ) if is_outer_scope && @buffer != [ result ]
94- end
66+ def remove_nodes_from_args ( args )
67+ args [ 0 ] . each do |key , value |
68+ value . as_node if value . is_a? ( Element ) rescue nil
69+ end if args [ 0 ] && args [ 0 ] . is_a? ( Hash )
70+ end
9571
96- # heurestically raise a meaningful error based on the situation
97-
98- def self . raise_render_error ( result )
99- improper_render 'A different element was returned than was generated within the DSL.' ,
100- 'Possibly improper use of Element#delete.' if @buffer . count == 1
101- improper_render "Instead #{ @buffer . count } elements were generated." ,
102- 'Do you want to wrap your elements in a div?' if @buffer . count > 1
103- improper_render "Instead the component #{ result } was returned." ,
104- "Did you mean #{ result } ()?" if result . try :reactrb_component?
105- improper_render "Instead the #{ result . class } #{ result } was returned." ,
106- 'You may need to convert this to a string.'
107- end
72+ # run_child_block gathers the element(s) generated by a child block.
73+ # for example when rendering this div: div { "hello".span; "goodby".span }
74+ # two child Elements will be generated.
75+ #
76+ # the final value of the block should either be
77+ # 1 an object that responds to :acts_as_string?
78+ # 2 a string,
79+ # 3 an element that is NOT yet pushed on the rendering buffer
80+ # 4 or the last element pushed on the buffer
81+ #
82+ # in case 1 we change the object to a string, and then it becomes case 2
83+ # in case 2 we automatically push the string onto the buffer
84+ # in case 3 we also push the Element onto the buffer IF the buffer is empty
85+ # case 4 requires no special processing
86+ #
87+ # Once we have taken care of these special cases we do a check IF we are in an
88+ # outer rendering scope. In this case react only allows us to generate 1 Element
89+ # so we insure that is the case, and also check to make sure that element in the buffer
90+ # is the element returned
91+
92+ def run_child_block ( is_outer_scope )
93+ result = yield
94+ result = result . to_s if result . try :acts_as_string?
95+ @buffer << result if result . is_a? ( String ) || ( result . is_a? ( Element ) && @buffer . empty? )
96+ raise_render_error ( result ) if is_outer_scope && @buffer != [ result ]
97+ end
98+
99+ # heurestically raise a meaningful error based on the situation
108100
109- def self . improper_render ( message , solution )
110- raise "a component's render method must generate and return exactly 1 element or a string.\n " \
111- " #{ message } #{ solution } "
101+ def raise_render_error ( result )
102+ improper_render 'A different element was returned than was generated within the DSL.' ,
103+ 'Possibly improper use of Element#delete.' if @buffer . count == 1
104+ improper_render "Instead #{ @buffer . count } elements were generated." ,
105+ 'Do you want to wrap your elements in a div?' if @buffer . count > 1
106+ improper_render "Instead the component #{ result } was returned." ,
107+ "Did you mean #{ result } ()?" if result . try :reactrb_component?
108+ improper_render "Instead the #{ result . class } #{ result } was returned." ,
109+ 'You may need to convert this to a string.'
110+ end
111+
112+ def improper_render ( message , solution )
113+ raise "a component's render method must generate and return exactly 1 element or a string.\n " \
114+ " #{ message } #{ solution } "
115+ end
112116 end
113117 end
114118
0 commit comments