summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorDax Raad <[email protected]>2025-05-26 22:56:58 -0400
committerDax Raad <[email protected]>2025-05-26 22:56:58 -0400
commit6ef0b991ecf8031ada69db02ad5be4b585f66bea (patch)
treedc7659a0024093057aebb2de995e8fdcad7302eb
parentf6ca06b8eae6d88392356e9d03aaa478135c85b9 (diff)
downloadopencode-6ef0b991ecf8031ada69db02ad5be4b585f66bea.tar.gz
opencode-6ef0b991ecf8031ada69db02ad5be4b585f66bea.zip
Fix race condition in share sync by implementing request queue
🤖 Generated with opencode Co-Authored-By: opencode <[email protected]>
-rw-r--r--js/src/share/share.ts35
1 files changed, 21 insertions, 14 deletions
diff --git a/js/src/share/share.ts b/js/src/share/share.ts
index daf6d67a5..a9b5ed8fd 100644
--- a/js/src/share/share.ts
+++ b/js/src/share/share.ts
@@ -7,27 +7,34 @@ import { Log } from "../util/log";
export namespace Share {
const log = Log.create({ service: "share" });
+ let queue: Promise<void> = Promise.resolve();
+
const state = App.state("share", async () => {
Bus.subscribe(Storage.Event.Write, async (payload) => {
const [root, ...splits] = payload.properties.key.split("/");
if (root !== "session") return;
- const [type, sessionID] = splits;
+ const [, sessionID] = splits;
const session = await Session.get(sessionID);
if (!session.shareID) return;
- await fetch(`${URL}/share_sync`, {
- method: "POST",
- body: JSON.stringify({
- sessionID: sessionID,
- shareID: session.shareID,
- key: payload.properties.key,
- content: JSON.stringify(payload.properties.content),
- }),
- }).then((x) => {
- log.info("synced", {
- key: payload.properties.key,
- status: x.status,
+
+ queue = queue
+ .then(() =>
+ fetch(`${URL}/share_sync`, {
+ method: "POST",
+ body: JSON.stringify({
+ sessionID: sessionID,
+ shareID: session.shareID,
+ key: payload.properties.key,
+ content: JSON.stringify(payload.properties.content),
+ }),
+ }),
+ )
+ .then((x) => {
+ log.info("synced", {
+ key: payload.properties.key,
+ status: x.status,
+ });
});
- });
});
});