Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 14 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@ The collaborative data structures you attach to a doc are:
- [SynkList](#synklist)
- [SynkText](#synktext)
- [SynkInt](#synkint)
- [SynkString](#synkstring)
- [SynkBool](#synkbool)
- [SynkDouble](#synkdouble)
- [SynkValue<T>](#synkvaluet)

### Creating a document

Expand Down Expand Up @@ -121,45 +119,31 @@ counter.value; // 8

> Full example - [`example/synk_int_example.dart`](example/synk_int_example.dart)

### `SynkString`
### `SynkValue<T>`

A single-value string register. Concurrent writes are resolved deterministically via LWW — the write with the higher logical clock wins. If clocks tie, the higher `clientId` wins.
A generic single-value collaborative register. Concurrent writes are resolved deterministically via LWW — the write with the higher logical clock wins. If clocks tie, the higher `clientId` wins.

```dart
final title = SynkString(doc, 'title');
`SynkValue` supports generic JSON-serializable primitives like `String`, `num`, and `bool`. If the value has not been set yet, it returns `null`.

```dart
// A collaborative string
final title = SynkValue<String>(doc, 'title');
title.value; // null
title.set('Hello, World!');
title.value; // 'Hello, World!'
```

> Full example - [`example/synk_string_example.dart`](example/synk_string_example.dart)

### `SynkBool`

Same LWW semantics as `SynkString`, with an extra `toggle()` helper.

```dart
final flag = SynkBool(doc, 'isPublished');

// A collaborative boolean
final flag = SynkValue<bool>(doc, 'isPublished');
flag.set(true);
flag.toggle();
flag.value; // false
```

> Full example - [`example/synk_bool_example.dart`](example/synk_bool_example.dart)

### `SynkDouble`

Same LWW semantics as `SynkString`. JSON-safe: integer payloads from the wire are cast to `double` transparently.

```dart
final price = SynkDouble(doc, 'price');
flag.value; // true

// A collaborative double
final price = SynkValue<double>(doc, 'price');
price.set(9.99);
price.value; // 9.99
```

> Full example - [`example/synk_double_example.dart`](example/synk_double_example.dart)
> Full example - [`example/synk_value_example.dart`](example/synk_value_example.dart)

## Syncing Between Peers

Expand Down
34 changes: 0 additions & 34 deletions example/synk_bool_example.dart

This file was deleted.

37 changes: 0 additions & 37 deletions example/synk_double_example.dart

This file was deleted.

37 changes: 0 additions & 37 deletions example/synk_string_example.dart

This file was deleted.

53 changes: 53 additions & 0 deletions example/synk_value_example.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// ignore_for_file: avoid_print

import 'package:synk/synk.dart';

void main() {
final docA = SynkDoc(clientId: 1);
final docB = SynkDoc(clientId: 2);

print('--- SynkValue Example ---');
print('SynkValue<T> behaves like a Last-Writer-Wins (LWW) Register');
print('It stores a single, overwritable generic value.\n');

// Initialize two peers with the same variable name
final titleA = SynkValue<String>(docA, 'pageTitle');
final titleB = SynkValue<String>(docB, 'pageTitle');

print('Initial titleA value: ${titleA.value}'); // null (unset)
print('Initial titleB value: ${titleB.value}\n');

// Peer A edits the document
titleA.set('Flutter Conference 2026');
print('Alice set title to: ${titleA.value}');

// Peer B edits concurrently offline (a conflict!)
titleB.set('Dart Developer Summit');
print('Bob set title to: ${titleB.value}\n');

// Sync to resolve the conflict completely
print('--- Synk resolves the tie automatically ---');
final aliceSv = SynkProtocol.encodeStateVector(docA);
final bobUpdate = SynkProtocol.encodeStateAsUpdate(docB, aliceSv);
SynkProtocol.applyUpdate(docA, bobUpdate);

final bobSv = SynkProtocol.encodeStateVector(docB);
final aliceUpdate = SynkProtocol.encodeStateAsUpdate(docA, bobSv);
SynkProtocol.applyUpdate(docB, aliceUpdate);

print("Alice's final title: ${titleA.value}");
print("Bob's final title: ${titleB.value}");
// Notice that they both converge to 'Dart Developer Summit' because when
// clocks tie, the higher clientId wins deterministically!

// They also work seamlessly for numbers and booleans
final flag = SynkValue<bool>(docA, 'isVisible');
final opacity = SynkValue<double>(docA, 'opacity');

flag.set(true);
opacity.set(0.7);

print('\nOther types supported:');
print('bool flag: ${flag.value}');
print('double opacity: ${opacity.value}');
}
73 changes: 0 additions & 73 deletions lib/src/types/synk_double.dart

This file was deleted.

75 changes: 0 additions & 75 deletions lib/src/types/synk_string.dart

This file was deleted.

Loading