Skip to content

Commit

Permalink
Fix utils now that core changed
Browse files Browse the repository at this point in the history
  • Loading branch information
Murderlon committed Dec 5, 2023
1 parent be25992 commit 521e4df
Show file tree
Hide file tree
Showing 10 changed files with 58 additions and 40 deletions.
4 changes: 2 additions & 2 deletions packages/@uppy/core/src/Uppy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,8 +280,8 @@ export class Uppy<M extends Meta, B extends Body> {
this.opts = {
...merged,
restrictions: {
...defaultOptions.restrictions,
...(opts && (opts.restrictions as Restrictions)),
...(defaultOptions.restrictions as Restrictions),
...(opts && opts.restrictions),
},
}

Expand Down
29 changes: 16 additions & 13 deletions packages/@uppy/utils/src/EventManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ import type {
GenericEventCallback,
// @ts-expect-error @uppy/core has not been typed yet
} from '@uppy/core'
import type { UppyFile } from './UppyFile'
import type { Meta, Body, UppyFile } from './UppyFile'
/**
* Create a wrapper around an event emitter with a `remove` method to remove
* all events that were added using the wrapped emitter.
*/
export default class EventManager {
#uppy: Uppy
export default class EventManager<M extends Meta, B extends Body> {
#uppy: Uppy<M, B>

#events: Array<Parameters<typeof Uppy.prototype.on>> = []

constructor(uppy: Uppy) {
constructor(uppy: Uppy<M, B>) {
this.#uppy = uppy
}

Expand All @@ -34,7 +34,10 @@ export default class EventManager {
}
}

onFilePause(fileID: UppyFile['id'], cb: (isPaused: boolean) => void): void {
onFilePause(
fileID: UppyFile<M, B>['id'],
cb: (isPaused: boolean) => void,
): void {
this.on(
'upload-pause',
(
Expand All @@ -50,16 +53,16 @@ export default class EventManager {
}

onFileRemove(
fileID: UppyFile['id'],
cb: (isPaused: UppyFile['id']) => void,
fileID: UppyFile<M, B>['id'],
cb: (isPaused: UppyFile<M, B>['id']) => void,
): void {
this.on('file-removed', (file: Parameters<FileRemovedCallback<any>>[0]) => {
// @ts-expect-error @uppy/core has not been typed yet
if (fileID === file.id) cb(file.id)
})
}

onPause(fileID: UppyFile['id'], cb: (isPaused: boolean) => void): void {
onPause(fileID: UppyFile<M, B>['id'], cb: (isPaused: boolean) => void): void {
this.on(
'upload-pause',
(
Expand All @@ -75,7 +78,7 @@ export default class EventManager {
)
}

onRetry(fileID: UppyFile['id'], cb: () => void): void {
onRetry(fileID: UppyFile<M, B>['id'], cb: () => void): void {
this.on(
'upload-retry',
(targetFileID: Parameters<UploadRetryCallback>[0]) => {
Expand All @@ -86,22 +89,22 @@ export default class EventManager {
)
}

onRetryAll(fileID: UppyFile['id'], cb: () => void): void {
onRetryAll(fileID: UppyFile<M, B>['id'], cb: () => void): void {
this.on('retry-all', () => {
if (!this.#uppy.getFile(fileID)) return
cb()
})
}

onPauseAll(fileID: UppyFile['id'], cb: () => void): void {
onPauseAll(fileID: UppyFile<M, B>['id'], cb: () => void): void {
this.on('pause-all', () => {
if (!this.#uppy.getFile(fileID)) return
cb()
})
}

onCancelAll(
fileID: UppyFile['id'],
fileID: UppyFile<M, B>['id'],
eventHandler: GenericEventCallback,
): void {
this.on('cancel-all', (...args: Parameters<GenericEventCallback>) => {
Expand All @@ -110,7 +113,7 @@ export default class EventManager {
})
}

onResumeAll(fileID: UppyFile['id'], cb: () => void): void {
onResumeAll(fileID: UppyFile<M, B>['id'], cb: () => void): void {
this.on('resume-all', () => {
if (!this.#uppy.getFile(fileID)) return
cb()
Expand Down
2 changes: 1 addition & 1 deletion packages/@uppy/utils/src/emitSocketProgress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type { FileProgress } from './FileProgress'
function emitSocketProgress(
uploader: any,
progressData: FileProgress,
file: UppyFile,
file: UppyFile<any,any>,
): void {
const { progress, bytesUploaded, bytesTotal } = progressData
if (progress) {
Expand Down
6 changes: 3 additions & 3 deletions packages/@uppy/utils/src/fileFilters.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import type { UppyFile } from './UppyFile'

export function filterNonFailedFiles(files: UppyFile[]): UppyFile[] {
const hasError = (file: UppyFile): boolean => 'error' in file && !!file.error
export function filterNonFailedFiles(files: UppyFile<any,any>[]): UppyFile<any,any>[] {
const hasError = (file: UppyFile<any,any>): boolean => 'error' in file && !!file.error

return files.filter((file) => !hasError(file))
}

// Don't double-emit upload-started for Golden Retriever-restored files that were already started
export function filterFilesToEmitUploadStarted(files: UppyFile[]): UppyFile[] {
export function filterFilesToEmitUploadStarted(files: UppyFile<any,any>[]): UppyFile<any,any>[] {
return files.filter(
(file) => !file.progress?.uploadStarted || !file.isRestored,
)
Expand Down
6 changes: 3 additions & 3 deletions packages/@uppy/utils/src/generateFileID.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function encodeFilename(name: string): string {
* Takes a file object and turns it into fileID, by converting file.name to lowercase,
* removing extra characters and adding type, size and lastModified
*/
export default function generateFileID(file: UppyFile): string {
export default function generateFileID(file: UppyFile<any,any>): string {
// It's tempting to do `[items].filter(Boolean).join('-')` here, but that
// is slower! simple string concatenation is fast

Expand Down Expand Up @@ -48,7 +48,7 @@ export default function generateFileID(file: UppyFile): string {

// If the provider has a stable, unique ID, then we can use that to identify the file.
// Then we don't have to generate our own ID, and we can add the same file many times if needed (different path)
function hasFileStableId(file: UppyFile): boolean {
function hasFileStableId(file: UppyFile<any,any>): boolean {
if (!file.isRemote || !file.remote) return false
// These are the providers that it seems like have stable IDs for their files. The other's I haven't checked yet.
const stableIdProviders = new Set([
Expand All @@ -61,7 +61,7 @@ function hasFileStableId(file: UppyFile): boolean {
return stableIdProviders.has(file.remote.provider as any)
}

export function getSafeFileId(file: UppyFile): string {
export function getSafeFileId(file: UppyFile<any,any>): string {
if (hasFileStableId(file)) return file.id

const fileType = getFileType(file)
Expand Down
9 changes: 5 additions & 4 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 {
return fileProgress.bytesTotal - (fileProgress.bytesUploaded as number)
export default function getBytesRemaining(fileProgress: {
bytesTotal: number
bytesUploaded: number
}): number {
return fileProgress.bytesTotal - fileProgress.bytesUploaded
}
15 changes: 14 additions & 1 deletion packages/@uppy/utils/src/getETA.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,20 @@ import getSpeed from './getSpeed.ts'
import getBytesRemaining from './getBytesRemaining.ts'
import type { FileProgress } from './FileProgress.ts'

export default function getETA(fileProgress: FileProgress): number {
type NonNullableFileProgress = Omit<
FileProgress,
| 'preprocess'
| 'postprocess'
| 'bytesTotal'
| 'bytesUploaded'
| 'uploadStarted'
> & {
bytesTotal: number
bytesUploaded: number
uploadStarted: number
}

export default function getETA(fileProgress: NonNullableFileProgress): number {
if (!fileProgress.bytesUploaded) return 0

const uploadSpeed = getSpeed(fileProgress)
Expand Down
18 changes: 9 additions & 9 deletions packages/@uppy/utils/src/getFileType.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ describe('getFileType', () => {
isRemote: true,
type: 'audio/webm',
name: 'foo.webm',
} as any as UppyFile
} as any as UppyFile<any,any>
expect(getFileType(file)).toEqual('audio/webm')
})

Expand All @@ -17,32 +17,32 @@ describe('getFileType', () => {
type: 'audio/webm',
name: 'foo.webm',
data: 'sdfsdfhq9efbicw',
} as any as UppyFile
} as any as UppyFile<any,any>
expect(getFileType(file)).toEqual('audio/webm')
})

it('should determine the filetype from the extension', () => {
const fileMP3 = {
name: 'foo.mp3',
data: 'sdfsfhfh329fhwihs',
} as any as UppyFile
} as any as UppyFile<any,any>
const fileYAML = {
name: 'bar.yaml',
data: 'sdfsfhfh329fhwihs',
} as any as UppyFile
} as any as UppyFile<any,any>
const fileMKV = {
name: 'bar.mkv',
data: 'sdfsfhfh329fhwihs',
} as any as UppyFile
} as any as UppyFile<any,any>
const fileDicom = {
name: 'bar.dicom',
data: 'sdfsfhfh329fhwihs',
} as any as UppyFile
} as any as UppyFile<any,any>
const fileWebp = {
name: 'bar.webp',
data: 'sdfsfhfh329fhwihs',
} as any as UppyFile
const toUpper = (file: UppyFile) => ({
} as any as UppyFile<any,any>
const toUpper = (file: UppyFile<any,any>) => ({
...file,
name: file.name.toUpperCase(),
})
Expand All @@ -62,7 +62,7 @@ describe('getFileType', () => {
const file = {
name: 'foobar',
data: 'sdfsfhfh329fhwihs',
} as any as UppyFile
} as any as UppyFile<any,any>
expect(getFileType(file)).toEqual('application/octet-stream')
})
})
2 changes: 1 addition & 1 deletion packages/@uppy/utils/src/getFileType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { UppyFile } from './UppyFile'
import getFileNameAndExtension from './getFileNameAndExtension.ts'
import mimeTypes from './mimeTypes.ts'

export default function getFileType(file: Partial<UppyFile>): string {
export default function getFileType(file: Partial<UppyFile<any, any>>): string {
if (file.type) return file.type

const fileExtension = file.name
Expand Down
7 changes: 4 additions & 3 deletions packages/@uppy/utils/src/getSpeed.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { FileProgress } from './FileProgress'

export default function getSpeed(fileProgress: FileProgress): number {
export default function getSpeed(fileProgress: {
bytesUploaded?: number
uploadStarted: number
}): number {
if (!fileProgress.bytesUploaded) return 0

const timeElapsed = Date.now() - fileProgress.uploadStarted
Expand Down

0 comments on commit 521e4df

Please sign in to comment.