Skip to content

Commit 17da4e6

Browse files
committed
chore: Improved how push operations sorting was handled.
1 parent 9ced087 commit 17da4e6

4 files changed

Lines changed: 40 additions & 21 deletions

File tree

lib/model/backend/synchronization/push/operation.dart

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
129138
extension 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
}

lib/model/backend/synchronization/queue.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ class PushOperationsQueue extends AsyncNotifier<List<PushOperation>> {
6767
.read(backendClientProvider.notifier)
6868
.sendHttpRequest(
6969
SynchronizationPushRequest(
70-
operations: compactedOperations,
70+
operations: compactedOperations.reversed.toList(),
7171
),
7272
);
7373
if (result is! ResultSuccess<SynchronizationPushResponse>) {

lib/model/database/database.dart

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,11 +154,13 @@ class AppDatabase extends _$AppDatabase {
154154
deletedTotps,
155155
[
156156
for (MapEntry<String, DateTime> entry in merged.entries)
157-
_DriftDeletedTotp(uuid: entry.key, deletedAt: entry.value),
157+
_DriftDeletedTotp(
158+
uuid: entry.key,
159+
deletedAt: entry.value,
160+
),
158161
],
159162
);
160163
});
161-
162164
}
163165

164166
/// Marks the given [totp] as not deleted.
@@ -178,7 +180,12 @@ class AppDatabase extends _$AppDatabase {
178180

179181
/// Selects the pending backend push operations.
180182
Selectable<PushOperation> _selectPendingBackendPushOperations() {
181-
SimpleSelectStatement<$PendingBackendPushOperationsTable, _DriftBackendPushOperation> operations = select(pendingBackendPushOperations)..orderBy([(table) => OrderingTerm.asc(table.createdAt)]);
183+
SimpleSelectStatement<$PendingBackendPushOperationsTable, _DriftBackendPushOperation> operations = select(pendingBackendPushOperations)
184+
..orderBy([
185+
(table) => OrderingTerm.desc(table.createdAt),
186+
(table) => OrderingTerm.asc(table.kind),
187+
(table) => OrderingTerm.asc(table.uuid),
188+
]);
182189
return operations.map((operation) => operation.asBackendPushOperation);
183190
}
184191

lib/model/database/tables.dart

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,3 @@ class BackendPushOperationErrors extends Table {
8787
/// Maps to [PushOperationResult.createdAt].
8888
DateTimeColumn get createdAt => dateTime().withDefault(currentDateAndTime)();
8989
}
90-
91-
/// Represents a push operation kind.
92-
enum PushOperationKind {
93-
/// Represents a set operation.
94-
set,
95-
96-
/// Represents a delete operation.
97-
delete,
98-
}

0 commit comments

Comments
 (0)