Skip to content

Commit

Permalink
Improve types of the loadingService
Browse files Browse the repository at this point in the history
  • Loading branch information
JammingBen committed Mar 17, 2023
1 parent 06c8707 commit 65c8e76
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 17 deletions.
11 changes: 4 additions & 7 deletions packages/web-app-files/src/helpers/resource/actions/transfer.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Resource } from 'web-client'
import { join } from 'path'
import { SpaceResource } from 'web-client/src/helpers'
import { ClientService, LoadingService, LoadingTaskState } from 'web-pkg/src/services'
import { ClientService, LoadingService, LoadingTaskCallbackArguments } from 'web-pkg/src/services'
import {
ConflictDialog,
ResolveStrategy,
Expand Down Expand Up @@ -120,12 +120,9 @@ export class ResourceTransfer extends ConflictDialog {

return this.loadingService.addTask(
({ setProgress }) => {
return this.moveResources(
resolvedConflicts,
targetFolderResources,
transferType,
return this.moveResources(resolvedConflicts, targetFolderResources, transferType, {
setProgress
)
})
},
{ indeterminate: transferType === TransferType.COPY }
)
Expand All @@ -135,7 +132,7 @@ export class ResourceTransfer extends ConflictDialog {
resolvedConflicts: FileConflict[],
targetFolderResources: Resource[],
transferType: TransferType,
setProgress: (args: LoadingTaskState) => void
{ setProgress }: LoadingTaskCallbackArguments
) {
const movedResources: Resource[] = []
const errors = []
Expand Down
7 changes: 4 additions & 3 deletions packages/web-app-files/src/store/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
SpaceResource
} from 'web-client/src/helpers'
import { WebDAV } from 'web-client/src/webdav'
import { ClientService, LoadingTaskState } from 'web-pkg/src/services'
import { ClientService, LoadingTaskCallbackArguments } from 'web-pkg/src/services'
import { Language } from 'vue3-gettext'
import { DavProperty } from 'web-client/src/webdav/constants'
import { AncestorMetaData } from 'web-app-files/src/helpers/resource/ancestorMetaData'
Expand Down Expand Up @@ -160,7 +160,7 @@ export default {
space: SpaceResource
files: Resource[]
clientService: ClientService
setProgress: (args: LoadingTaskState) => void
loadingCallbackArgs: LoadingTaskCallbackArguments
firstRun: boolean
} & Language
) {
Expand All @@ -170,9 +170,10 @@ export default {
space,
files,
clientService,
setProgress,
loadingCallbackArgs,
firstRun = true
} = options
const { setProgress } = loadingCallbackArgs
const promises = []
const removedFiles = []
for (const file of files) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ClientService, LoadingService } from 'web-pkg/src/services'
import { ClientService, LoadingService, LoadingTaskCallbackArguments } from 'web-pkg/src/services'
import {
ResolveConflict,
ResourceTransfer,
Expand All @@ -12,7 +12,7 @@ import { ListFilesResult } from 'web-client/src/webdav/listFiles'
const clientServiceMock = mockDeep<ClientService>()
const loadingServiceMock = mock<LoadingService>({
addTask: (callback) => {
return callback({ setProgress: jest.fn() })
return callback(mock<LoadingTaskCallbackArguments>())
}
})
let resourcesToMove
Expand Down
10 changes: 7 additions & 3 deletions packages/web-pkg/src/services/loadingService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ export interface LoadingTask {
state?: LoadingTaskState
}

export interface LoadingTaskCallbackArguments {
setProgress: (args: LoadingTaskState) => void
}

// time until a loading task is being set active
const DEFAULT_DEBOUNCE_TIME = 200

Expand Down Expand Up @@ -51,13 +55,13 @@ export class LoadingService {
return Math.round((progress / tasks.length) * 100)
}

public addTask(
callback: ({ setProgress }: { setProgress: (args: LoadingTaskState) => void }) => Promise<any>,
public addTask<T>(
callback: ({ setProgress }: LoadingTaskCallbackArguments) => Promise<T>,
{
debounceTime = DEFAULT_DEBOUNCE_TIME,
indeterminate = true
}: { debounceTime?: number; indeterminate?: boolean } = {}
): Promise<any> {
): Promise<T> {
const task = {
id: uuid.v4(),
active: false,
Expand Down
4 changes: 2 additions & 2 deletions packages/web-test-helpers/src/mocks/defaultComponentMocks.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { mock, mockDeep } from 'jest-mock-extended'
import { ClientService, LoadingService } from 'web-pkg/src/services'
import { ClientService, LoadingService, LoadingTaskCallbackArguments } from 'web-pkg/src/services'
import { Router, RouteLocationNormalizedLoaded, RouteLocationRaw, RouteLocation } from 'vue-router'
import { UppyService } from 'web-runtime/src/services/uppyService'
import { OwnCloudSdk } from 'web-client/src/types'
Expand All @@ -25,7 +25,7 @@ export const defaultComponentMocks = ({ currentRoute = undefined }: ComponentMoc
$uppyService: mockDeep<UppyService>(),
$loadingService: mock<LoadingService>({
addTask: (callback) => {
return callback({ setProgress: jest.fn() })
return callback(mock<LoadingTaskCallbackArguments>())
}
})
}
Expand Down

0 comments on commit 65c8e76

Please sign in to comment.