mirror of
https://github.com/loro-dev/loro.git
synced 2025-01-22 21:07:43 +00:00
fix: add event diff test & fix related bugs
This commit is contained in:
parent
49f664dd8f
commit
0a421d3931
3 changed files with 131 additions and 16 deletions
|
@ -501,7 +501,7 @@ pub mod wasm {
|
|||
let added = Object::new();
|
||||
for (key, value) in value.added.iter() {
|
||||
js_sys::Reflect::set(
|
||||
&obj,
|
||||
&added,
|
||||
&JsValue::from_str(key),
|
||||
&JsValue::from(value.clone()),
|
||||
)
|
||||
|
@ -515,7 +515,7 @@ pub mod wasm {
|
|||
let deleted = Object::new();
|
||||
for (key, value) in value.deleted.iter() {
|
||||
js_sys::Reflect::set(
|
||||
&obj,
|
||||
&deleted,
|
||||
&JsValue::from_str(key),
|
||||
&JsValue::from(value.clone()),
|
||||
)
|
||||
|
@ -529,12 +529,20 @@ pub mod wasm {
|
|||
let updated = Object::new();
|
||||
for (key, pair) in value.updated.iter() {
|
||||
let pair_obj = Object::new();
|
||||
js_sys::Reflect::set(&obj, &JsValue::from_str("old"), &pair.old.clone().into())
|
||||
.unwrap();
|
||||
js_sys::Reflect::set(&obj, &JsValue::from_str("new"), &pair.new.clone().into())
|
||||
.unwrap();
|
||||
js_sys::Reflect::set(
|
||||
&obj,
|
||||
&pair_obj,
|
||||
&JsValue::from_str("old"),
|
||||
&pair.old.clone().into(),
|
||||
)
|
||||
.unwrap();
|
||||
js_sys::Reflect::set(
|
||||
&pair_obj,
|
||||
&JsValue::from_str("new"),
|
||||
&pair.new.clone().into(),
|
||||
)
|
||||
.unwrap();
|
||||
js_sys::Reflect::set(
|
||||
&updated,
|
||||
&JsValue::from_str(key),
|
||||
&pair_obj.into_js_result().unwrap(),
|
||||
)
|
||||
|
|
|
@ -50,25 +50,34 @@ export type Delta<T> = {
|
|||
len: number;
|
||||
};
|
||||
|
||||
export type Diff = {
|
||||
export type ListDiff = {
|
||||
type: "list";
|
||||
diff: Delta<Value[]>;
|
||||
} | {
|
||||
type: "list";
|
||||
diff: Delta<string>;
|
||||
} | {
|
||||
diff: Delta<Value[]>[];
|
||||
};
|
||||
|
||||
export type TextDiff = {
|
||||
type: "text";
|
||||
diff: Delta<string>[];
|
||||
};
|
||||
|
||||
export type MapDIff = {
|
||||
type: "map";
|
||||
diff: {
|
||||
added: Record<string, Value>;
|
||||
deleted: Record<string, Value>;
|
||||
updated: Record<string, { old: Value; new: Value }>;
|
||||
updated: Record<string, {
|
||||
old: Value;
|
||||
new: Value;
|
||||
}>;
|
||||
};
|
||||
};
|
||||
|
||||
export type Diff = ListDiff | TextDiff | MapDIff;
|
||||
|
||||
export interface LoroEvent {
|
||||
local: boolean;
|
||||
origin?: string;
|
||||
diff: Diff;
|
||||
diff: Diff[];
|
||||
target: ContainerID;
|
||||
path: Path;
|
||||
}
|
||||
|
|
|
@ -1,5 +1,13 @@
|
|||
import { describe, expect, it } from "vitest";
|
||||
import { Loro, LoroEvent, LoroMap } from "../src";
|
||||
import {
|
||||
Diff,
|
||||
ListDiff,
|
||||
Loro,
|
||||
LoroEvent,
|
||||
LoroMap,
|
||||
MapDIff as MapDiff,
|
||||
TextDiff,
|
||||
} from "../src";
|
||||
|
||||
describe("event", () => {
|
||||
it("target", async () => {
|
||||
|
@ -29,10 +37,100 @@ describe("event", () => {
|
|||
const list = subMap.insertContainer(loro, "list", "List");
|
||||
list.insert(loro, 0, "2");
|
||||
const text = list.insertContainer(loro, 1, "Text");
|
||||
await zeroMs();
|
||||
text.insert(loro, 0, "3");
|
||||
await zeroMs();
|
||||
expect(lastEvent?.path).toStrictEqual(["map", "sub", "list", 1]);
|
||||
});
|
||||
|
||||
it("text diff", async () => {
|
||||
const loro = new Loro();
|
||||
let lastEvent: undefined | LoroEvent;
|
||||
loro.subscribe((event) => {
|
||||
lastEvent = event;
|
||||
});
|
||||
const text = loro.getText("t");
|
||||
text.insert(loro, 0, "3");
|
||||
await zeroMs();
|
||||
expect(lastEvent?.diff).toStrictEqual(
|
||||
[{ type: "text", diff: [{ type: "insert", value: "3" }] } as TextDiff],
|
||||
);
|
||||
text.insert(loro, 1, "12");
|
||||
await zeroMs();
|
||||
expect(lastEvent?.diff).toStrictEqual(
|
||||
[{
|
||||
type: "text",
|
||||
diff: [{ type: "retain", len: 1 }, { type: "insert", value: "12" }],
|
||||
} as TextDiff],
|
||||
);
|
||||
});
|
||||
|
||||
it("list diff", async () => {
|
||||
const loro = new Loro();
|
||||
let lastEvent: undefined | LoroEvent;
|
||||
loro.subscribe((event) => {
|
||||
lastEvent = event;
|
||||
});
|
||||
const text = loro.getList("l");
|
||||
text.insert(loro, 0, "3");
|
||||
await zeroMs();
|
||||
expect(lastEvent?.diff).toStrictEqual(
|
||||
[{ type: "list", diff: [{ type: "insert", value: ["3"] }] } as ListDiff],
|
||||
);
|
||||
text.insert(loro, 1, "12");
|
||||
await zeroMs();
|
||||
expect(lastEvent?.diff).toStrictEqual(
|
||||
[{
|
||||
type: "list",
|
||||
diff: [{ type: "retain", len: 1 }, { type: "insert", value: ["12"] }],
|
||||
} as ListDiff],
|
||||
);
|
||||
});
|
||||
|
||||
it("map diff", async () => {
|
||||
const loro = new Loro();
|
||||
let lastEvent: undefined | LoroEvent;
|
||||
loro.subscribe((event) => {
|
||||
lastEvent = event;
|
||||
});
|
||||
const map = loro.getMap("m");
|
||||
loro.transact((tx) => {
|
||||
map.set(tx, "0", "3");
|
||||
map.set(tx, "1", "2");
|
||||
});
|
||||
await zeroMs();
|
||||
expect(lastEvent?.diff).toStrictEqual(
|
||||
[{
|
||||
type: "map",
|
||||
diff: {
|
||||
added: {
|
||||
"0": "3",
|
||||
"1": "2",
|
||||
},
|
||||
deleted: {},
|
||||
updated: {},
|
||||
},
|
||||
} as MapDiff],
|
||||
);
|
||||
loro.transact((tx) => {
|
||||
map.set(tx, "0", "0");
|
||||
map.set(tx, "1", "1");
|
||||
});
|
||||
await zeroMs();
|
||||
expect(lastEvent?.diff).toStrictEqual(
|
||||
[{
|
||||
type: "map",
|
||||
diff: {
|
||||
added: {},
|
||||
updated: {
|
||||
"0": { old: "3", new: "0" },
|
||||
"1": { old: "2", new: "1" },
|
||||
},
|
||||
deleted: {},
|
||||
},
|
||||
} as MapDiff],
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
function zeroMs(): Promise<void> {
|
||||
|
|
Loading…
Reference in a new issue