@@ -30,7 +30,7 @@ sealed class PushOperation<T> with EquatableMixin {
3030 });
3131
3232 /// The operation kind.
33- String get _kind;
33+ PushOperationKind get _kind;
3434
3535 @override
3636 List <Object ?> get props => [
@@ -46,7 +46,7 @@ sealed class PushOperation<T> with EquatableMixin {
4646 }) => {
4747 'uuid' : uuid,
4848 'payload' : payload,
49- 'kind' : _kind,
49+ 'kind' : _kind.name ,
5050 if (! httpRequest) ...{
5151 'createdAt' : createdAt.millisecondsSinceEpoch,
5252 },
@@ -76,7 +76,7 @@ class SetTotpsPushOperation extends PushOperation<Map<String, dynamic>> {
7676 );
7777
7878 @override
79- String get _kind => ' set' ;
79+ PushOperationKind get _kind => PushOperationKind . set ;
8080
8181 @override
8282 SetTotpsPushOperation copyWith ({
@@ -112,7 +112,7 @@ class DeleteTotpsPushOperation extends PushOperation<Map<String, int>> {
112112 );
113113
114114 @override
115- String get _kind => ' delete' ;
115+ PushOperationKind get _kind => PushOperationKind . delete;
116116
117117 @override
118118 DeleteTotpsPushOperation copyWith ({
@@ -125,19 +125,40 @@ class DeleteTotpsPushOperation extends PushOperation<Map<String, int>> {
125125 );
126126}
127127
128+ /// Represents a push operation kind.
129+ enum PushOperationKind {
130+ /// Represents a delete operation.
131+ delete,
132+
133+ /// Represents a set operation.
134+ set ,
135+ }
136+
128137/// Allows to compact a list of push operations.
129138extension Compact on List <PushOperation > {
130- /// Compacts the current push operations while preserving their relative order.
131- /// Only the latest operation for each TOTP UUID is kept.
139+ /// Compacts the current push operations. Only the latest operation for each TOTP UUID is kept.
132140 List <PushOperation > get compacted {
133141 if (isEmpty) {
134142 return [];
135143 }
136144
145+ List <PushOperation > sorted = List .of (this )
146+ ..sort ((a, b) {
147+ int createdAtComparison = b.createdAt.compareTo (a.createdAt);
148+ if (createdAtComparison != 0 ) {
149+ return createdAtComparison;
150+ }
151+ int kindComparison = a._kind.index.compareTo (b._kind.index);
152+ if (kindComparison != 0 ) {
153+ return kindComparison;
154+ }
155+ return a.uuid.compareTo (b.uuid);
156+ });
157+
137158 Set <String > processedTotpUuids = {};
138159 List <PushOperation > result = [];
139160
140- for (PushOperation operation in reversed ) {
161+ for (PushOperation operation in sorted ) {
141162 switch (operation) {
142163 case SetTotpsPushOperation (: final payload):
143164 Map <String , dynamic > newPayload = {
@@ -160,6 +181,6 @@ extension Compact on List<PushOperation> {
160181 }
161182 }
162183
163- return result.reversed. toList () ;
184+ return result;
164185 }
165186}
0 commit comments