Skip to content

Conversation

simlay
Copy link
Contributor

@simlay simlay commented Mar 2, 2024

Comment on lines +134 to +144
if !children.isEmpty {
if let node = children[children.count - 1].next() {
children.append(node.children().makeIterator())
return node
} else {
children.removeLast()
return self.next()
}
} else {
return nil
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if !children.isEmpty {
if let node = children[children.count - 1].next() {
children.append(node.children().makeIterator())
return node
} else {
children.removeLast()
return self.next()
}
} else {
return nil
}
guard !children.isEmpty else { return nil }
if let node = children.last?.next() {
children.append(node.children().makeIterator())
return node
} else {
children.removeLast()
return self.next()
}

return try self.mergeFragmentJson(payload)
}

public func on(_ event: EventType, _ callback: @escaping (Document, NodeRef) -> ()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It'd be nice if we could have an AsyncStream for this API.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well I refactored this a few times to fix a deadlock. When mergeFragmentJson is called, it's locking a mutex for the Document on the rust side as it's mutating the document. Today, I pulled in latest main liveview-native/liveview-client-swiftui#1287 and found that recent changes to ObservedElement caused the deadlock. This is in part because when the closure is called on an incremental edit update, elementChanged which triggers the sink closure on the ObservedElement which uses Document.subscript and Document.children which require read locks on the same mutex that's currently locked for editing. I wonder if an AsyncStream for this API would help?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

elementChanged could be refactored to publish the element, instead of just Void. Then the child caching could be done on read instead of when the change occurs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants