diff --git a/lib/src/types/synk_list.dart b/lib/src/types/synk_list.dart index 7fafd43..3788bb5 100644 --- a/lib/src/types/synk_list.dart +++ b/lib/src/types/synk_list.dart @@ -33,6 +33,9 @@ class SynkList { // used for safe topological replay of existing history. final Set _integrated = {}; + // Cache for the length of the list (number of non-deleted items). + int _length = 0; + // ── Internals ──────────────────────────────────────────────────────────── void _processItem(Item item) { @@ -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; @@ -137,6 +143,7 @@ class SynkList { item.right!.left = item; } + _length++; _integrated.add(item.id); } @@ -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 toList() { diff --git a/lib/src/types/synk_text.dart b/lib/src/types/synk_text.dart index cda45b8..d0ce8c9 100644 --- a/lib/src/types/synk_text.dart +++ b/lib/src/types/synk_text.dart @@ -34,6 +34,9 @@ class SynkText { // used for safe topological replay of existing history. final Set _integrated = {}; + // Cache for the length of the sequence (number of non-deleted characters). + int _length = 0; + // ── Internals ──────────────────────────────────────────────────────────── void _processItem(Item item) { @@ -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; @@ -138,6 +144,7 @@ class SynkText { item.right!.left = item; } + _length++; _integrated.add(item.id); } @@ -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 {