Skip to content
This repository was archived by the owner on Aug 30, 2025. It is now read-only.

Commit 0ffa7f3

Browse files
committed
fix: fix memory error when closing entities.
Entities were getting freed before they had finished getting saved.
1 parent 3044b99 commit 0ffa7f3

File tree

3 files changed

+7
-3
lines changed

3 files changed

+7
-3
lines changed

change/@muni-town-leaf-ed823780-b098-440a-ba32-1651b34b201a.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33
"comment": "fix memory error when closing entities.",
44
"packageName": "@muni-town/leaf",
55
"email": "[email protected]",
6-
"dependentChangeType": "patch"
6+
"dependentChangeType": "prerelease"
77
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"url": "https://github.com/muni-town/leaf"
55
},
66
"scripts": {
7+
"change": "npx beachball change",
78
"build-api-docs": "turbo build-api-docs",
89
"syncserver": "PORT=8095 DB_FILE=./data/data.sqlite turbo syncserver",
910
"publish-packages": "turbo build && beachball publish -m 'chore: publish packages.'"

packages/leaf/src/index.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -643,7 +643,6 @@ export class Peer {
643643

644644
/** Commit the entity, stop syncing it, and flush it to storage. */
645645
close(id: IntoEntityId): Promise<void> {
646-
// console.log('close');
647646
return new Promise((resolve) => {
648647
// NOTE: we queue a microtask here because if you have _just_ committed an entity, and then
649648
// you call this function, the change callbacks on the entity have not yet bent run, and the
@@ -660,7 +659,11 @@ export class Peer {
660659
if (entity) {
661660
// This will trigger a write to storage
662661
entity.doc.commit();
663-
entity.free();
662+
// For the same reason mentioned in the nte above, we have to wait until the next tick
663+
// to actually free the entity after the commit.
664+
queueMicrotask(() => {
665+
entity.free();
666+
});
664667
}
665668

666669
this.#rawUnload(entIdStr);

0 commit comments

Comments
 (0)