Attached vs Detached States
Quick Reference
Loro uses “attached/detached” in two distinct contexts:
- Document States - Version synchronization (latest vs historical)
- Container States - Document membership (belongs to doc vs standalone)
⚠️ These are independent concepts - a container can be attached to a detached document.
Document States
Attached (Default)
- Synchronized with latest OpLog version
- Normal editing mode
- All operations applied immediately
const = new ();
.(.()); // false - normal state
Detached (Time Travel)
- Viewing historical version
- Editing disabled by default
- OpLog has newer operations not shown
const = new ();
.("text").(0, "v1");
const = .();
.("text").(2, " -> v2");
// Time travel to v1
.();
.(.()); // true
.(.("text").()); // "v1"
Container States
Detached (Standalone)
- Created with constructors (
new LoroMap()
) - Not part of any document
- No valid ContainerID
- Used as templates
const = new ();
.(.()); // false
Attached (Document Member)
- Part of document hierarchy
- Has ContainerID
- Changes tracked in document
const = new ();
const = .("data"); // Attached
// Adding detached container returns attached version
const = new ();
const = .("text", );
.(.()); // false - original unchanged
.(.()); // true - new attached copy
Key Differences
Aspect | Document Attached/Detached | Container Attached/Detached |
---|---|---|
Purpose | Version control | Document membership |
When | Time travel, branching | Adding to document |
Independence | N/A | Independent from doc state |
Editing | Restricted when detached | Always allowed |
Common Use Cases
Time Travel
const = new ();
const = .();
// ... more edits ...
.(); // View old version
.(); // Return to latest
Container Templates
const = new ();
.("type", "task");
const = new ();
const = .("tasks");
// Reuse template multiple times
.(0, );
.(1, );
Related Documentation
- OpLog and DocState - Understanding version states
- Containers - Container types and usage
- Time Travel - Using checkout and branching