Skip to content

Commit 09c22c8

Browse files
committed
Add convenience function to parse over-the-wire changesets
1 parent 19db953 commit 09c22c8

File tree

4 files changed

+24
-2
lines changed

4 files changed

+24
-2
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## 5.0.2
2+
- Add convenience function to parse over-the-wire changesets
3+
14
## 5.0.1
25
- Fix dependency compatibility with the Flutter SDK
36

lib/src/crdt.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ abstract mixin class Crdt {
9090
}
9191
}
9292

93+
/// Thrown on merge errors. Contains the failed payload to help with debugging
94+
/// large datasets.
9395
class MergeError<T> {
9496
final T error;
9597
final String table;

lib/src/types.dart

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,24 @@
1+
import 'hlc.dart';
2+
13
typedef CrdtRecord = Map<String, Object?>;
24
typedef CrdtTableChangeset = List<CrdtRecord>;
35
typedef CrdtChangeset = Map<String, CrdtTableChangeset>;
46

7+
/// Utility function to simplify parsing untyped changesets.
8+
/// It performs all necessary casts to satisfy Dart's type system, and parses
9+
/// Hlc timestamps.
10+
/// Useful when receiving datasets over the wire.
11+
CrdtChangeset parseCrdtChangeset(Map<String, dynamic> message) =>
12+
// Cast payload to CrdtChangeset
13+
message.map((table, records) => MapEntry(
14+
table,
15+
(records as List)
16+
.cast<Map<String, dynamic>>()
17+
// Parse Hlc
18+
.map((e) => e.map((key, value) =>
19+
MapEntry(key, key == 'hlc' ? Hlc.parse(value) : value)))
20+
.toList()));
21+
522
extension CrdtChangesetX on CrdtChangeset {
623
/// Convenience method to get number of records in a changeset
724
int get recordCount => values.fold<int>(0, (prev, e) => prev + e.length);

pubspec.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: crdt
22
description: Dart implementation of Conflict-free Replicated Data Types (CRDTs).
3-
version: 5.0.1
3+
version: 5.0.2
44
homepage: https://github.com/cachapa/crdt
55
repository: https://github.com/cachapa/crdt
66
issue_tracker: https://github.com/cachapa/crdt/issues
@@ -9,7 +9,7 @@ environment:
99
sdk: '>=3.0.0 <4.0.0'
1010

1111
dependencies:
12-
# Prevent ongoing dependency issues since this package is bundled in the Flutter SDK
12+
# Prevent ongoing dependency issues since this package is bundled with the Flutter SDK
1313
meta: '>=1.0.0 <2.0.0'
1414
uuid: ^4.0.0
1515

0 commit comments

Comments
 (0)