mirror of
https://github.com/loro-dev/loro.git
synced 2025-01-22 21:07:43 +00:00
refactor: emit event after a micro task
This commit is contained in:
parent
40d6de3146
commit
8957b98236
3 changed files with 42 additions and 3 deletions
|
@ -674,8 +674,8 @@ impl Loro {
|
|||
let observer = observer::Observer::new(f);
|
||||
self.0
|
||||
.subscribe_root(Arc::new(move |e| {
|
||||
// call_after_micro_task(observer.clone(), e)
|
||||
call_subscriber(observer.clone(), e);
|
||||
call_after_micro_task(observer.clone(), e)
|
||||
// call_subscriber(observer.clone(), e);
|
||||
}))
|
||||
.into_u32()
|
||||
}
|
||||
|
|
|
@ -21,6 +21,7 @@ describe("event", () => {
|
|||
const id = text.id;
|
||||
text.insert(0, "123");
|
||||
loro.commit();
|
||||
await oneMs();
|
||||
expect(lastEvent?.target).toEqual(id);
|
||||
});
|
||||
|
||||
|
@ -41,14 +42,18 @@ describe("event", () => {
|
|||
const map = loro.getMap("map");
|
||||
map.set("0", 100);
|
||||
loro.commit();
|
||||
await oneMs();
|
||||
expect(uniqueEvent).toBe(1);
|
||||
const bDoc = new Loro();
|
||||
bDoc.getText("text").insert(0, "1");
|
||||
bDoc.getText("text").insert(1, "1");
|
||||
bDoc.commit();
|
||||
await oneMs();
|
||||
bDoc.getMap("map").set("0", "1");
|
||||
bDoc.commit();
|
||||
await oneMs();
|
||||
loro.import(bDoc.exportFrom());
|
||||
await oneMs();
|
||||
expect(uniqueEvent).toBe(2);
|
||||
});
|
||||
|
||||
|
@ -62,14 +67,17 @@ describe("event", () => {
|
|||
const subMap = map.setContainer("sub", "Map");
|
||||
subMap.set("0", "1");
|
||||
loro.commit();
|
||||
await oneMs();
|
||||
|
||||
expect(lastEvent?.path).toStrictEqual(["map", "sub"]);
|
||||
const list = subMap.setContainer("list", "List");
|
||||
list.insert(0, "2");
|
||||
const text = list.insertContainer(1, "Text");
|
||||
loro.commit();
|
||||
await oneMs();
|
||||
text.insert(0, "3");
|
||||
loro.commit();
|
||||
await oneMs();
|
||||
expect(lastEvent?.path).toStrictEqual(["map", "sub", "list", 1]);
|
||||
});
|
||||
|
||||
|
@ -82,12 +90,14 @@ describe("event", () => {
|
|||
const text = loro.getText("t");
|
||||
text.insert(0, "3");
|
||||
loro.commit();
|
||||
await oneMs();
|
||||
expect(lastEvent?.diff).toStrictEqual({
|
||||
type: "text",
|
||||
diff: [{ insert: "3" }],
|
||||
} as TextDiff);
|
||||
text.insert(1, "12");
|
||||
loro.commit();
|
||||
await oneMs();
|
||||
expect(lastEvent?.diff).toStrictEqual({
|
||||
type: "text",
|
||||
diff: [{ retain: 1 }, { insert: "12" }],
|
||||
|
@ -103,12 +113,14 @@ describe("event", () => {
|
|||
const text = loro.getList("l");
|
||||
text.insert(0, "3");
|
||||
loro.commit();
|
||||
await oneMs();
|
||||
expect(lastEvent?.diff).toStrictEqual({
|
||||
type: "list",
|
||||
diff: [{ insert: ["3"] }],
|
||||
} as ListDiff);
|
||||
text.insert(1, "12");
|
||||
loro.commit();
|
||||
await oneMs();
|
||||
expect(lastEvent?.diff).toStrictEqual({
|
||||
type: "list",
|
||||
diff: [{ retain: 1 }, { insert: ["12"] }],
|
||||
|
@ -125,6 +137,7 @@ describe("event", () => {
|
|||
map.set("0", "3");
|
||||
map.set("1", "2");
|
||||
loro.commit();
|
||||
await oneMs();
|
||||
expect(lastEvent?.diff).toStrictEqual({
|
||||
type: "map",
|
||||
updated: {
|
||||
|
@ -135,6 +148,7 @@ describe("event", () => {
|
|||
map.set("0", "0");
|
||||
map.set("1", "1");
|
||||
loro.commit();
|
||||
await oneMs();
|
||||
expect(lastEvent?.diff).toStrictEqual({
|
||||
type: "map",
|
||||
updated: {
|
||||
|
@ -161,8 +175,10 @@ describe("event", () => {
|
|||
|
||||
text.insert(0, "123");
|
||||
loro.commit();
|
||||
await oneMs();
|
||||
text.insert(1, "456");
|
||||
loro.commit();
|
||||
await oneMs();
|
||||
expect(ran).toBeTruthy();
|
||||
// subscribeOnce test
|
||||
expect(text.toString()).toEqual("145623");
|
||||
|
@ -172,6 +188,7 @@ describe("event", () => {
|
|||
text.unsubscribe(loro, sub);
|
||||
text.insert(0, "789");
|
||||
loro.commit();
|
||||
await oneMs();
|
||||
expect(ran).toBe(oldRan);
|
||||
});
|
||||
|
||||
|
@ -185,18 +202,22 @@ describe("event", () => {
|
|||
|
||||
const subMap = map.setContainer("sub", "Map");
|
||||
loro.commit();
|
||||
await oneMs();
|
||||
expect(times).toBe(1);
|
||||
const text = subMap.setContainer("k", "Text");
|
||||
loro.commit();
|
||||
await oneMs();
|
||||
expect(times).toBe(2);
|
||||
text.insert(0, "123");
|
||||
loro.commit();
|
||||
await oneMs();
|
||||
expect(times).toBe(3);
|
||||
|
||||
// unsubscribe
|
||||
loro.unsubscribe(sub);
|
||||
text.insert(0, "123");
|
||||
loro.commit();
|
||||
await oneMs();
|
||||
expect(times).toBe(3);
|
||||
});
|
||||
|
||||
|
@ -210,8 +231,10 @@ describe("event", () => {
|
|||
|
||||
const text = list.insertContainer(0, "Text");
|
||||
loro.commit();
|
||||
await oneMs();
|
||||
expect(times).toBe(1);
|
||||
text.insert(0, "123");
|
||||
await oneMs();
|
||||
loro.commit();
|
||||
expect(times).toBe(2);
|
||||
|
||||
|
@ -219,6 +242,7 @@ describe("event", () => {
|
|||
loro.unsubscribe(sub);
|
||||
text.insert(0, "123");
|
||||
loro.commit();
|
||||
await oneMs();
|
||||
expect(times).toBe(2);
|
||||
});
|
||||
});
|
||||
|
@ -250,23 +274,27 @@ describe("event", () => {
|
|||
});
|
||||
text.insert(0, "你好");
|
||||
loro.commit();
|
||||
await oneMs();
|
||||
expect(text.toString()).toBe(string);
|
||||
|
||||
text.insert(1, "世界");
|
||||
loro.commit();
|
||||
await oneMs();
|
||||
expect(text.toString()).toBe(string);
|
||||
|
||||
text.insert(2, "👍");
|
||||
loro.commit();
|
||||
await oneMs();
|
||||
expect(text.toString()).toBe(string);
|
||||
|
||||
text.insert(2, "♪(^∇^*)");
|
||||
loro.commit();
|
||||
await oneMs();
|
||||
expect(text.toString()).toBe(string);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
function zeroMs(): Promise<void> {
|
||||
function oneMs(): Promise<void> {
|
||||
return new Promise((r) => setTimeout(r));
|
||||
}
|
||||
|
|
|
@ -31,6 +31,7 @@ describe("transaction", () => {
|
|||
text.insert(0, "hello world");
|
||||
assertEquals(count, 0);
|
||||
loro.commit();
|
||||
await one_ms();
|
||||
assertEquals(count, 1);
|
||||
});
|
||||
|
||||
|
@ -50,6 +51,7 @@ describe("transaction", () => {
|
|||
text.insert(0, "hello world");
|
||||
assertEquals(count, 0);
|
||||
loro.commit("origin");
|
||||
await one_ms();
|
||||
assertEquals(count, 1);
|
||||
});
|
||||
});
|
||||
|
@ -73,14 +75,17 @@ describe("subscribe", () => {
|
|||
|
||||
text.insert(0, "hello world");
|
||||
loro.commit();
|
||||
await one_ms();
|
||||
|
||||
assertEquals(count, 2);
|
||||
text.insert(0, "hello world");
|
||||
loro.commit();
|
||||
await one_ms();
|
||||
assertEquals(count, 3);
|
||||
loro.unsubscribe(sub);
|
||||
text.insert(0, "hello world");
|
||||
loro.commit();
|
||||
await one_ms();
|
||||
assertEquals(count, 3);
|
||||
});
|
||||
|
||||
|
@ -95,10 +100,12 @@ describe("subscribe", () => {
|
|||
assertEquals(count, 0);
|
||||
text.insert(0, "hello world");
|
||||
loro.commit();
|
||||
await one_ms();
|
||||
|
||||
assertEquals(count, 1);
|
||||
text.insert(0, "hello world");
|
||||
loro.commit();
|
||||
await one_ms();
|
||||
|
||||
assertEquals(count, 1);
|
||||
});
|
||||
|
@ -112,13 +119,16 @@ describe("subscribe", () => {
|
|||
});
|
||||
text.insert(0, "hello world");
|
||||
loro.commit();
|
||||
await one_ms();
|
||||
assertEquals(count, 1);
|
||||
text.insert(0, "hello world");
|
||||
loro.commit();
|
||||
await one_ms();
|
||||
assertEquals(count, 2);
|
||||
loro.unsubscribe(sub);
|
||||
text.insert(0, "hello world");
|
||||
loro.commit();
|
||||
await one_ms();
|
||||
assertEquals(count, 2);
|
||||
});
|
||||
});
|
||||
|
@ -147,6 +157,7 @@ describe("sync", () => {
|
|||
const bText = b.getText("text");
|
||||
aText.insert(0, "abc");
|
||||
a.commit();
|
||||
await one_ms();
|
||||
|
||||
assertEquals(aText.toString(), bText.toString());
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue