Skip to content

Commit

Permalink
feat(wasm): add event id
Browse files Browse the repository at this point in the history
  • Loading branch information
zxch3n committed Nov 13, 2023
1 parent 9101b1e commit e54d2ac
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 6 deletions.
4 changes: 4 additions & 0 deletions crates/loro-wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -871,6 +871,7 @@ fn call_subscriber(ob: observer::Observer, e: DiffEvent) {
// [1]: https://caniuse.com/?search=FinalizationRegistry
// [2]: https://rustwasm.github.io/wasm-bindgen/reference/weak-references.html
let event = Event {
id: e.doc.id(),
path: Event::get_path(
e.container.path.len() as u32,
e.container.path.iter().map(|x| &x.1),
Expand All @@ -897,6 +898,7 @@ fn call_after_micro_task(ob: observer::Observer, e: DiffEvent) {
let drop_handler: Rc<RefCell<Option<C>>> = Rc::new(RefCell::new(None));
let copy = drop_handler.clone();
let event = Event {
id: e.doc.id(),
from_children: e.from_children,
from_checkout: e.doc.from_checkout,
local: e.doc.local,
Expand Down Expand Up @@ -931,6 +933,7 @@ impl Default for Loro {
pub struct Event {
pub local: bool,
pub from_children: bool,
id: u64,
origin: String,
target: ContainerID,
from_checkout: bool,
Expand All @@ -948,6 +951,7 @@ impl Event {
Reflect::set(&obj, &"target".into(), &self.target.to_string().into()).unwrap();
Reflect::set(&obj, &"diff".into(), &self.diff.into()).unwrap();
Reflect::set(&obj, &"path".into(), &self.path).unwrap();
Reflect::set(&obj, &"id".into(), &self.id.into()).unwrap();
obj.into()
}

Expand Down
25 changes: 19 additions & 6 deletions loro-js/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
export * from "loro-wasm";
import { Delta } from "loro-wasm";
import { PrelimText,PrelimList,PrelimMap } from "loro-wasm";
import { ContainerID, Loro, LoroList, LoroMap, LoroText , LoroTree, TreeID} from "loro-wasm";
import { PrelimText, PrelimList, PrelimMap } from "loro-wasm";
import {
ContainerID,
Loro,
LoroList,
LoroMap,
LoroText,
LoroTree,
TreeID,
} from "loro-wasm";

Loro.prototype.getTypedMap = function (...args) {
return this.getMap(...args);
Expand Down Expand Up @@ -45,12 +53,11 @@ export type Value =
| Uint8Array
| Value[];


export type Prelim = PrelimList | PrelimMap | PrelimText;

/**
* Represents a path to identify the exact location of an event's target.
* The path is composed of numbers (e.g., indices of a list container) and strings
* The path is composed of numbers (e.g., indices of a list container) and strings
* (e.g., keys of a map container), indicating the absolute position of the event's source
* within a loro document.
*/
Expand All @@ -65,6 +72,10 @@ export type Path = (number | string)[];
* @prop path - Specifies the absolute path of the event's emitter, which can be an index of a list container or a key of a map container.
*/
export interface LoroEvent {
/**
* The unique ID of the event.
*/
id: bigint;
local: boolean;
origin?: string;
/**
Expand Down Expand Up @@ -97,8 +108,10 @@ export type MapDiff = {

export type TreeDiff = {
type: "tree";
diff: {target: TreeID, action: "create"|"delete" } | {target: TreeID; action:"move"; parent: TreeID};
}
diff:
| { target: TreeID; action: "create" | "delete" }
| { target: TreeID; action: "move"; parent: TreeID };
};

export type Diff = ListDiff | TextDiff | MapDiff | TreeDiff;

Expand Down
28 changes: 28 additions & 0 deletions loro-js/tests/event.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,34 @@ describe("event", () => {
expect(lastEvent?.target).toEqual(id);
});

it("event id", async () => {
const loro = new Loro();
let lastEventId: bigint = 0n;
let uniqueEvent = 0;
loro.subscribe((event) => {
console.log(event.id);
if (event.id === lastEventId) {
return;
}

lastEventId = event.id;
uniqueEvent += 1;
});

const map = loro.getMap("map");
map.set("0", 100);
loro.commit();
expect(uniqueEvent).toBe(1);
const bDoc = new Loro();
bDoc.getText("text").insert(0, "1");
bDoc.getText("text").insert(1, "1");
bDoc.commit();
bDoc.getMap("map").set("0", "1");
bDoc.commit();
loro.import(bDoc.exportFrom());
expect(uniqueEvent).toBe(2);
});

it("path", async () => {
const loro = new Loro();
let lastEvent: undefined | LoroEvent;
Expand Down

0 comments on commit e54d2ac

Please sign in to comment.