Skip to content

Commit 098a01d

Browse files
committed
fix: starting from v1.8 events are emitted synchronously
1 parent dad6c5d commit 098a01d

File tree

2 files changed

+10
-16
lines changed

2 files changed

+10
-16
lines changed

pages/docs/api/js.mdx

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,7 @@ Note: Under the hood, Loro combines a Fugue-based CRDT core with Eg-walker-inspi
3737
- Initialize all child containers for a LoroMap upfront when possible
3838
- Operations on the root containers will not override each other
3939

40-
**Events & Transactions**
41-
42-
- Events emit asynchronously after a microtask in JS API
40+
- Events emit synchronously during commit/import/checkout in JS API (v1.8+). Stay on <=1.7.x? Await a microtask before reading batched events.
4341
- Import/export/checkout trigger automatic commits
4442
- Loro transactions are NOT ACID - no rollback/isolation
4543

@@ -799,7 +797,7 @@ const forkedDoc = doc.forkAt(frontiers);
799797

800798
## Events & Transactions
801799

802-
React to changes and group local operations into transactions. Events are delivered asynchronously after a microtask. See [Event Handling](/docs/tutorial/event) and [Transaction Model](/docs/concepts/transaction_model).
800+
React to changes and group local operations into transactions. Starting in v1.8, events are delivered synchronously; older releases require awaiting a microtask. See [Event Handling](/docs/tutorial/event) and [Transaction Model](/docs/concepts/transaction_model).
803801

804802
### Subscription Methods
805803

@@ -844,11 +842,11 @@ const unsubscribe = doc.subscribe((event) => {
844842
// Later: unsubscribe();
845843
```
846844

847-
**⚠️ Important:** Events are emitted asynchronously after a microtask.
845+
**⚠️ Important:** Events are emitted synchronously as of v1.8. If you are pinned to <=1.7.x, await a microtask before reading the batch.
848846
```ts no_run
849847
doc.commit();
850-
await Promise.resolve();
851-
// Now events have been emitted
848+
// Events have already been delivered in v1.8+
849+
// await Promise.resolve(); // Only needed on <=1.7.x
852850
```
853851

854852
**📝 Note:** Multiple operations before a commit are batched into a single event. See [Event Handling](/docs/tutorial/event).

pages/docs/tutorial/event.mdx

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ with changes in the document state.
1919
- When `LoroDoc.commit()` is explicitly called
2020
- Automatically before an import or export operation
2121

22-
Note that events are emitted asynchronously after a microtask. If you need to handle events immediately after a commit, you should await a microtask:
22+
Starting from `[email protected]`, events are emitted synchronously during the commit cycle. If you are using an older version (<=1.7.x), you will still need to await a microtask for the callbacks to fire.
2323

2424
```ts no_run twoslash
2525
import { LoroDoc } from "loro-crdt";
@@ -32,9 +32,7 @@ doc.subscribe((event) => {
3232
const text = doc.getText("text");
3333
text.insert(0, "Hello");
3434
doc.commit();
35-
// Event hasn't been emitted yet
36-
await Promise.resolve();
37-
// Now the event has been emitted
35+
// Event has already been emitted synchronously
3836
```
3937

4038
3. **Import**: When importing changes from a remote source using the `import()`
@@ -50,21 +48,19 @@ doc.subscribe((event) => {
5048
console.log("Event:", event);
5149
});
5250

53-
doc.import(remoteChanges); // This will trigger events after a microtask
54-
await Promise.resolve(); // Wait for events to be emitted
51+
doc.import(remoteChanges); // This immediately triggers events (v1.8+)
5552
```
5653

5754
4. **Version Checkout**: When you switch document state to a different version
5855
using `doc.checkout(frontiers)`, Loro emits an event to reflect this change.
59-
Like other events, these are also emitted after a microtask.
56+
As of v1.8, checkout events fire synchronously alongside the state change.
6057

6158
```ts no_run twoslash
6259
import { LoroDoc } from "loro-crdt";
6360
// ---cut---
6461
const doc = new LoroDoc();
6562
const frontiers = doc.frontiers();
66-
doc.checkout(frontiers); // This will trigger events after a microtask
67-
await Promise.resolve(); // Wait for events to be emitted
63+
doc.checkout(frontiers); // This triggers events immediately (v1.8+)
6864
```
6965

7066
## Transaction Behavior

0 commit comments

Comments
 (0)