Skip to content

Commit

Permalink
feat(editor): resolve with result for addRequest
Browse files Browse the repository at this point in the history
  • Loading branch information
robertu7 committed Aug 2, 2024
1 parent c78fe87 commit 44c9439
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 21 deletions.
52 changes: 31 additions & 21 deletions src/components/Context/DraftDetailState/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ import { ME_WORKS_TABS } from '~/views/Me/Works/WorksTabs/gql'
type Job = {
id: string
fn: () => Promise<any>
resolve: (value: any) => void
reject: (reason?: any) => void
}

const NEW_DRAFT_ID = 'new'

export const DraftDetailStateContext = createContext(
{} as {
addRequest: (fn: () => Promise<any>) => void
addRequest: (fn: () => Promise<any>) => Promise<any>
getDraftId: () => string | undefined
isNewDraft: () => boolean
createDraft: (props: { onCreate: (draftId: string) => any }) => any
Expand All @@ -33,19 +35,21 @@ export const DraftDetailStateProvider = ({
* Request job queue
*/
// Run request jobs in sequence
const jobsRef = useRef<Job[]>()
const jobsRef = useRef<Job[]>([])
const runningRef = useRef<string>()

// push request job
const addRequest = (fn: () => Promise<any>) => {
const id = randomString()
const jobs = jobsRef.current || []
const newJobs = [...jobs, { id, fn }]
jobsRef.current = newJobs

if (!runningRef.current) {
runFirstJob()
}
const addRequest = (fn: () => Promise<any>): Promise<any> => {
return new Promise((resolve, reject) => {
const id = randomString()
const jobs = jobsRef.current
const newJob = { id, fn, resolve, reject }
jobsRef.current = [...jobs, newJob]

if (!runningRef.current) {
runFirstJob()
}
})
}

const getJobs = () => {
Expand All @@ -63,17 +67,23 @@ export const DraftDetailStateProvider = ({
return
}

// run first job
// Run first job
runningRef.current = firstJob.id
await firstJob.fn()
runningRef.current = ''

// set to rest jobs
const { restJobs } = getJobs()
jobsRef.current = restJobs

// run next job
runFirstJob()
try {
const result = await firstJob.fn()
firstJob.resolve(result) // Resolve the promise with the job result
} catch (error) {
firstJob.reject(error) // Reject the promise if there's an error
} finally {
runningRef.current = ''

// Set to rest jobs
const { restJobs } = getJobs()
jobsRef.current = restJobs

// Run next job
runFirstJob()
}
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/components/Editor/SetCover/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ const SetCover: React.FC<SetCoverProps> & { Dialog: typeof SetCoverDialog } = ({
const [selected, setSelected] = useState(assets.find(filter))
const onSave = async () => {
const result = await editCover(selected)

// set selected cover if fallback cover specified by server
if (cover && cover === result?.data?.putDraft?.cover && !selected) {
setSelected(assets.find(filter))
Expand Down

0 comments on commit 44c9439

Please sign in to comment.