Skip to content

Commit

Permalink
fix: add is ready poll in uploadFromUrl (#238)
Browse files Browse the repository at this point in the history
* add is ready poll
* use is ready poll in from url and base uploads
  • Loading branch information
jeetiss authored Mar 13, 2020
1 parent f291888 commit dd0202d
Show file tree
Hide file tree
Showing 6 changed files with 200 additions and 190 deletions.
47 changes: 47 additions & 0 deletions src/tools/isReadyPoll.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import info from '../api/info'
import { poll } from './poll'
import { FileInfo } from '../api/types'
import CancelController from './CancelController'

type ArgsIsReadyPool = {
file: string
publicKey: string
baseURL?: string
source?: string
integration?: string
retryThrottledRequestMaxTimes?: number
onProgress?: (args: { value: number }) => void
cancel?: CancelController
}

function isReadyPoll({
file,
publicKey,
baseURL,
source,
integration,
retryThrottledRequestMaxTimes,
cancel,
onProgress
}: ArgsIsReadyPool): FileInfo | PromiseLike<FileInfo> {
return poll<FileInfo>({
check: cancel =>
info(file, {
publicKey,
baseURL,
cancel,
source,
integration,
retryThrottledRequestMaxTimes
}).then(response => {
if (response.isReady) {
return response
}
onProgress && onProgress({ value: 1 })
return false
}),
cancel
})
}

export { isReadyPoll }
62 changes: 0 additions & 62 deletions src/uploadFile/pollStrategy.ts

This file was deleted.

76 changes: 0 additions & 76 deletions src/uploadFile/pushStrategy.ts

This file was deleted.

16 changes: 16 additions & 0 deletions src/uploadFile/pusher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,4 +182,20 @@ class Pusher {
}
}

let pusher: Pusher | null = null
const getPusher = (key: string): Pusher => {
if (!pusher) {
// no timeout for nodeJS and 30000 ms for browser
const disconectTimeout = typeof window === 'undefined' ? 0 : 30000
pusher = new Pusher(key, disconectTimeout)
}

return pusher
}

const preconnect = (key: string): void => {
getPusher(key).connect()
}

export default Pusher
export { getPusher, preconnect }
47 changes: 11 additions & 36 deletions src/uploadFile/uploadBase.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import base from '../api/base'
import info from '../api/info'
import { poll } from '../tools/poll'
import { UploadcareFile } from '../tools/UploadcareFile'
import CancelController from '../tools/CancelController'

/* Types */
import { FileInfo } from '../api/types'
import { NodeFile, BrowserFile } from '../request/types'
import { isReadyPoll } from '../tools/isReadyPoll'

type FromObjectOptions = {
publicKey: string
Expand Down Expand Up @@ -50,13 +47,6 @@ const uploadFromObject = (
baseCDN
}: FromObjectOptions
): Promise<UploadcareFile> => {
let progress: number

const onProgressCallback = ({ value }): void => {
progress = value * 0.98
onProgress && onProgress({ value: progress })
}

return base(file, {
publicKey,
fileName,
Expand All @@ -65,36 +55,21 @@ const uploadFromObject = (
secureExpire,
store,
cancel,
onProgress: onProgress ? onProgressCallback : undefined,
onProgress,
source,
integration,
retryThrottledRequestMaxTimes
})
.then(({ file }) => {
return poll<FileInfo>({
check: cancel =>
info(file, {
publicKey,
baseURL,
cancel,
source,
integration,
retryThrottledRequestMaxTimes
}).then(response => {
if (response.isReady) {
onProgress && onProgress({ value: 1 })

return response
}

if (onProgress) {
onProgress({
value: Math.min(progress + 0.02, 1)
})
}

return false
})
return isReadyPoll({
file,
publicKey,
baseURL,
source,
integration,
retryThrottledRequestMaxTimes,
onProgress,
cancel
})
})
.then(fileInfo => new UploadcareFile(fileInfo, { baseCDN }))
Expand Down
Loading

0 comments on commit dd0202d

Please sign in to comment.