@@ -25,16 +25,17 @@ def initialize(todo_text, todo_items)
2525$sample_app_state = Rx ::BehaviorSubject . new ( AppState . new ( "" , [ ] ) )
2626
2727def on_click
28- new_todo_item = TodoItem . new ( "New Todo" , false )
29-
30- current_state = $sample_app_state. value
31-
32- new_state = AppState . new (
33- current_state . todo_text ,
34- current_state . todo_items + [ new_todo_item ]
35- )
28+ promise = Concurrent ::Promise . execute do
29+ new_todo_item = TodoItem . new ( "New Todo" , false )
30+ current_state = $sample_app_state. value
31+ new_state = AppState . new (
32+ current_state . todo_text ,
33+ current_state . todo_items + [ new_todo_item ]
34+ )
35+ $sample_app_state. on_next ( new_state )
36+ end
3637
37- $sample_app_state . on_next ( new_state )
38+ promise . wait
3839end
3940
4041$text_style = WidgetStyle . new (
@@ -62,45 +63,38 @@ class App < BaseComponent
6263 def initialize
6364 super ( { } )
6465
65- @sample_app_state = Rx ::BehaviorSubject . new ( AppState . new ( "" , [ ] ) )
66-
67- @props . on_next ( {
68- "todo_text" => "" ,
69- "todo_items" => [ ]
70- } )
71-
72- puts "App initialize called 3"
73-
7466 promise = Concurrent ::Promise . execute do
75- @sample_app_state . subscribe do |latest_app_state |
76- # Safe operation, as Async ensures execution is handled in the appropriate thread
67+ $sample_app_state. subscribe do |latest_app_state |
68+ puts "app state changed"
69+
7770 @props . on_next ( {
7871 "todo_text" => latest_app_state . todo_text ,
7972 "todo_items" => latest_app_state . todo_items
8073 } )
8174 end
8275 end
8376
84- # Wait for the promise to complete before moving forward
8577 promise . wait
8678
87- puts "App initialize called 4"
79+ @props . on_next ( {
80+ "todo_text" => "" ,
81+ "todo_items" => [ TodoItem . new ( "New Todo" , false ) ]
82+ } )
8883 end
8984
9085 def render
91- puts "App render called"
92- children = [ button ( "Add todo" , Proc . new { puts "suga" } , $button_style) ]
93- # children = [button("Add todo")]
94-
95- puts "App render called 2"
96-
97- # props.value["todo_items"].each do |todo_item|
98- # text = "#{todo_item.text} (#{todo_item.done ? 'done' : 'to do'})."
99- # children << unformatted_text(text, $text_style)
100- # children << unformatted_text(text)
101- # end
86+ children = [ button ( "Add todo" , Proc . new {
87+ on_click ( )
88+ } , $button_style) ]
10289
103- puts "App render called 3"
90+ promise = Concurrent ::Promise . execute do
91+ @props . value [ "todo_items" ] . each do |todo_item |
92+ text = "#{ todo_item . text } (#{ todo_item . done ? 'done' : 'to do' } )."
93+ children << unformatted_text ( text , $text_style)
94+ end
95+ end
96+
97+ promise . wait
10498
10599 node ( children )
106100 end
0 commit comments