Просмотр исходного кода

Fix race condition in share sync by implementing request queue

🤖 Generated with opencode
Co-Authored-By: opencode <[email protected]>
Dax Raad 9 месяцев назад
Родитель
Сommit
6ef0b991ec
1 измененных файлов с 21 добавлено и 14 удалено
  1. 21 14
      js/src/share/share.ts

+ 21 - 14
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,
+          });
         });
-      });
     });
   });