Skip to content

Commit

Permalink
fix various type issues (#4958)
Browse files Browse the repository at this point in the history
neglected in #4911:

- bytesTotal can be null (also handle that bug in runtime)
- progress is not a property on the progress object
- use `satisfies` instead of `as`
- add todo about broken `throttle` implementation
- in emitSocketProgress, progressData should not have the FileProgress type, as it is a completely different type (it comes from companion)
  • Loading branch information
mifi committed Feb 28, 2024
1 parent a4bbd82 commit 448e667
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 7 deletions.
5 changes: 3 additions & 2 deletions packages/@uppy/core/src/Uppy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -941,7 +941,7 @@ export class Uppy<M extends Meta, B extends Body> {
bytesTotal: size,
uploadComplete: false,
uploadStarted: null,
} as FileProgressNotStarted,
} satisfies FileProgressNotStarted,
size,
isGhost: false,
isRemote: file.isRemote || false,
Expand Down Expand Up @@ -1376,6 +1376,8 @@ export class Uppy<M extends Meta, B extends Body> {
// and click 'ADD MORE FILES', - focus won't activate in Firefox.
// - We must throttle at around >500ms to avoid performance lags.
// [Practical Check] Firefox, try to upload a big file for a prolonged period of time. Laptop will start to heat up.
// todo when uploading multiple files, this will cause problems because they share the same throttle,
// meaning some files might never get their progress reported (eaten up by progress events from other files)
calculateProgress = throttle(
(file, data) => {
const fileInState = this.getFile(file?.id)
Expand Down Expand Up @@ -1558,7 +1560,6 @@ export class Uppy<M extends Meta, B extends Body> {
file.id,
{
progress: {
progress: 0,
uploadStarted: Date.now(),
uploadComplete: false,
percentage: 0,
Expand Down
4 changes: 1 addition & 3 deletions packages/@uppy/utils/src/FileProgress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@ export type FileProcessingInfo =
| DeterminateFileProcessing

interface FileProgressBase {
progress?: number
uploadComplete?: boolean
percentage?: number
bytesTotal: number
bytesTotal: number | null
preprocess?: FileProcessingInfo
postprocess?: FileProcessingInfo
}
Expand All @@ -26,7 +25,6 @@ interface FileProgressBase {
export type FileProgressStarted = FileProgressBase & {
uploadStarted: number
bytesUploaded: number
progress?: number
}
export type FileProgressNotStarted = FileProgressBase & {
uploadStarted: null
Expand Down
9 changes: 7 additions & 2 deletions packages/@uppy/utils/src/emitSocketProgress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,22 @@ import type { FileProgress } from './FileProgress'

function emitSocketProgress(
uploader: any,
progressData: FileProgress,
progressData: {
progress: string // pre-formatted percentage
bytesTotal: number
bytesUploaded: number
},
file: UppyFile<any, any>,
): void {
const { progress, bytesUploaded, bytesTotal } = progressData
if (progress) {
uploader.uppy.log(`Upload progress: ${progress}`)
uploader.uppy.emit('upload-progress', file, {
// @ts-expect-error todo remove in next major
uploader,
bytesUploaded,
bytesTotal,
})
} satisfies FileProgress)
}
}

Expand Down
1 change: 1 addition & 0 deletions packages/@uppy/utils/src/getBytesRemaining.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { FileProgress } from './FileProgress'

export default function getBytesRemaining(fileProgress: FileProgress): number {
if (fileProgress.bytesTotal == null) return 0
return fileProgress.bytesTotal - (fileProgress.bytesUploaded as number)
}

0 comments on commit 448e667

Please sign in to comment.