@@ -12,6 +12,9 @@ import 'package:flutter/foundation.dart' show objectRuntimeType;
12
12
import '../dart/model.dart' ;
13
13
14
14
/// Signature for the callback passed to [DynamicContent.subscribe] .
15
+ ///
16
+ /// Do not modify the provided value (e.g. if it is a map or list). Doing so
17
+ /// would leave the [DynamicContent] in an inconsistent state.
15
18
typedef SubscriptionCallback = void Function (Object value);
16
19
17
20
/// Returns a copy of a data structure if it consists of only [DynamicMap] s,
@@ -116,9 +119,12 @@ Object? deepClone(Object? template) {
116
119
/// [missing] as the new value. It is not an error to subscribe to missing data.
117
120
/// It _is_ an error to add [missing] values to the data model, however.
118
121
///
122
+ /// To subscribe to the root of the [DynamicContent] , use an empty list as the
123
+ /// key when subscribing.
124
+ ///
119
125
/// The [LocalWidgetBuilder] s passed to a [LocalWidgetLibrary] use a
120
126
/// [DataSource] as their interface into the [DynamicContent] . To ensure the
121
- /// integrity of the update mechanism, that interface only allows access to
127
+ /// integrity of the update mechanism, _that_ interface only allows access to
122
128
/// leaves, not intermediate nodes (maps and lists).
123
129
///
124
130
/// It is an error to subscribe to the same key multiple times with the same
@@ -143,6 +149,13 @@ class DynamicContent {
143
149
/// key.
144
150
///
145
151
/// Existing keys that are not present in the given map are left unmodified.
152
+ ///
153
+ /// If the root node has subscribers (see [subscribe] ), they are called once
154
+ /// per key in `initialData` , not just a single time.
155
+ ///
156
+ /// Collections (maps and lists) in `initialData` must not be mutated after
157
+ /// calling this method; doing so would leave the [DynamicContent] in an
158
+ /// inconsistent state.
146
159
void updateAll (DynamicMap initialData) {
147
160
for (final String key in initialData.keys) {
148
161
final Object value = initialData[key] ?? missing;
@@ -156,6 +169,10 @@ class DynamicContent {
156
169
///
157
170
/// The `value` must consist exclusively of [DynamicMap] , [DynamicList] , [int] ,
158
171
/// [double] , [bool] , and [String] objects.
172
+ ///
173
+ /// Collections (maps and lists) in `value` must not be mutated after calling
174
+ /// this method; doing so would leave the [DynamicContent] in an inconsistent
175
+ /// state.
159
176
void update (String rootKey, Object value) {
160
177
_root.updateKey (rootKey, deepClone (value)! );
161
178
_scheduleCleanup ();
@@ -167,7 +184,14 @@ class DynamicContent {
167
184
/// The value is always non-null; if the value is missing, the [missing]
168
185
/// object is used instead.
169
186
///
187
+ /// The empty key refers to the root of the [DynamicContent] object (i.e.
188
+ /// the map manipulated by [updateAll] and [update] ).
189
+ ///
170
190
/// Use [unsubscribe] when the subscription is no longer needed.
191
+ ///
192
+ /// Do not modify the value returned by this method or passed to the given
193
+ /// `callback` (e.g. if it is a map or list). Changes made in this manner will
194
+ /// leave the [DynamicContent] in an inconsistent state.
171
195
Object subscribe (List <Object > key, SubscriptionCallback callback) {
172
196
return _root.subscribe (key, 0 , callback);
173
197
}
@@ -329,12 +353,6 @@ class _DynamicNode {
329
353
_sendUpdates (value);
330
354
}
331
355
332
- void _sendUpdates (Object value) {
333
- for (final SubscriptionCallback callback in _callbacks) {
334
- callback (value);
335
- }
336
- }
337
-
338
356
void updateKey (String rootKey, Object value) {
339
357
assert (_value is DynamicMap );
340
358
assert (_hasValidType (value));
@@ -345,6 +363,13 @@ class _DynamicNode {
345
363
if (_children.containsKey (rootKey)) {
346
364
_children[rootKey]! .update (value);
347
365
}
366
+ _sendUpdates (_value);
367
+ }
368
+
369
+ void _sendUpdates (Object value) {
370
+ for (final SubscriptionCallback callback in _callbacks) {
371
+ callback (value);
372
+ }
348
373
}
349
374
350
375
@override
0 commit comments