-
Notifications
You must be signed in to change notification settings - Fork 149
feat: introduce a2ui_core library #857
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 21 commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
1ce0763
Add a2ui_core runtime: models, processor, expressions, binder
andrewkolos 430532d
Fix DataPath.hashCode collision for escaped segments
andrewkolos 623f983
Apply dart fix and resolve lint errors and warnings
andrewkolos 3132bdb
Add ValueNotifier.forceNotify() to eliminate protected member warnings
andrewkolos a5d56ee
Fix ComponentModel.onUpdated not firing after the first property update
andrewkolos 015dcf9
Fix ComputedNotifier leak in GenericBinder on rebuild
andrewkolos 0586dc3
Remove redundant double-initialization in GenericBinder and ComputedN…
andrewkolos f14650b
Add A2uiMessage.fromJson for deserializing JSON envelopes
andrewkolos 82cf101
Clear CancellationSignal listeners after cancel to release references
andrewkolos a7348b3
Remove action forwarder listener before disposing deleted surfaces
andrewkolos 02ad76b
Perf: replace per-character RegExp allocations with codeUnitAt checks
andrewkolos ad41554
Add missing copyright headers to a2ui_core source and test files
andrewkolos a1d80cd
Remove unused uuid dependency from a2ui_core
andrewkolos 4119ac7
Replace unnecessary dynamic with Object? across a2ui_core
andrewkolos 67b4c5e
Apply dart fix, dart format, and fix line length lint issues
andrewkolos aea5196
Fix FormatStringFunction.returnType from 'any' to 'string'
andrewkolos 5342a4e
Merge protocol/ and state/ into core/ to match renderer guide structure
andrewkolos a1b1617
Fix CancellationSignal concurrent modification during cancel
andrewkolos 06c7add
Fix schema corruption from shallow copy in toJsonMap
andrewkolos 0fd7e6e
Remove unnecessary type annotation in cancellation.dart
andrewkolos 8b1c8df
Run dart format on processor and test files
andrewkolos ef493b7
Decouple DataContext from SurfaceModel
andrewkolos 553f468
Add EventNotifier for discrete events
andrewkolos b337854
Add reactivity test coverage for nested batch, forceNotify, and dispose
andrewkolos dcba1a7
Rename Catalog.invoker to Catalog.invoke
andrewkolos b66e0c5
Split SurfaceGroupModel into its own file
andrewkolos a9ca134
Improve doc comments on GenericBinder, DataContext, and resolve methods
andrewkolos 844c17b
Rename common/ to primitives/
andrewkolos 7345376
Replace returnType string with A2uiReturnType enum
andrewkolos 7d8b053
Try reworking GenericBinder doc comment again
andrewkolos 6022f17
Run dart format
andrewkolos df958ba
Replace custom ValueNotifier/ComputedNotifier with preact_signals
andrewkolos File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| // Copyright 2025 The Flutter Authors. | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| /// A signal that can be used to cancel an operation. | ||
| class CancellationSignal { | ||
| bool _isCancelled = false; | ||
|
|
||
| final _listeners = <void Function()>[]; | ||
|
|
||
| /// Whether the operation has been cancelled. | ||
| bool get isCancelled => _isCancelled; | ||
|
|
||
| /// Cancels the operation. | ||
| void cancel() { | ||
| if (_isCancelled) return; | ||
| _isCancelled = true; | ||
| final List<void Function()> listeners = List.of(_listeners); | ||
| _listeners.clear(); | ||
| for (final listener in listeners) { | ||
| listener(); | ||
| } | ||
| } | ||
|
|
||
| /// Adds a listener to be notified when the operation is cancelled. | ||
| void addListener(void Function() listener) { | ||
| if (_isCancelled) { | ||
| listener(); | ||
| } else { | ||
| _listeners.add(listener); | ||
| } | ||
| } | ||
|
|
||
| /// Removes a listener. | ||
| void removeListener(void Function() listener) { | ||
| _listeners.remove(listener); | ||
| } | ||
|
|
||
| /// Throws a [CancellationException] if the operation has been cancelled. | ||
| void throwIfCancelled() { | ||
| if (_isCancelled) { | ||
| throw const CancellationException(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// An exception thrown when an operation is cancelled. | ||
| class CancellationException implements Exception { | ||
| /// Creates a [CancellationException]. | ||
| const CancellationException([this.message]); | ||
|
|
||
| /// A message describing the cancellation. | ||
| final String? message; | ||
|
|
||
| @override | ||
| String toString() => message == null | ||
| ? 'CancellationException' | ||
| : 'CancellationException: $message'; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| // Copyright 2025 The Flutter Authors. | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| import 'package:collection/collection.dart'; | ||
|
|
||
| /// A class for handling JSON Pointer (RFC 6901) paths. | ||
| class DataPath { | ||
| final List<String> segments; | ||
|
|
||
| DataPath(this.segments); | ||
|
|
||
| /// Parses a JSON Pointer string into a [DataPath]. | ||
| factory DataPath.parse(String path) { | ||
| if (path.isEmpty || path == '/') { | ||
| return DataPath([]); | ||
| } | ||
|
|
||
| var normalized = path; | ||
| if (path.startsWith('/')) { | ||
| normalized = path.substring(1); | ||
| } | ||
|
|
||
| if (normalized.endsWith('/')) { | ||
| normalized = normalized.substring(0, normalized.length - 1); | ||
| } | ||
|
|
||
| if (normalized.isEmpty) { | ||
| return DataPath([]); | ||
| } | ||
|
|
||
| final List<String> segments = normalized.split('/').map((s) { | ||
| return s.replaceAll('~1', '/').replaceAll('~0', '~'); | ||
| }).toList(); | ||
|
|
||
| return DataPath(segments); | ||
| } | ||
|
|
||
| /// The number of segments in the path. | ||
| int get length => segments.length; | ||
|
|
||
| /// Whether the path is empty (points to the root). | ||
| bool get isEmpty => segments.isEmpty; | ||
|
|
||
| /// Whether the path starts with a slash (is absolute). | ||
| bool get isAbsolute => | ||
| true; // All parsed paths are treated as absolute for now in our context | ||
|
|
||
| /// Joins this path with another path or segment. | ||
| DataPath append(Object? other) { | ||
| if (other is DataPath) { | ||
| return DataPath([...segments, ...other.segments]); | ||
| } else if (other is String) { | ||
| if (other.startsWith('/')) { | ||
| return DataPath.parse(other); | ||
| } | ||
| return DataPath([...segments, ...DataPath.parse(other).segments]); | ||
| } | ||
| return DataPath([...segments, other.toString()]); | ||
| } | ||
|
|
||
| /// Returns the parent path. | ||
| DataPath? get parent { | ||
| if (segments.isEmpty) return null; | ||
| return DataPath(segments.sublist(0, segments.length - 1)); | ||
| } | ||
|
|
||
| @override | ||
| String toString() { | ||
| if (segments.isEmpty) return '/'; | ||
| return '/${segments.map((s) => s.replaceAll('~', '~0').replaceAll('/', '~1')).join('/')}'; | ||
| } | ||
|
|
||
| @override | ||
| bool operator ==(Object other) => | ||
| identical(this, other) || | ||
| other is DataPath && | ||
| const ListEquality<String>().equals(segments, other.segments); | ||
|
|
||
| @override | ||
| int get hashCode => const ListEquality<String>().hash(segments); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| // Copyright 2025 The Flutter Authors. | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| /// Base class for all A2UI specific errors. | ||
| class A2uiError implements Exception { | ||
| final String message; | ||
| final String code; | ||
|
|
||
| A2uiError(this.message, [this.code = 'UNKNOWN_ERROR']); | ||
|
|
||
| @override | ||
| String toString() => '$runtimeType [$code]: $message'; | ||
| } | ||
|
|
||
| /// Thrown when JSON validation fails or schemas are mismatched. | ||
| class A2uiValidationError extends A2uiError { | ||
| final Object? details; | ||
|
|
||
| A2uiValidationError(String message, {this.details}) | ||
| : super(message, 'VALIDATION_ERROR'); | ||
| } | ||
|
|
||
| /// Thrown during DataModel mutations (invalid paths, type mismatches). | ||
| class A2uiDataError extends A2uiError { | ||
| final String? path; | ||
|
|
||
| A2uiDataError(String message, {this.path}) : super(message, 'DATA_ERROR'); | ||
| } | ||
|
|
||
| /// Thrown during string interpolation and function evaluation. | ||
| class A2uiExpressionError extends A2uiError { | ||
| final String? expression; | ||
| final Object? details; | ||
|
|
||
| A2uiExpressionError(String message, {this.expression, this.details}) | ||
| : super(message, 'EXPRESSION_ERROR'); | ||
| } | ||
|
|
||
| /// Thrown for structural issues in the UI tree (missing surfaces, duplicate | ||
| /// components). | ||
| class A2uiStateError extends A2uiError { | ||
| A2uiStateError(String message) : super(message, 'STATE_ERROR'); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,164 @@ | ||
| // Copyright 2025 The Flutter Authors. | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| import '../listenable/notifiers.dart' as notifiers; | ||
|
|
||
| /// Alias for [notifiers.GenUiListenable]. | ||
| typedef Listenable = notifiers.GenUiListenable; | ||
|
|
||
| /// Alias for [notifiers.GenUiValueListenable]. | ||
| typedef ValueListenable<T> = notifiers.GenUiValueListenable<T>; | ||
|
|
||
| bool _inBatch = false; | ||
| final _pendingNotifiers = <ValueNotifier<Object?>>{}; | ||
|
|
||
| /// Executes [callback] and defers notifications until it completes. | ||
| void batch(void Function() callback) { | ||
| if (_inBatch) { | ||
| callback(); | ||
| return; | ||
| } | ||
|
|
||
| _inBatch = true; | ||
| try { | ||
| callback(); | ||
| } finally { | ||
| _inBatch = false; | ||
| final List<ValueNotifier<Object?>> toNotify = _pendingNotifiers.toList(); | ||
| _pendingNotifiers.clear(); | ||
| for (final notifier in toNotify) { | ||
| notifier.forceNotify(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// A value holder that notifies listeners when the value changes. | ||
| /// | ||
| /// Extends [notifiers.ChangeNotifier] for robust listener management and adds | ||
| /// batch-aware notification and automatic dependency tracking for use with | ||
| /// [ComputedNotifier]. | ||
| class ValueNotifier<T> extends notifiers.ChangeNotifier | ||
| implements notifiers.GenUiValueListenable<T> { | ||
| T _value; | ||
|
|
||
| ValueNotifier(this._value); | ||
|
|
||
| @override | ||
| T get value { | ||
| _DependencyTracker.instance?._reportRead(this); | ||
| return _value; | ||
| } | ||
|
|
||
| set value(T newValue) { | ||
| if (_value == newValue) return; | ||
| _value = newValue; | ||
| if (_inBatch) { | ||
| _pendingNotifiers.add(this); | ||
| return; | ||
| } | ||
| notifyListeners(); | ||
| } | ||
|
|
||
| /// Notifies listeners unconditionally, even if the value hasn't changed. | ||
| /// | ||
| /// Use this when the held value is a mutable container (e.g. a [Map] or | ||
| /// [List]) whose contents changed in place without changing the reference. | ||
| void forceNotify() { | ||
| if (_inBatch) { | ||
| _pendingNotifiers.add(this); | ||
| return; | ||
| } | ||
| notifyListeners(); | ||
| } | ||
| } | ||
|
|
||
| /// A derived notifier that automatically tracks and listens to other | ||
| /// [ValueListenable] dependencies, recalculating its value only when they | ||
| /// change. | ||
| class ComputedNotifier<T> extends ValueNotifier<T> { | ||
| final T Function() _compute; | ||
| final Set<notifiers.GenUiValueListenable<Object?>> _dependencies = {}; | ||
|
|
||
| ComputedNotifier(this._compute) : super(_initialValue(_compute)) { | ||
| _subscribePendingDeps(); | ||
| } | ||
|
|
||
| // Stack-based pending deps to handle reentrant ComputedNotifier creation | ||
| // (e.g. when _compute itself creates a nested ComputedNotifier). | ||
| static final List<Set<notifiers.GenUiValueListenable<Object?>>> | ||
| _pendingDepsStack = []; | ||
|
|
||
| static T _initialValue<T>(T Function() compute) { | ||
| final tracker = _DependencyTracker(); | ||
| final T value = tracker.track(compute); | ||
| _pendingDepsStack.add(tracker.dependencies); | ||
| return value; | ||
| } | ||
|
|
||
| void _subscribePendingDeps() { | ||
| final Set<notifiers.GenUiValueListenable<Object?>> deps = _pendingDepsStack | ||
| .removeLast(); | ||
| for (final dep in deps) { | ||
| dep.addListener(_onDependencyChanged); | ||
| } | ||
| _dependencies.addAll(deps); | ||
| } | ||
|
|
||
| void _updateDependencies() { | ||
| final tracker = _DependencyTracker(); | ||
| final T newValue = tracker.track(_compute); | ||
|
|
||
| final Set<notifiers.GenUiValueListenable<Object?>> newDeps = | ||
| tracker.dependencies; | ||
|
|
||
| // Unsubscribe from old dependencies no longer needed. | ||
| for (final notifiers.GenUiValueListenable<Object?> dep | ||
| in _dependencies.difference(newDeps)) { | ||
| dep.removeListener(_onDependencyChanged); | ||
| } | ||
|
|
||
| // Subscribe to new dependencies. | ||
| for (final notifiers.GenUiValueListenable<Object?> dep | ||
| in newDeps.difference(_dependencies)) { | ||
| dep.addListener(_onDependencyChanged); | ||
| } | ||
|
|
||
| _dependencies.clear(); | ||
| _dependencies.addAll(newDeps); | ||
|
|
||
| super.value = newValue; | ||
| } | ||
|
|
||
| void _onDependencyChanged() { | ||
| _updateDependencies(); | ||
| } | ||
|
|
||
| @override | ||
| void dispose() { | ||
| for (final notifiers.GenUiValueListenable<Object?> dep in _dependencies) { | ||
| dep.removeListener(_onDependencyChanged); | ||
| } | ||
| _dependencies.clear(); | ||
| super.dispose(); | ||
| } | ||
| } | ||
|
|
||
| class _DependencyTracker { | ||
| static _DependencyTracker? instance; | ||
| final Set<notifiers.GenUiValueListenable<Object?>> dependencies = {}; | ||
|
|
||
| T track<T>(T Function() callback) { | ||
| final _DependencyTracker? previous = instance; | ||
| instance = this; | ||
| try { | ||
| return callback(); | ||
| } finally { | ||
| instance = previous; | ||
| } | ||
| } | ||
|
|
||
| void _reportRead(notifiers.GenUiValueListenable<Object?> listenable) { | ||
| dependencies.add(listenable); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.