mirror of
https://github.com/loro-dev/loro.git
synced 2024-12-01 04:28:18 +00:00
feat(wasm): add methods to encode and decode Frontiers (#517)
This commit is contained in:
parent
d872ac6488
commit
e32fdbf542
2 changed files with 25 additions and 0 deletions
|
@ -48,6 +48,20 @@ fn run() {
|
|||
console_error_panic_hook::set_once();
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub fn encodeFrontiers(frontiers: Vec<JsID>) -> JsResult<Vec<u8>> {
|
||||
let frontiers = ids_to_frontiers(frontiers)?;
|
||||
let encoded = frontiers.encode();
|
||||
Ok(encoded)
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub fn decodeFrontiers(bytes: &[u8]) -> JsResult<JsIDs> {
|
||||
let frontiers =
|
||||
Frontiers::decode(bytes).map_err(|_| JsError::new("Invalid frontiers binary data"))?;
|
||||
Ok(frontiers_to_ids(&frontiers))
|
||||
}
|
||||
|
||||
/// Enable debug info of Loro
|
||||
#[wasm_bindgen(js_name = setDebug)]
|
||||
pub fn set_debug() {
|
||||
|
|
|
@ -12,6 +12,9 @@ import {
|
|||
VersionVector,
|
||||
MapDiff,
|
||||
TextDiff,
|
||||
Frontiers,
|
||||
encodeFrontiers,
|
||||
decodeFrontiers,
|
||||
} from "../src";
|
||||
|
||||
it("basic example", () => {
|
||||
|
@ -608,3 +611,11 @@ it("can query pending txn length", () => {
|
|||
doc.commit();
|
||||
expect(doc.getPendingTxnLength()).toBe(0);
|
||||
})
|
||||
|
||||
|
||||
it("can encode/decode frontiers", () => {
|
||||
const frontiers = [{ peer: "1123", counter: 1 }, { peer: "222", counter: 2 }] as Frontiers;
|
||||
const encoded = encodeFrontiers(frontiers);
|
||||
const decoded = decodeFrontiers(encoded);
|
||||
expect(decoded).toStrictEqual(frontiers);
|
||||
})
|
||||
|
|
Loading…
Reference in a new issue