Skip to content

Commit 50aef14

Browse files
committed
feat: add replaceWithShallow to loro-wasm
1 parent 2a8c952 commit 50aef14

3 files changed

Lines changed: 882 additions & 1 deletion

File tree

crates/loro-wasm/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,7 @@ decorateMethods(LoroDoc.prototype, [
502502
"commit",
503503
"getCursorPos",
504504
"revertTo",
505+
"replaceWithShallow",
505506
"export",
506507
"exportJsonUpdates",
507508
"exportJsonInIdSpan",

crates/loro-wasm/src/lib.rs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1348,6 +1348,34 @@ impl LoroDoc {
13481348
self.doc.is_shallow()
13491349
}
13501350

1351+
/// Replace the current document state with a shallow snapshot at the given frontiers.
1352+
///
1353+
/// This method trims the history in place, preserving subscriptions and configuration.
1354+
/// After calling this method, the document will only contain history from the specified
1355+
/// frontiers onwards.
1356+
///
1357+
/// @param frontiers - The frontiers to trim history to
1358+
///
1359+
/// @example
1360+
/// ```ts
1361+
/// import { LoroDoc } from "loro-crdt";
1362+
///
1363+
/// const doc = new LoroDoc();
1364+
/// const text = doc.getText("text");
1365+
/// text.insert(0, "Hello World!");
1366+
/// doc.commit();
1367+
/// const frontiers = doc.frontiers();
1368+
/// // ... more operations ...
1369+
/// // Trim history to the frontiers, discarding earlier operations
1370+
/// doc.replaceWithShallow(frontiers);
1371+
/// ```
1372+
#[wasm_bindgen(js_name = "replaceWithShallow")]
1373+
pub fn replace_with_shallow(&self, frontiers: Vec<JsID>) -> JsResult<()> {
1374+
let frontiers = ids_to_frontiers(frontiers)?;
1375+
self.doc.replace_with_shallow(&frontiers)?;
1376+
Ok(())
1377+
}
1378+
13511379
/// The doc only contains the history since this version
13521380
///
13531381
/// This is empty if the doc is not shallow.
@@ -1519,7 +1547,8 @@ impl LoroDoc {
15191547
let json = if IN_PRE_COMMIT_CALLBACK.with(|f| f.get()) {
15201548
self.doc.export_json_in_id_span(id_span)
15211549
} else {
1522-
self.doc.with_barrier(|| self.doc.export_json_in_id_span(id_span))
1550+
self.doc
1551+
.with_barrier(|| self.doc.export_json_in_id_span(id_span))
15231552
};
15241553
let s = serde_wasm_bindgen::Serializer::new().serialize_maps_as_objects(true);
15251554
let v = json

0 commit comments

Comments
 (0)