about summary refs log tree commit diff
path: root/src/lib
diff options
context:
space:
mode:
authordan <dan.abramov@gmail.com>2024-10-25 00:00:07 +0100
committerGitHub <noreply@github.com>2024-10-25 00:00:07 +0100
commita729efc3b654a3086bb4216f9f9fae74f622e57c (patch)
treed32e0c30f02e72da68223d5c395f9e914b62480e /src/lib
parent52f05424bf8f3b4decf29e0612a16de24cb972c1 (diff)
downloadvoidsky-a729efc3b654a3086bb4216f9f9fae74f622e57c.tar.zst
Remove waterfalls from posting (#5931)
* Extract resolveRT, resolveReply

* Parallelize waterfalls
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/api/index.ts65
1 files changed, 36 insertions, 29 deletions
diff --git a/src/lib/api/index.ts b/src/lib/api/index.ts
index a08d7ae5b..f727aa4ca 100644
--- a/src/lib/api/index.ts
+++ b/src/lib/api/index.ts
@@ -49,43 +49,20 @@ export async function post(
   opts: PostOpts,
 ) {
   const draft = opts.draft
-  let reply
-  let rt = new RichText(
-    {text: draft.richtext.text.trimEnd()},
-    {cleanNewlines: true},
-  )
 
   opts.onStateChange?.(t`Processing...`)
-
-  await rt.detectFacets(agent)
-
-  rt = shortenLinks(rt)
-  rt = stripInvalidMentions(rt)
-
-  const embed = await resolveEmbed(
+  // NB -- Do not await anything here to avoid waterfalls!
+  // Instead, store Promises which will be unwrapped as they're needed.
+  const rtPromise = resolveRT(agent, draft.richtext)
+  const embedPromise = resolveEmbed(
     agent,
     queryClient,
     draft,
     opts.onStateChange,
   )
-
-  // add replyTo if post is a reply to another post
+  let replyPromise
   if (opts.replyTo) {
-    const replyToUrip = new AtUri(opts.replyTo)
-    const parentPost = await agent.getPost({
-      repo: replyToUrip.host,
-      rkey: replyToUrip.rkey,
-    })
-    if (parentPost) {
-      const parentRef = {
-        uri: parentPost.uri,
-        cid: parentPost.cid,
-      }
-      reply = {
-        root: parentPost.value.reply?.root || parentRef,
-        parent: parentRef,
-      }
-    }
+    replyPromise = resolveReply(agent, opts.replyTo)
   }
 
   // set labels
@@ -111,6 +88,9 @@ export async function post(
 
   // Create post record
   {
+    const rt = await rtPromise
+    const embed = await embedPromise
+    const reply = await replyPromise
     const record: AppBskyFeedPost.Record = {
       $type: 'app.bsky.feed.post',
       createdAt: date,
@@ -188,6 +168,33 @@ export async function post(
   return {uri}
 }
 
+async function resolveRT(agent: BskyAgent, richtext: RichText) {
+  let rt = new RichText({text: richtext.text.trimEnd()}, {cleanNewlines: true})
+  await rt.detectFacets(agent)
+
+  rt = shortenLinks(rt)
+  rt = stripInvalidMentions(rt)
+  return rt
+}
+
+async function resolveReply(agent: BskyAgent, replyTo: string) {
+  const replyToUrip = new AtUri(replyTo)
+  const parentPost = await agent.getPost({
+    repo: replyToUrip.host,
+    rkey: replyToUrip.rkey,
+  })
+  if (parentPost) {
+    const parentRef = {
+      uri: parentPost.uri,
+      cid: parentPost.cid,
+    }
+    return {
+      root: parentPost.value.reply?.root || parentRef,
+      parent: parentRef,
+    }
+  }
+}
+
 async function resolveEmbed(
   agent: BskyAgent,
   queryClient: QueryClient,