Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: LoroCounter issues #626 #627

Merged
merged 1 commit into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions crates/loro-wasm/src/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ pub(crate) fn js_to_container(js: JsContainer) -> Result<Container, JsValue> {
let obj = unsafe { LoroMovableList::ref_from_abi(ptr_u32) };
Container::MovableList(obj.clone())
}
"Counter" => {
let obj = unsafe { LoroCounter::ref_from_abi(ptr_u32) };
Container::Counter(obj.clone())
}
_ => {
return Err(JsValue::from_str(
format!(
Expand Down
18 changes: 15 additions & 3 deletions crates/loro-wasm/src/counter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ use std::sync::Arc;
use wasm_bindgen::prelude::*;

use crate::{
call_after_micro_task, convert::handler_to_js_value, observer, JsContainerOrUndefined,
JsLoroTreeOrUndefined, JsResult,
call_after_micro_task, convert::handler_to_js_value, observer, JsContainerID,
JsContainerOrUndefined, JsCounterStr, JsLoroTreeOrUndefined, JsResult,
};

/// The handler of a tree(forest) container.
/// The handler of a counter container.
#[derive(Clone)]
#[wasm_bindgen]
pub struct LoroCounter {
Expand All @@ -36,6 +36,18 @@ impl LoroCounter {
}
}

/// "Counter"
pub fn kind(&self) -> JsCounterStr {
JsValue::from_str("Counter").into()
}

/// The container id of this handler.
#[wasm_bindgen(js_name = "id", method, getter)]
pub fn id(&self) -> JsContainerID {
let value: JsValue = (&self.handler.id()).into();
value.into()
}

/// Increment the counter by the given value.
pub fn increment(&self, value: f64) -> JsResult<()> {
self.handler.increment(value)?;
Expand Down
8 changes: 6 additions & 2 deletions crates/loro-wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,8 @@ extern "C" {
pub type JsListStr;
#[wasm_bindgen(typescript_type = "'MovableList'")]
pub type JsMovableListStr;
#[wasm_bindgen(typescript_type = "'Counter'")]
pub type JsCounterStr;
#[wasm_bindgen(typescript_type = "ImportBlobMetadata")]
pub type JsImportBlobMetadata;
#[wasm_bindgen(typescript_type = "Side")]
Expand Down Expand Up @@ -4930,6 +4932,7 @@ enum Container {
List(LoroList),
Tree(LoroTree),
MovableList(LoroMovableList),
Counter(LoroCounter),
}

impl Container {
Expand All @@ -4940,6 +4943,7 @@ impl Container {
Container::List(l) => Handler::List(l.handler.clone()),
Container::Tree(t) => Handler::Tree(t.handler.clone()),
Container::MovableList(l) => Handler::MovableList(l.handler.clone()),
Container::Counter(c) => Handler::Counter(c.handler.clone()),
}
}
}
Expand Down Expand Up @@ -5048,7 +5052,7 @@ const TYPES: &'static str = r#"
* const text = list.insertContainer(1, new LoroText());
* ```
*/
export type ContainerType = "Text" | "Map" | "List"| "Tree" | "MovableList";
export type ContainerType = "Text" | "Map" | "List"| "Tree" | "MovableList" | "Counter";

export type PeerID = `${number}`;
/**
Expand Down Expand Up @@ -5292,7 +5296,7 @@ export type UndoConfig = {
onPush?: (isUndo: boolean, counterRange: { start: number, end: number }, event?: LoroEventBatch) => { value: Value, cursors: Cursor[] },
onPop?: (isUndo: boolean, value: { value: Value, cursors: Cursor[] }, counterRange: { start: number, end: number }) => void
};
export type Container = LoroList | LoroMap | LoroText | LoroTree | LoroMovableList;
export type Container = LoroList | LoroMap | LoroText | LoroTree | LoroMovableList | LoroCounter;

export interface ImportBlobMetadata {
/**
Expand Down
25 changes: 24 additions & 1 deletion crates/loro-wasm/tests/basic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ import {
encodeFrontiers,
decodeFrontiers,
OpId,
ContainerID,
LoroCounter
} from "../bundler/index";
import { ContainerID } from "loro-wasm";

it("basic example", () => {
const doc = new LoroDoc();
Expand Down Expand Up @@ -1293,3 +1294,25 @@ it("setRecordTimestamp should be reflected on current txn", async () => {
const updates = doc.exportJsonUpdates();
expect(updates.changes[1].timestamp).toBeGreaterThan(0);
})

it("insert counter container", () => {
function createItem(label: string, checked: boolean) {
const item = new LoroMap<Record<string, Container>>();

const $label = new LoroText();
$label.insert(0, label);

const $checked = new LoroCounter();
if (checked) $checked.increment(1);

item.setContainer("label", $label);
item.setContainer("checked", $checked);

return item;
}

const item = createItem("hello", true);

console.log(item.get("label").toString());
console.log((item.get("checked") as LoroCounter).value);
})