Skip to content

Commit b10144a

Browse files
Push item state for list items instead of pulling
Instead of creating watchers on a list directly, apply 1 layer of indirection to make sure the list of deps doesn't grow too big. The list gets updated by a single expression, which then updates a list of context values. The list child fragments will then pick up the changes automatically through a watch mechanism.
1 parent 3ec9f88 commit b10144a

1 file changed

Lines changed: 18 additions & 18 deletions

File tree

collagraph/fragment.py

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -423,29 +423,29 @@ def expression(self):
423423
value = self.expression()
424424
return value if hasattr(value, "__len__") else list(value)
425425

426+
# Keep a list with all the rendered values
427+
self.values = []
428+
426429
@weak(self)
427430
def update_children(self):
428-
for index in reversed(range(len(expression()), len(self.children))):
431+
items = expression()
432+
for index in reversed(range(len(items), len(self.children))):
433+
# Remove extra items and context
429434
fragment = self.children.pop(index)
430435
fragment.unmount()
436+
self.values.pop(index)
431437

432-
for i, item in enumerate(expression()):
433-
if i >= len(self.children):
434-
435-
def index_in_value(i=i):
436-
if i < len(expression()):
437-
return expression()[i]
438-
439-
# NOTE: I could try and figure out the lambda names
440-
# in the expression / self.expression
441-
442-
# TODO: create_fragment should contain a reactive object
443-
# probably with all the required props. Then within the
444-
# 'create_node' method, the binds can be picked from the
445-
# reactive object. And we'll need a watcher to update that
446-
# reactive object from the 'outside' (so here in this function)
447-
448-
fragment = self.create_fragment(index_in_value)
438+
for i, item in enumerate(items):
439+
if i < len(self.children):
440+
# Update the content for existing values
441+
self.values[i]["context"] = item
442+
else:
443+
# Create a new fragment + context
444+
context = reactive({"context": item})
445+
self.values.append(context)
446+
fragment = self.create_fragment(
447+
lambda i=i: self.values[i]["context"]
448+
)
449449
self.children.append(fragment)
450450
fragment.parent = self
451451
fragment.mount(target, anchor=self.anchor())

0 commit comments

Comments
 (0)