Skip to content

Commit 8957b98

Browse files
committed
refactor: emit event after a micro task
1 parent 40d6de3 commit 8957b98

3 files changed

Lines changed: 42 additions & 3 deletions

File tree

crates/loro-wasm/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -674,8 +674,8 @@ impl Loro {
674674
let observer = observer::Observer::new(f);
675675
self.0
676676
.subscribe_root(Arc::new(move |e| {
677-
// call_after_micro_task(observer.clone(), e)
678-
call_subscriber(observer.clone(), e);
677+
call_after_micro_task(observer.clone(), e)
678+
// call_subscriber(observer.clone(), e);
679679
}))
680680
.into_u32()
681681
}

loro-js/tests/event.test.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ describe("event", () => {
2121
const id = text.id;
2222
text.insert(0, "123");
2323
loro.commit();
24+
await oneMs();
2425
expect(lastEvent?.target).toEqual(id);
2526
});
2627

@@ -41,14 +42,18 @@ describe("event", () => {
4142
const map = loro.getMap("map");
4243
map.set("0", 100);
4344
loro.commit();
45+
await oneMs();
4446
expect(uniqueEvent).toBe(1);
4547
const bDoc = new Loro();
4648
bDoc.getText("text").insert(0, "1");
4749
bDoc.getText("text").insert(1, "1");
4850
bDoc.commit();
51+
await oneMs();
4952
bDoc.getMap("map").set("0", "1");
5053
bDoc.commit();
54+
await oneMs();
5155
loro.import(bDoc.exportFrom());
56+
await oneMs();
5257
expect(uniqueEvent).toBe(2);
5358
});
5459

@@ -62,14 +67,17 @@ describe("event", () => {
6267
const subMap = map.setContainer("sub", "Map");
6368
subMap.set("0", "1");
6469
loro.commit();
70+
await oneMs();
6571

6672
expect(lastEvent?.path).toStrictEqual(["map", "sub"]);
6773
const list = subMap.setContainer("list", "List");
6874
list.insert(0, "2");
6975
const text = list.insertContainer(1, "Text");
7076
loro.commit();
77+
await oneMs();
7178
text.insert(0, "3");
7279
loro.commit();
80+
await oneMs();
7381
expect(lastEvent?.path).toStrictEqual(["map", "sub", "list", 1]);
7482
});
7583

