Skip to content

Commit

Permalink
Wait for the appview to recognize the list before proceeding with lis…
Browse files Browse the repository at this point in the history
…t creation
  • Loading branch information
pfrazee committed Nov 1, 2023
1 parent 0525ce0 commit 5b73242
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/lib/async/until.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import {timeout} from './timeout'

export async function until(
retries: number,
delay: number,
cond: (v: any, err: any) => boolean,
fn: () => Promise<any>,
): Promise<boolean> {
let lastErr
while (retries > 0) {
try {
const v = await fn()
if (cond(v, undefined)) {
return true
}
} catch (e: any) {
if (cond(undefined, e)) {
return true
}
}
await timeout(delay)
}
throw lastErr
}
15 changes: 15 additions & 0 deletions src/state/models/content/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import * as apilib from 'lib/api/index'
import {cleanError} from 'lib/strings/errors'
import {bundleAsync} from 'lib/async/bundle'
import {track} from 'lib/analytics/analytics'
import {until} from 'lib/async/until'

const PAGE_SIZE = 30

Expand Down Expand Up @@ -84,6 +85,20 @@ export class ListModel {
},
record,
)

// wait for the appview to update
await until(
5, // 5 tries
1e3, // 1s delay between tries
(v: GetList.Response, _e: any) => {
return typeof v?.data?.list.uri === 'string'
},
() =>
rootStore.agent.app.bsky.graph.getList({
list: res.uri,
limit: 1,
}),
)
return res
}

Expand Down

0 comments on commit 5b73242

Please sign in to comment.