@@ -77,7 +77,7 @@ void commitScene(PersistedScene scene) {
77
77
if (_retainedSurfaces.isNotEmpty) {
78
78
for (int i = 0 ; i < _retainedSurfaces.length; i++ ) {
79
79
final PersistedSurface retainedSurface = _retainedSurfaces[i];
80
- assert (retainedSurface.isPendingRetention );
80
+ assert (debugAssertSurfaceState ( retainedSurface, PersistedSurfaceState .pendingRetention) );
81
81
retainedSurface.state = PersistedSurfaceState .active;
82
82
}
83
83
_retainedSurfaces = < PersistedSurface > [];
@@ -483,6 +483,39 @@ enum PersistedSurfaceState {
483
483
released,
484
484
}
485
485
486
+ class PersistedSurfaceException implements Exception {
487
+ PersistedSurfaceException (this .surface, this .message);
488
+
489
+ final PersistedSurface surface;
490
+ final String message;
491
+
492
+ @override
493
+ String toString () {
494
+ if (assertionsEnabled) {
495
+ return '${surface .runtimeType }: $message ' ;
496
+ }
497
+ return super .toString ();
498
+ }
499
+ }
500
+
501
+ /// Verifies that the [surface] is in one of the valid states.
502
+ ///
503
+ /// This function should be used inside an assertion expression.
504
+ bool debugAssertSurfaceState (PersistedSurface surface, PersistedSurfaceState state1, [PersistedSurfaceState state2, PersistedSurfaceState state3]) {
505
+ final List <PersistedSurfaceState > validStates = [ state1, state2, state3 ];
506
+
507
+ if (validStates.contains (surface.state)) {
508
+ return true ;
509
+ }
510
+
511
+ throw PersistedSurfaceException (
512
+ surface,
513
+ 'is in an unexpected state.\n '
514
+ 'Expected one of: ${validStates .whereType <PersistedSurfaceState >().join (', ' )}\n '
515
+ 'But was: ${surface .state }' ,
516
+ );
517
+ }
518
+
486
519
/// A node in the tree built by [SceneBuilder] that contains information used to
487
520
/// compute the fewest amount of mutations necessary to update the browser DOM.
488
521
abstract class PersistedSurface implements ui.EngineLayer {
@@ -537,7 +570,7 @@ abstract class PersistedSurface implements ui.EngineLayer {
537
570
/// reused before the request to retain it came in. In this case, the surface
538
571
/// is [revive] d and rebuilt from scratch.
539
572
void tryRetain () {
540
- assert (isActive || isReleased );
573
+ assert (debugAssertSurfaceState ( this , PersistedSurfaceState .active, PersistedSurfaceState .released) );
541
574
// Request that the layer is retained, but only if it's still active. It
542
575
// could have been released.
543
576
if (isActive) {
@@ -559,7 +592,7 @@ abstract class PersistedSurface implements ui.EngineLayer {
559
592
@visibleForTesting
560
593
@protected
561
594
void revive () {
562
- assert (isReleased );
595
+ assert (debugAssertSurfaceState ( this , PersistedSurfaceState .released) );
563
596
state = PersistedSurfaceState .created;
564
597
}
565
598
@@ -636,7 +669,7 @@ abstract class PersistedSurface implements ui.EngineLayer {
636
669
}
637
670
}
638
671
assert (rootElement == null );
639
- assert (isCreated );
672
+ assert (debugAssertSurfaceState ( this , PersistedSurfaceState .created) );
640
673
rootElement = createElement ();
641
674
applyWebkitClipFix (rootElement);
642
675
if (_debugExplainSurfaceStats) {
@@ -656,7 +689,7 @@ abstract class PersistedSurface implements ui.EngineLayer {
656
689
@mustCallSuper
657
690
void adoptElements (covariant PersistedSurface oldSurface) {
658
691
assert (oldSurface.rootElement != null );
659
- assert (oldSurface.isActive || oldSurface.isPendingUpdate );
692
+ assert (debugAssertSurfaceState ( oldSurface, PersistedSurfaceState .active, PersistedSurfaceState .pendingUpdate) );
660
693
assert (() {
661
694
if (oldSurface.isPendingUpdate) {
662
695
final PersistedContainerSurface self = this ;
@@ -684,7 +717,8 @@ abstract class PersistedSurface implements ui.EngineLayer {
684
717
void update (covariant PersistedSurface oldSurface) {
685
718
assert (oldSurface != null );
686
719
assert (! identical (oldSurface, this ));
687
- assert (isCreated && (oldSurface.isPendingUpdate || oldSurface.isActive));
720
+ assert (debugAssertSurfaceState (this , PersistedSurfaceState .created));
721
+ assert (debugAssertSurfaceState (oldSurface, PersistedSurfaceState .active, PersistedSurfaceState .pendingUpdate));
688
722
689
723
adoptElements (oldSurface);
690
724
@@ -729,7 +763,7 @@ abstract class PersistedSurface implements ui.EngineLayer {
729
763
@protected
730
764
@mustCallSuper
731
765
void discard () {
732
- assert (isActive );
766
+ assert (debugAssertSurfaceState ( this , PersistedSurfaceState .active) );
733
767
assert (rootElement != null );
734
768
// TODO(yjbanov): it may be wasteful to recursively disassemble the DOM tree
735
769
// node by node. It should be sufficient to detach the root
@@ -919,8 +953,7 @@ abstract class PersistedContainerSurface extends PersistedSurface {
919
953
920
954
/// Adds a child to this container.
921
955
void appendChild (PersistedSurface child) {
922
- assert (child.isCreated || child.isPendingRetention || child.isPendingUpdate,
923
- 'Child is in incorrect state ${child .state }' );
956
+ assert (debugAssertSurfaceState (child, PersistedSurfaceState .created, PersistedSurfaceState .pendingRetention, PersistedSurfaceState .pendingUpdate));
924
957
_children.add (child);
925
958
child.parent = this ;
926
959
}
@@ -957,10 +990,10 @@ abstract class PersistedContainerSurface extends PersistedSurface {
957
990
} else if (child is PersistedContainerSurface && child.oldLayer != null ) {
958
991
final PersistedSurface oldLayer = child.oldLayer;
959
992
assert (oldLayer.rootElement != null );
960
- assert (oldLayer.isPendingUpdate );
993
+ assert (debugAssertSurfaceState ( oldLayer, PersistedSurfaceState .pendingUpdate) );
961
994
child.update (child.oldLayer);
962
995
} else {
963
- assert (child.isCreated );
996
+ assert (debugAssertSurfaceState ( child, PersistedSurfaceState .created) );
964
997
assert (child.rootElement == null );
965
998
child.build ();
966
999
}
@@ -984,10 +1017,10 @@ abstract class PersistedContainerSurface extends PersistedSurface {
984
1017
985
1018
@override
986
1019
void update (PersistedContainerSurface oldSurface) {
987
- assert (oldSurface.isActive || oldSurface.isPendingUpdate );
1020
+ assert (debugAssertSurfaceState ( oldSurface, PersistedSurfaceState .active, PersistedSurfaceState .pendingUpdate) );
988
1021
assert (runtimeType == oldSurface.runtimeType);
989
1022
super .update (oldSurface);
990
- assert (oldSurface.isReleased );
1023
+ assert (debugAssertSurfaceState ( oldSurface, PersistedSurfaceState .released) );
991
1024
992
1025
if (oldSurface._children.isEmpty) {
993
1026
_updateZeroToMany (oldSurface);
@@ -1019,8 +1052,7 @@ abstract class PersistedContainerSurface extends PersistedSurface {
1019
1052
}
1020
1053
for (int i = 0 ; i < _children.length; i++ ) {
1021
1054
final PersistedSurface newChild = _children[i];
1022
- assert (newChild.isActive || newChild.isPendingRetention,
1023
- 'New child is in incorrect state ${newChild .state }' );
1055
+ assert (debugAssertSurfaceState (newChild, PersistedSurfaceState .active, PersistedSurfaceState .pendingRetention));
1024
1056
assert (newChild.rootElement != null );
1025
1057
assert (newChild.rootElement.parent == childContainer);
1026
1058
}
@@ -1043,17 +1075,17 @@ abstract class PersistedContainerSurface extends PersistedSurface {
1043
1075
final PersistedSurface newChild = _children[i];
1044
1076
if (newChild.isPendingRetention) {
1045
1077
newChild.retain ();
1046
- assert (newChild.isPendingRetention );
1078
+ assert (debugAssertSurfaceState ( newChild, PersistedSurfaceState .pendingRetention) );
1047
1079
} else if (newChild is PersistedContainerSurface &&
1048
1080
newChild.oldLayer != null ) {
1049
1081
final PersistedContainerSurface oldLayer = newChild.oldLayer;
1050
- assert (oldLayer.isPendingUpdate );
1082
+ assert (debugAssertSurfaceState ( oldLayer, PersistedSurfaceState .pendingUpdate) );
1051
1083
newChild.update (oldLayer);
1052
- assert (oldLayer.isReleased );
1053
- assert (newChild.isActive );
1084
+ assert (debugAssertSurfaceState ( oldLayer, PersistedSurfaceState .released) );
1085
+ assert (debugAssertSurfaceState ( newChild, PersistedSurfaceState .active) );
1054
1086
} else {
1055
1087
newChild.build ();
1056
- assert (newChild.isActive );
1088
+ assert (debugAssertSurfaceState ( newChild, PersistedSurfaceState .active) );
1057
1089
}
1058
1090
assert (newChild.rootElement != null );
1059
1091
containerElement.append (newChild.rootElement);
@@ -1093,14 +1125,14 @@ abstract class PersistedContainerSurface extends PersistedSurface {
1093
1125
newChild.retain ();
1094
1126
1095
1127
_discardActiveChildren (oldSurface);
1096
- assert (newChild.isPendingRetention );
1128
+ assert (debugAssertSurfaceState ( newChild, PersistedSurfaceState .pendingRetention) );
1097
1129
return ;
1098
1130
}
1099
1131
1100
1132
// Updated child is moved to the correct location in the tree; all others
1101
1133
// are released.
1102
1134
if (newChild is PersistedContainerSurface && newChild.oldLayer != null ) {
1103
- assert (newChild.oldLayer.isPendingUpdate );
1135
+ assert (debugAssertSurfaceState ( newChild.oldLayer, PersistedSurfaceState .pendingUpdate) );
1104
1136
assert (newChild.rootElement == null );
1105
1137
assert (newChild.oldLayer.rootElement != null );
1106
1138
@@ -1113,12 +1145,12 @@ abstract class PersistedContainerSurface extends PersistedSurface {
1113
1145
1114
1146
newChild.update (oldLayer);
1115
1147
_discardActiveChildren (oldSurface);
1116
- assert (oldLayer.isReleased );
1117
- assert (newChild.isActive );
1148
+ assert (debugAssertSurfaceState ( oldLayer, PersistedSurfaceState .released) );
1149
+ assert (debugAssertSurfaceState ( newChild, PersistedSurfaceState .active) );
1118
1150
return ;
1119
1151
}
1120
1152
1121
- assert (newChild.isCreated );
1153
+ assert (debugAssertSurfaceState ( newChild, PersistedSurfaceState .created) );
1122
1154
1123
1155
PersistedSurface bestMatch;
1124
1156
double bestScore = 2.0 ;
@@ -1135,19 +1167,19 @@ abstract class PersistedContainerSurface extends PersistedSurface {
1135
1167
}
1136
1168
1137
1169
if (bestMatch != null ) {
1138
- assert (bestMatch.isActive );
1170
+ assert (debugAssertSurfaceState ( bestMatch, PersistedSurfaceState .active) );
1139
1171
newChild.update (bestMatch);
1140
1172
1141
1173
// Move the HTML node if necessary.
1142
1174
if (newChild.rootElement.parent != childContainer) {
1143
1175
childContainer.append (newChild.rootElement);
1144
1176
}
1145
1177
1146
- assert (bestMatch.isReleased );
1178
+ assert (debugAssertSurfaceState ( bestMatch, PersistedSurfaceState .released) );
1147
1179
} else {
1148
1180
newChild.build ();
1149
1181
childContainer.append (newChild.rootElement);
1150
- assert (newChild.isActive );
1182
+ assert (debugAssertSurfaceState ( newChild, PersistedSurfaceState .active) );
1151
1183
}
1152
1184
1153
1185
// Child nodes that were not used this frame that are still active and not
@@ -1202,29 +1234,29 @@ abstract class PersistedContainerSurface extends PersistedSurface {
1202
1234
final PersistedSurface newChild = _children[bottomInNew];
1203
1235
if (newChild.isPendingRetention) {
1204
1236
newChild.retain ();
1205
- assert (newChild.isPendingRetention );
1237
+ assert (debugAssertSurfaceState ( newChild, PersistedSurfaceState .pendingRetention) );
1206
1238
} else if (newChild is PersistedContainerSurface &&
1207
1239
newChild.oldLayer != null ) {
1208
1240
final PersistedContainerSurface oldLayer = newChild.oldLayer;
1209
- assert (oldLayer.isPendingUpdate );
1241
+ assert (debugAssertSurfaceState ( oldLayer, PersistedSurfaceState .pendingUpdate) );
1210
1242
newChild.update (oldLayer);
1211
- assert (oldLayer.isReleased );
1212
- assert (newChild.isActive );
1243
+ assert (debugAssertSurfaceState ( oldLayer, PersistedSurfaceState .released) );
1244
+ assert (debugAssertSurfaceState ( newChild, PersistedSurfaceState .active) );
1213
1245
} else {
1214
1246
final PersistedSurface matchedOldChild = matches[newChild];
1215
1247
if (matchedOldChild != null ) {
1216
- assert (matchedOldChild.isActive );
1248
+ assert (debugAssertSurfaceState ( matchedOldChild, PersistedSurfaceState .active) );
1217
1249
newChild.update (matchedOldChild);
1218
- assert (matchedOldChild.isReleased );
1219
- assert (newChild.isActive );
1250
+ assert (debugAssertSurfaceState ( matchedOldChild, PersistedSurfaceState .released) );
1251
+ assert (debugAssertSurfaceState ( newChild, PersistedSurfaceState .active) );
1220
1252
} else {
1221
1253
newChild.build ();
1222
- assert (newChild.isActive );
1254
+ assert (debugAssertSurfaceState ( newChild, PersistedSurfaceState .active) );
1223
1255
}
1224
1256
}
1225
1257
insertDomNodeIfMoved (newChild);
1226
1258
assert (newChild.rootElement != null );
1227
- assert (newChild.isActive || newChild.isPendingRetention );
1259
+ assert (debugAssertSurfaceState ( newChild, PersistedSurfaceState .active, PersistedSurfaceState .pendingRetention) );
1228
1260
nextSibling = newChild;
1229
1261
}
1230
1262
0 commit comments