@@ -82,12 +90,14 @@ describe("event", () => {
8290
const text = loro.getText("t");
8391
text.insert(0, "3");
8492
loro.commit();
93+
await oneMs();
8594
expect(lastEvent?.diff).toStrictEqual({
8695
type: "text",
8796
diff: [{ insert: "3" }],
8897
} as TextDiff);
8998
text.insert(1, "12");
9099
loro.commit();
100+
await oneMs();
91101
expect(lastEvent?.diff).toStrictEqual({
92102
type: "text",
93103
diff: [{ retain: 1 }, { insert: "12" }],
@@ -103,12 +113,14 @@ describe("event", () => {
103113
const text = loro.getList("l");
104114
text.insert(0, "3");
105115
loro.commit();
116+
await oneMs();
106117
expect(lastEvent?.diff).toStrictEqual({
107118
type: "list",
108119
diff: [{ insert: ["3"] }],
109120
} as ListDiff);
110121
text.insert(1, "12");
111122
loro.commit();
123+
await oneMs();
112124
expect(lastEvent?.diff).toStrictEqual({
113125
type: "list",
114126
diff: [{ retain: 1 }, { insert: ["12"] }],
@@ -125,6 +137,7 @@ describe("event", () => {
125137
map.set("0", "3");
126138
map.set("1", "2");
127139
loro.commit();
140+
await oneMs();
128141
expect(lastEvent?.diff).toStrictEqual({
129142
type: "map",
130143
updated: {
@@ -135,6 +148,7 @@ describe("event", () => {
135148
map.set("0", "0");
136149
map.set("1", "1");
137150
loro.commit();
151+
await oneMs();
138152
expect(lastEvent?.diff).toStrictEqual({
139153
type: "map",
140154
updated: {
@@ -161,8 +175,10 @@ describe("event", () => {
161175

162176
text.insert(0, "123");
163177
loro.commit();
178+
await oneMs();
164179
text.insert(1, "456");
165180
loro.commit();
181+
await oneMs();
166182
expect(ran).toBeTruthy();
167183
// subscribeOnce test
168184
expect(text.toString()).toEqual("145623");
@@ -172,6 +188,7 @@ describe("event", () => {
172188
text.unsubscribe(loro, sub);
173189
text.insert(0, "789");
174190
loro.commit();
191+
await oneMs();
175192
expect(ran).toBe(oldRan);
176193
});
177194

@@ -185,18 +202,22 @@ describe("event", () => {
185202

186203
const subMap = map.setContainer("sub", "Map");
187204
loro.commit();
205+
await oneMs();
188206
expect(times).toBe(1);
189207
const text = subMap.setContainer("k", "Text");
190208
loro.commit();
209+
await oneMs();
191210
expect(times).toBe(2);
192211
text.insert(0, "123");
193212
loro.commit();
213+
await oneMs();
194214
expect(times).toBe(3);
195215

196216
// unsubscribe
197217
loro.unsubscribe(sub);
198218
text.insert(0, "123");
199219
loro.commit();
220+
await oneMs();
200221
expect(times).toBe(3);
201222
});
202223

@@ -210,15 +231,18 @@ describe("event", () => {
210231

211232
const text = list.insertContainer(0, "Text");
212233
loro.commit();
234+
await oneMs();
213235
expect(times).toBe(1);
214236
text.insert(0, "123");
237+
await oneMs();
215238
loro.commit();
216239
expect(times).toBe(2);
217240

218241
// unsubscribe
219242
loro.unsubscribe(sub);
220243
text.insert(0, "123");
221244
loro.commit();
245+
await oneMs();
222246
expect(times).toBe(2);
223247
});
224248
});
@@ -250,23 +274,27 @@ describe("event", () => {
250274
});
251275
text.insert(0, "你好");
252276
loro.commit();
277+
await oneMs();
253278
expect(text.toString()).toBe(string);
254279

255280
text.insert(1, "世界");
256281
loro.commit();
282+
await oneMs();
257283
expect(text.toString()).toBe(string);
258284

259285
text.insert(2, "👍");
260286
loro.commit();
287+
await oneMs();
261288
expect(text.toString()).toBe(string);
262289

263290
text.insert(2, "♪(^∇^*)");
264291
loro.commit();
292+
await oneMs();
265293
expect(text.toString()).toBe(string);
266294
});
267295
});
268296
});
269297

270-
function zeroMs(): Promise<void> {
298+
function oneMs(): Promise<void> {
271299
return new Promise((r) => setTimeout(r));
272300
}

loro-js/tests/misc.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ describe("transaction", () => {
3131
text.insert(0, "hello world");
3232
assertEquals(count, 0);
3333
loro.commit();
34+
await one_ms();
3435
assertEquals(count, 1);
3536
});
3637

@@ -50,6 +51,7 @@ describe("transaction", () => {
5051
text.insert(0, "hello world");
5152
assertEquals(count, 0);
5253
loro.commit("origin");
54+
await one_ms();
5355
assertEquals(count, 1);
5456
});
5557
});
@@ -73,14 +75,17 @@ describe("subscribe", () => {
7375

7476
text.insert(0, "hello world");
7577
loro.commit();
78+
await one_ms();
7679

7780
assertEquals(count, 2);
7881
text.insert(0, "hello world");
7982
loro.commit();
83+
await one_ms();
8084
assertEquals(count, 3);
8185
loro.unsubscribe(sub);
8286
text.insert(0, "hello world");
8387
loro.commit();
88+
await one_ms();
8489
assertEquals(count, 3);
8590
});
8691

@@ -95,10 +100,12 @@ describe("subscribe", () => {
95100
assertEquals(count, 0);
96101
text.insert(0, "hello world");
97102
loro.commit();
103+
await one_ms();
98104

99105
assertEquals(count, 1);
100106
text.insert(0, "hello world");
101107
loro.commit();
108+
await one_ms();
102109

103110
assertEquals(count, 1);
104111
});
@@ -112,13 +119,16 @@ describe("subscribe", () => {
112119
});
113120
text.insert(0, "hello world");
114121
loro.commit();
122+
await one_ms();
115123
assertEquals(count, 1);
116124
text.insert(0, "hello world");
117125
loro.commit();
126+
await one_ms();
118127
assertEquals(count, 2);
119128
loro.unsubscribe(sub);
120129
text.insert(0, "hello world");
121130
loro.commit();
131+
await one_ms();
122132
assertEquals(count, 2);
123133
});
124134
});
@@ -147,6 +157,7 @@ describe("sync", () => {
147157
const bText = b.getText("text");
148158
aText.insert(0, "abc");
149159
a.commit();
160+
await one_ms();
150161

151162
assertEquals(aText.toString(), bText.toString());
152163
});

0 commit comments

Comments
 (0)