Skip to content

Commit

Permalink
Add convenience function to parse over-the-wire changesets
Browse files Browse the repository at this point in the history
  • Loading branch information
cachapa committed Sep 12, 2023
1 parent 19db953 commit 09c22c8
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 5.0.2
- Add convenience function to parse over-the-wire changesets

## 5.0.1
- Fix dependency compatibility with the Flutter SDK

Expand Down
2 changes: 2 additions & 0 deletions lib/src/crdt.dart
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ abstract mixin class Crdt {
}
}

/// Thrown on merge errors. Contains the failed payload to help with debugging
/// large datasets.
class MergeError<T> {
final T error;
final String table;
Expand Down
17 changes: 17 additions & 0 deletions lib/src/types.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,24 @@
import 'hlc.dart';

typedef CrdtRecord = Map<String, Object?>;
typedef CrdtTableChangeset = List<CrdtRecord>;
typedef CrdtChangeset = Map<String, CrdtTableChangeset>;

/// Utility function to simplify parsing untyped changesets.
/// It performs all necessary casts to satisfy Dart's type system, and parses
/// Hlc timestamps.
/// Useful when receiving datasets over the wire.
CrdtChangeset parseCrdtChangeset(Map<String, dynamic> message) =>
// Cast payload to CrdtChangeset
message.map((table, records) => MapEntry(
table,
(records as List)
.cast<Map<String, dynamic>>()
// Parse Hlc
.map((e) => e.map((key, value) =>
MapEntry(key, key == 'hlc' ? Hlc.parse(value) : value)))
.toList()));

extension CrdtChangesetX on CrdtChangeset {
/// Convenience method to get number of records in a changeset
int get recordCount => values.fold<int>(0, (prev, e) => prev + e.length);
Expand Down
4 changes: 2 additions & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: crdt
description: Dart implementation of Conflict-free Replicated Data Types (CRDTs).
version: 5.0.1
version: 5.0.2
homepage: https://github.com/cachapa/crdt
repository: https://github.com/cachapa/crdt
issue_tracker: https://github.com/cachapa/crdt/issues
Expand All @@ -9,7 +9,7 @@ environment:
sdk: '>=3.0.0 <4.0.0'

dependencies:
# Prevent ongoing dependency issues since this package is bundled in the Flutter SDK
# Prevent ongoing dependency issues since this package is bundled with the Flutter SDK
meta: '>=1.0.0 <2.0.0'
uuid: ^4.0.0

Expand Down

0 comments on commit 09c22c8

Please sign in to comment.