Skip to content

Commit

Permalink
update: improve observable returning type
Browse files Browse the repository at this point in the history
  • Loading branch information
clostao committed Dec 19, 2024
1 parent 277452d commit 42f7b49
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 16 deletions.
36 changes: 26 additions & 10 deletions packages/auto-drive/src/api/models/uploads.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,17 +74,33 @@ export type CompleteUploadResponse = {
cid: string
}

export type UploadFileStatus = {
type: 'file'
progress: number
cid?: string
}
export type UploadFileStatus =
| {
completed: true
type: 'file'
progress: number
cid: string
}
| {
completed: false
type: 'file'
progress: number
cid: null
}

export type UploadFolderStatus = {
type: 'folder'
progress: number
cid?: string
}
export type UploadFolderStatus =
| {
completed: true
type: 'folder'
progress: number
cid: string
}
| {
completed: false
type: 'folder'
progress: number
cid: null
}

export type UploadChunksStatus = {
uploadBytes: number
Expand Down
37 changes: 32 additions & 5 deletions packages/auto-drive/src/api/wrappers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,22 @@ export const uploadFileFromInput = (
})

await uploadFileChunks(api, fileUpload.id, asyncIterable, uploadChunkSize).forEach((e) =>
subscriber.next({ type: 'file', progress: progressToPercentage(e.uploadBytes, file.size) }),
subscriber.next({
type: 'file',
progress: progressToPercentage(e.uploadBytes, file.size),
completed: false,
cid: null,
}),
)

const result = await apiCalls.completeUpload(api, { uploadId: fileUpload.id })

subscriber.next({ type: 'file', progress: 100, cid: result.cid })
subscriber.next({
type: 'file',
progress: 100,
completed: true,
cid: result.cid,
})
subscriber.complete()
})
}
Expand Down Expand Up @@ -169,12 +179,22 @@ export const uploadFile = (
})

await uploadFileChunks(api, fileUpload.id, asyncIterable, uploadChunkSize).forEach((e) =>
subscriber.next({ type: 'file', progress: progressToPercentage(e.uploadBytes, file.size) }),
subscriber.next({
type: 'file',
progress: progressToPercentage(e.uploadBytes, file.size),
completed: false,
cid: null,
}),
)

const result = await apiCalls.completeUpload(api, { uploadId: fileUpload.id })

subscriber.next({ type: 'file', progress: 100, cid: result.cid })
subscriber.next({
type: 'file',
progress: 100,
completed: true,
cid: result.cid,
})
subscriber.complete()
})
}
Expand Down Expand Up @@ -251,6 +271,8 @@ export const uploadFolderFromInput = async (
subscriber.next({
type: 'folder',
progress: progressToPercentage(currentBytesUploaded + e.uploadBytes, totalSize),
completed: false,
cid: null,
})
})

Expand All @@ -259,7 +281,12 @@ export const uploadFolderFromInput = async (

const result = await apiCalls.completeUpload(api, { uploadId: folderUpload.id })

subscriber.next({ type: 'folder', progress: 100, cid: result.cid })
subscriber.next({
type: 'folder',
progress: 100,
completed: true,
cid: result.cid,
})
subscriber.complete()
})
}
Expand Down
9 changes: 8 additions & 1 deletion packages/auto-drive/src/fs/wrappers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,14 +126,21 @@ export const uploadFolderFromFolderPath = async (
subscriber.next({
type: 'folder',
progress: progressToPercentage(progress + e.uploadBytes, totalSize),
completed: false,
cid: null,
}),
)
progress += file.size
}

const result = await apiCalls.completeUpload(api, { uploadId: folderUpload.id })

subscriber.next({ type: 'folder', progress: 100, cid: result.cid })
subscriber.next({
type: 'folder',
progress: 100,
completed: true,
cid: result.cid,
})
subscriber.complete()
})
}

0 comments on commit 42f7b49

Please sign in to comment.