Skip to content

Commit 67fda53

Browse files
committed
Not quite working
1 parent fbad9e5 commit 67fda53

File tree

4 files changed

+75
-102
lines changed

4 files changed

+75
-102
lines changed

main.rb

Lines changed: 4 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -94,60 +94,13 @@
9494
font_defs_json = JSON.pretty_generate(defs: font_size_pairs)
9595
theme_json = JSON.pretty_generate(theme2)
9696

97-
class Node
98-
attr_accessor :id, :root
99-
100-
def initialize(id, root)
101-
@type = 'node'
102-
@id = id
103-
@root = root
104-
end
105-
106-
def to_json(*options)
107-
{
108-
type: @type,
109-
id: @id,
110-
root: @root
111-
}.to_json(*options)
112-
end
113-
end
114-
115-
class UnformattedText
116-
attr_accessor :id, :text
117-
118-
def initialize(id, text)
119-
@type = 'unformatted-text'
120-
@id = id
121-
@text = text
122-
end
123-
124-
def to_json(*options)
125-
{
126-
type: @type,
127-
id: @id,
128-
text: @text
129-
}.to_json(*options)
130-
end
131-
end
132-
13397
service = WidgetRegistrationService.new
13498
shadow_node_traversal_helper = ShadowNodeTraversalHelper.new(service)
13599

136100
on_init = FFI::Function.new(:void, []) do
137-
# Create an Async task using concurrent-ruby's Async
138-
# Concurrent::Async.perform do
139-
# Thread.main do
140-
puts "OnInit called!"
101+
root = Root.new()
141102

142-
root = Root.new()
143-
144-
puts "After root creation"
145-
146-
shadow_node_traversal_helper.traverse_tree(root)
147-
148-
puts "After tree"
149-
# end
150-
# end
103+
shadow_node_traversal_helper.traverse_tree(root)
151104
end
152105

153106
on_text_changed = FFI::Function.new(:void, [:int, :string]) do |id, text|
@@ -172,6 +125,8 @@ def to_json(*options)
172125
end
173126

174127
on_click = FFI::Function.new(:void, [:int]) do |id|
128+
service.dispatch_on_click_event(id)
129+
175130
puts "Button clicked: ID=#{id}"
176131
end
177132

sampleapp.rb

Lines changed: 28 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,17 @@ def initialize(todo_text, todo_items)
2525
$sample_app_state = Rx::BehaviorSubject.new(AppState.new("", []))
2626

2727
def 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
3839
end
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

services.rb

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,25 @@
33
require 'thread'
44
require_relative 'xframes'
55

6+
# $events_subject = Rx::ReplaySubject.new()
7+
68
class WidgetRegistrationService
79
def initialize
810
@id_generator_lock = Mutex.new
911
@id_widget_registration_lock = Mutex.new
1012
@id_event_registration_lock = Mutex.new
1113

12-
@events_subject = Rx::ReplaySubject.new(10)
13-
# @events_subject.debounce(0.001).subscribe { |fn| fn.call }
14-
14+
15+
# @events_subject = Rx::ReplaySubject.new()
16+
# @events_subject.debounce(0.001).subscribe(Proc.new { |fn| fn.call })
17+
# promise = Concurrent::Promise.execute do
18+
# $events_subject.subscribe do |cb|
19+
# puts "yo"
20+
# cb()
21+
# end
22+
# end
23+
# promise.wait
24+
1525
@widget_registry = {}
1626
@on_click_registry = Rx::BehaviorSubject.new({})
1727

@@ -56,12 +66,22 @@ def register_on_click(widget_id, on_click)
5666
end
5767

5868
def dispatch_on_click_event(widget_id)
59-
on_click = @on_click_registry.value[widget_id]
60-
if on_click
61-
@events_subject.on_next(on_click)
62-
else
63-
puts "Widget with id #{widget_id} has no on_click handler"
69+
promise = Concurrent::Promise.execute do
70+
on_click = @on_click_registry.value[widget_id]
71+
72+
if on_click
73+
# promise = Concurrent::Promise.execute do
74+
on_click()
75+
# $events_subject.on_next(on_click)
76+
# end
77+
78+
79+
else
80+
puts "Widget with id #{widget_id} has no on_click handler"
81+
end
6482
end
83+
84+
promise.wait
6585
end
6686

6787
def create_widget(widget)

treetraversal.rb

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -53,18 +53,22 @@ def subscribe_to_props_helper(shadow_node)
5353
shadow_node.props_change_subscription.dispose
5454
end
5555

56-
return nil
57-
58-
if shadow_node.renderable.is_a?(BaseComponent)
59-
component = shadow_node.renderable
60-
shadow_node.props_change_subscription = component.props.pipe(
61-
Rx::Operators.skip(1)
62-
).subscribe { |new_props| handle_component_props_change(shadow_node, component, new_props) }
63-
elsif shadow_node.renderable.is_a?(WidgetNode)
64-
shadow_node.props_change_subscription = shadow_node.renderable.props.pipe(
65-
Rx::Operators.skip(1)
66-
).subscribe { |new_props| handle_widget_node_props_change(shadow_node, shadow_node.renderable, new_props) }
56+
promise = Concurrent::Promise.execute do
57+
58+
if shadow_node.renderable.is_a?(BaseComponent)
59+
component = shadow_node.renderable
60+
shadow_node.props_change_subscription = component.props.pipe(
61+
Rx::Operators.skip(1)
62+
).subscribe { |new_props| handle_component_props_change(shadow_node, component, new_props) }
63+
elsif shadow_node.renderable.is_a?(WidgetNode)
64+
shadow_node.props_change_subscription = shadow_node.renderable.props.pipe(
65+
Rx::Operators.skip(1)
66+
).subscribe { |new_props| handle_widget_node_props_change(shadow_node, shadow_node.renderable, new_props) }
67+
end
68+
6769
end
70+
71+
promise.wait
6872
end
6973

7074
def handle_widget_node(widget)

0 commit comments

Comments
 (0)