Skip to content

Commit 0683aee

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

3 files changed

Lines changed: 804 additions & 10 deletions

File tree

crates/loro-wasm/index.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import {
2727
/**
2828
* @deprecated Please use LoroDoc
2929
*/
30-
export class Loro extends LoroDoc {}
30+
export class Loro extends LoroDoc { }
3131

3232
const CONTAINER_TYPES = [
3333
"Map",
@@ -94,14 +94,14 @@ export function getType<T>(
9494
): T extends LoroText
9595
? "Text"
9696
: T extends LoroMap<any>
97-
? "Map"
98-
: T extends LoroTree<any>
99-
? "Tree"
100-
: T extends LoroList<any>
101-
? "List"
102-
: T extends LoroCounter
103-
? "Counter"
104-
: "Json" {
97+
? "Map"
98+
: T extends LoroTree<any>
99+
? "Tree"
100+
: T extends LoroList<any>
101+
? "List"
102+
: T extends LoroCounter
103+
? "Counter"
104+
: "Json" {
105105
if (isContainer(value)) {
106106
return value.kind() as unknown as any;
107107
}
@@ -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)