-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic-fabric.py
More file actions
495 lines (393 loc) · 15.9 KB
/
basic-fabric.py
File metadata and controls
495 lines (393 loc) · 15.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
from time import sleep
import json
# Simplified version of get_node_from_instance
def get_node_from_instance(inst):
# For simplicity, we assume inst is already the node.
return inst
# Guarded Callback Handling
def invoke_guarded_callback_impl(name, func, context, *args):
try:
func(context, *args)
except Exception as error:
reporter.on_error(error)
class Reporter:
def __init__(self):
self.has_error = False
self.caught_error = None
def on_error(self, error):
self.has_error = True
self.caught_error = error
reporter = Reporter()
# Synthetic Event System
class SyntheticEvent:
def __init__(self, dispatch_config, target_inst, native_event, native_event_target):
self.dispatch_config = dispatch_config
self._target_inst = target_inst
self.native_event = native_event
self._dispatch_instances = self._dispatch_listeners = None
dispatch_config = self.__class__.Interface
for prop_name in dispatch_config:
if prop_name in dispatch_config:
self[prop_name] = dispatch_config[prop_name](native_event) if dispatch_config[prop_name] else native_event.get(prop_name, None)
self.target = native_event_target
def prevent_default(self):
self.is_default_prevented = True
def stop_propagation(self):
self.is_propagation_stopped = True
# Responder Event Handling
class ResponderSyntheticEvent(SyntheticEvent):
def __init__(self, dispatch_config, target_inst, native_event, native_event_target):
super().__init__(dispatch_config, target_inst, native_event, native_event_target)
self.touch_history = None
class ResponderEventPlugin:
@staticmethod
def extract_events(top_level_type, target_inst, native_event, native_event_target):
# Implement touch event extraction logic here, similar to JavaScript version
pass
# Node Creation and Updates
class Node:
def __init__(self, node_type, props=None, context=None):
self.node_type = node_type
self.props = props or {}
self.context = context
self.children = []
def add_child(self, child):
self.children.append(child)
def update_props(self, new_props):
self.props.update(new_props)
def to_dict(self):
"""Recursively converts the node and its children to a dictionary."""
return {
"node_type": self.node_type,
"props": self.props,
"children": [child.to_dict() for child in self.children]
}
def to_json(self):
"""Converts the node tree to a JSON string."""
return json.dumps(self.to_dict(), indent=4)
# Creating and cloning nodes
def create_node(context, node_type, root_container, props, internal_instance_handle):
return Node(node_type, props, context)
def clone_node_with_new_props(node, new_props):
node.update_props(new_props)
return node
# Guarded callback wrapper to catch and handle errors
def invoke_guarded_callback_and_catch_first_error(listener, event):
try:
listener(event) # Execute the listener with the event
except Exception as e:
print(f"Error occurred while executing callback: {e}")
# Additional error handling logic can go here if needed
# Event Dispatching
def execute_dispatch(event, listener, inst):
event.current_target = get_node_from_instance(inst)
invoke_guarded_callback_and_catch_first_error(listener, event)
event.current_target = None
def execute_direct_dispatch(event):
dispatch_listener = event._dispatch_listeners
event.current_target = dispatch_listener and get_node_from_instance(event._dispatch_instances) or None
if dispatch_listener:
dispatch_listener(event)
event.current_target = None
event._dispatch_listeners = None
event._dispatch_instances = None
return dispatch_listener
def schedule_callback(callback, priority=1):
sleep(priority) # Simulate delay based on priority
callback()
def cancel_callback(callback):
# Logic to cancel the scheduled callback (if needed)
pass
def should_yield():
# Placeholder to control when to yield to other tasks
return False
# Basic Reconciliation Logic
def diff_trees(old_node, new_node):
changes = []
# Type change
if old_node.node_type != new_node.node_type:
changes.append(("REPLACE", new_node))
return changes
# Prop change
for key in new_node.props:
if new_node.props.get(key) != old_node.props.get(key):
changes.append(("UPDATE_PROP", key, new_node.props[key]))
# Child changes
old_children = old_node.children
new_children = new_node.children
for i, child in enumerate(new_children):
if i < len(old_children):
changes.extend(diff_trees(old_children[i], child))
else:
changes.append(("ADD_CHILD", child))
return changes
# Node Update Handling
def apply_changes(node, changes):
for change in changes:
if change[0] == "REPLACE":
node = change[1] # Replace the node with the new one
elif change[0] == "UPDATE_PROP":
node.props[change[1]] = change[2] # Update the property
elif change[0] == "ADD_CHILD":
node.add_child(change[1]) # Add a new child
return node
# Simplified Scheduler and Task Management
class Task:
def __init__(self, callback, priority=1):
self.callback = callback
self.priority = priority
class Scheduler:
def __init__(self):
self.queue = []
def schedule(self, task):
self.queue.append(task)
self.queue.sort(key=lambda x: x.priority, reverse=True) # Sort by priority
def run(self):
while self.queue:
task = self.queue.pop(0)
task.callback()
# Example usage of Scheduler
scheduler = Scheduler()
scheduler.schedule(Task(lambda: print("Task 1"), priority=2))
scheduler.schedule(Task(lambda: print("Task 2"), priority=1))
scheduler.run()
# UI Rendering Logic
class Renderer:
def __init__(self):
self.root_node = None
def set_root(self, root_node):
self.root_node = root_node
def render(self):
if self.root_node:
self._render_node(self.root_node)
def _render_node(self, node):
print(f"Rendering {node.node_type} with props: {node.props}")
for child in node.children:
self._render_node(child)
# Example of setting up and rendering a simple UI
root = Node("View", {"style": "background-color: blue"})
child = Node("Text", {"value": "Hello, World!"})
root.add_child(child)
renderer = Renderer()
renderer.set_root(root)
renderer.render()
# Simplified Event Propagation
class Event:
def __init__(self, type, target):
self.type = type
self.target = target
self.current_target = target
self.is_propagation_stopped = False
self.is_default_prevented = False
def stop_propagation(self):
self.is_propagation_stopped = True
def prevent_default(self):
self.is_default_prevented = True
class EventDispatcher:
def __init__(self):
self.listeners = {}
def add_event_listener(self, target, event_type, listener):
if target not in self.listeners:
self.listeners[target] = {}
if event_type not in self.listeners[target]:
self.listeners[target][event_type] = []
self.listeners[target][event_type].append(listener)
def dispatch_event(self, event):
if event.target in self.listeners and event.type in self.listeners[event.target]:
for listener in self.listeners[event.target][event.type]:
listener(event)
if event.is_propagation_stopped:
break
# React-like Component System
class Component:
def __init__(self, props=None):
self.props = props or {}
self.state = {}
def set_state(self, new_state):
self.state.update(new_state)
self.render()
def render(self):
raise NotImplementedError("Subclasses must implement render()")
class View(Component):
def __init__(self, props=None):
super().__init__(props)
def render(self):
print(f"Rendering View with props: {self.props} and state: {self.state}")
class Text(Component):
def __init__(self, props=None):
super().__init__(props)
def render(self):
print(f"Rendering Text with props: {self.props} and state: {self.state}")
# Example of usage
view = View({"style": "background-color: red"})
text = Text({"value": "Hello, ReactPy!"})
view.set_state({"background_color": "blue"})
view.render()
text.render()
# Node Reconciliation and Diffing
def reconcile(old_node, new_node):
changes = []
# Check if node types are different
if old_node.node_type != new_node.node_type:
changes.append(("REPLACE", new_node))
return changes
# Check if node properties have changed
for key, value in new_node.props.items():
if old_node.props.get(key) != value:
changes.append(("UPDATE_PROP", key, value))
# Handle child nodes (diffing them)
old_children = old_node.children
new_children = new_node.children
for i, new_child in enumerate(new_children):
if i < len(old_children):
changes.extend(reconcile(old_children[i], new_child))
else:
changes.append(("ADD_CHILD", new_child))
# Remove any extra children
for i in range(len(new_children), len(old_children)):
changes.append(("REMOVE_CHILD", old_children[i]))
return changes
# Applying Changes to Nodes
def apply_reconciliation_changes(node, changes):
for change in changes:
if change[0] == "REPLACE":
node = change[1] # Replace the node with a new one
elif change[0] == "UPDATE_PROP":
node.props[change[1]] = change[2] # Update the property
elif change[0] == "ADD_CHILD":
node.add_child(change[1]) # Add a new child
elif change[0] == "REMOVE_CHILD":
node.children.remove(change[1]) # Remove the child
return node
# Component Lifecycle
class Component:
def __init__(self, props=None):
self.props = props or {}
self.state = {}
self._is_mounted = False
def set_state(self, new_state):
self.state.update(new_state)
self.render()
def render(self):
raise NotImplementedError("Subclasses must implement render()")
def component_did_mount(self):
print(f"{self.__class__.__name__} mounted.")
def component_will_unmount(self):
print(f"{self.__class__.__name__} will unmount.")
def mount(self):
if not self._is_mounted:
self._is_mounted = True
self.component_did_mount()
def unmount(self):
if self._is_mounted:
self._is_mounted = False
self.component_will_unmount()
# Example of a Component with Lifecycle Methods
class MyComponent(Component):
def __init__(self, props=None):
super().__init__(props)
self.state = {"count": 0}
def render(self):
print(f"Rendering MyComponent with props: {self.props} and state: {self.state}")
def increment(self):
self.set_state({"count": self.state["count"] + 1})
print(f"Count incremented: {self.state['count']}")
# Creating and mounting the component
my_component = MyComponent(props={"name": "Test Component"})
my_component.mount() # Trigger the componentDidMount lifecycle method
my_component.render() # Initial render
# Updating the state and triggering the lifecycle method
my_component.increment()
my_component.render()
# Unmounting the component
my_component.unmount() # Trigger componentWillUnmount lifecycle method
# Handling Node Update in a UI Tree
class UIUpdater:
def __init__(self, root_node):
self.root_node = root_node
def update_node(self, old_node, new_node):
changes = reconcile(old_node, new_node) # Get differences between old and new node
return apply_reconciliation_changes(old_node, changes) # Apply changes to the node
def update_ui(self, new_tree):
self.root_node = self.update_node(self.root_node, new_tree)
print("UI updated to reflect changes.")
# Event Listener Management
class EventListenerManager:
def __init__(self):
self.listeners = {}
def add_listener(self, target, event_type, listener):
if target not in self.listeners:
self.listeners[target] = {}
if event_type not in self.listeners[target]:
self.listeners[target][event_type] = []
self.listeners[target][event_type].append(listener)
def remove_listener(self, target, event_type, listener):
if target in self.listeners and event_type in self.listeners[target]:
self.listeners[target][event_type].remove(listener)
if not self.listeners[target][event_type]:
del self.listeners[target][event_type]
if not self.listeners[target]:
del self.listeners[target]
def dispatch_event(self, event):
if event.target in self.listeners and event.type in self.listeners[event.target]:
for listener in self.listeners[event.target][event.type]:
listener(event)
if event.is_propagation_stopped:
break
# Handling Task Scheduling and Execution
class TaskScheduler:
def __init__(self):
self.task_queue = []
def schedule_task(self, task, priority=1):
self.task_queue.append({"task": task, "priority": priority})
self.task_queue.sort(key=lambda x: x["priority"], reverse=True) # Sort by priority
def execute_tasks(self):
while self.task_queue:
task = self.task_queue.pop(0)["task"]
task() # Execute the task
# Example usage of TaskScheduler
scheduler = TaskScheduler()
# Schedule tasks with different priorities
scheduler.schedule_task(lambda: print("High priority task"), priority=2)
scheduler.schedule_task(lambda: print("Low priority task"), priority=1)
# Execute the tasks
scheduler.execute_tasks()
# Complete Example and Final Integration with Child Node Update
class MyApp:
def __init__(self):
self.scheduler = TaskScheduler()
self.event_manager = EventListenerManager()
self.ui_updater = UIUpdater(None) # Initial empty root node
self.root = Node("View", {"style": "background-color: white"})
self.ui_updater.root_node = self.root # Set the root node for UI updates
# Create child nodes and add them to the root
self.child1 = Node("Text", {"value": "Child 1"})
self.child2 = Node("Text", {"value": "Child 2"})
self.root.add_child(self.child1)
self.root.add_child(self.child2)
def update_child_node(self, child, new_props):
# Update the child node's props and trigger a re-render
print(f"Updating {child.node_type} with new props: {new_props}")
child.update_props(new_props)
self.ui_updater.update_ui(self.root) # Reconcile the root to apply changes
def start(self):
# Schedule and run tasks
self.scheduler.schedule_task(lambda: print("App has started"))
self.scheduler.execute_tasks()
# Initial UI render
print("Initial UI Render:")
self.ui_updater.update_ui(self.root)
print(f"Root node style: {self.root.props['style']}")
# Simulate updating the child nodes
print("\nUpdating child nodes:")
self.update_child_node(self.child1, {"value": "Updated Child 1"})
self.update_child_node(self.child2, {"value": "Updated Child 2"})
# Simulate an event dispatch (just an example)
event = Event(type="click", target=self.root)
self.event_manager.add_listener(self.root, "click", lambda e: print(f"Event {e.type} triggered on {e.target.node_type}"))
self.event_manager.dispatch_event(event)
print("Current UI Tree in JSON:")
print(self.root.to_json())
# Running the application
app = MyApp()
app.start()