feat: add JSONPath subscription #883
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
This PR adds a
subscribe_jsonpathmethod that allows subscribing to changes that may affect a JSONPath query result, without re-evaluating the query on every change. This enables efficient reactive patterns for applications that need to respond to specific data changes.Motivation
When building reactive UIs or data pipelines on top of Loro documents, you often want to watch specific paths (e.g.,
$.users[*].nameor$.config.theme). Without this feature, you'd need to:This is expensive for large documents. The new API provides a lightweight notification system that filters events at the path-matching level.
API
Rust:
JavaScript/TypeScript:
Design
The implementation uses an NFA-based (non-deterministic finite automaton) matcher optimized for minimal code size:
MatchSelectors (Name/Index/Wildcard) - no AST storage neededKey design choices:
Wildcardduring matching, keeping the matcher simple[*],[?...], etc.), any key change in that container triggers the callback - this ensures filter changes are never missedBehavior
$.a.b.c)$[*],[?...])..)