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
19 changes: 9 additions & 10 deletions lib/src/types/synk_list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ class SynkList {
// used for safe topological replay of existing history.
final Set<ID> _integrated = {};

// Cache for the length of the list (number of non-deleted items).
int _length = 0;

// ── Internals ────────────────────────────────────────────────────────────

void _processItem(Item item) {
Expand Down Expand Up @@ -91,7 +94,10 @@ class SynkList {
if (item.deleted) {
if (item.leftOrigin != null) {
final target = _findById(item.leftOrigin!);
target?.delete();
if (target != null && !target.deleted) {
target.delete();
_length--;
}
}
_integrated.add(item.id);
return;
Expand Down Expand Up @@ -137,6 +143,7 @@ class SynkList {
item.right!.left = item;
}

_length++;
_integrated.add(item.id);
}

Expand Down Expand Up @@ -243,15 +250,7 @@ class SynkList {
}

/// The number of non-deleted elements.
int get length {
var count = 0;
var current = _start;
while (current != null) {
if (!current.deleted) count++;
current = current.right;
}
return count;
}
int get length => _length;

/// Returns all non-deleted values as an ordered [List].
List<dynamic> toList() {
Expand Down
19 changes: 9 additions & 10 deletions lib/src/types/synk_text.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ class SynkText {
// used for safe topological replay of existing history.
final Set<ID> _integrated = {};

// Cache for the length of the sequence (number of non-deleted characters).
int _length = 0;

// ── Internals ────────────────────────────────────────────────────────────

void _processItem(Item item) {
Expand Down Expand Up @@ -92,7 +95,10 @@ class SynkText {
if (item.deleted) {
if (item.leftOrigin != null) {
final target = _findById(item.leftOrigin!);
target?.delete();
if (target != null && !target.deleted) {
target.delete();
_length--;
}
}
_integrated.add(item.id);
return;
Expand Down Expand Up @@ -138,6 +144,7 @@ class SynkText {
item.right!.left = item;
}

_length++;
_integrated.add(item.id);
}

Expand Down Expand Up @@ -251,15 +258,7 @@ class SynkText {
}

/// The number of active (non-deleted) characters.
int get length {
var count = 0;
var current = _start;
while (current != null) {
if (!current.deleted) count++;
current = current.right;
}
return count;
}
int get length => _length;

/// Returns the complete visible text string.
String get text {
Expand Down
Loading