Remove waterfalls from posting (#5931)

* Extract resolveRT, resolveReply

* Parallelize waterfalls
This commit is contained in:
dan
2024-10-25 00:00:07 +01:00
committed by GitHub
parent 52f05424bf
commit a729efc3b6
+36 -29
View File
@@ -49,43 +49,20 @@ export async function post(
opts: PostOpts, opts: PostOpts,
) { ) {
const draft = opts.draft const draft = opts.draft
let reply
let rt = new RichText(
{text: draft.richtext.text.trimEnd()},
{cleanNewlines: true},
)
opts.onStateChange?.(t`Processing...`) opts.onStateChange?.(t`Processing...`)
// NB -- Do not await anything here to avoid waterfalls!
await rt.detectFacets(agent) // Instead, store Promises which will be unwrapped as they're needed.
const rtPromise = resolveRT(agent, draft.richtext)
rt = shortenLinks(rt) const embedPromise = resolveEmbed(
rt = stripInvalidMentions(rt)
const embed = await resolveEmbed(
agent, agent,
queryClient, queryClient,
draft, draft,
opts.onStateChange, opts.onStateChange,
) )
let replyPromise
// add replyTo if post is a reply to another post
if (opts.replyTo) { if (opts.replyTo) {
const replyToUrip = new AtUri(opts.replyTo) replyPromise = resolveReply(agent, 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,
}
}
} }
// set labels // set labels
@@ -111,6 +88,9 @@ export async function post(
// Create post record // Create post record
{ {
const rt = await rtPromise
const embed = await embedPromise
const reply = await replyPromise
const record: AppBskyFeedPost.Record = { const record: AppBskyFeedPost.Record = {
$type: 'app.bsky.feed.post', $type: 'app.bsky.feed.post',
createdAt: date, createdAt: date,
@@ -188,6 +168,33 @@ export async function post(
return {uri} 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( async function resolveEmbed(
agent: BskyAgent, agent: BskyAgent,
queryClient: QueryClient, queryClient: QueryClient,