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 15, 2023
1 parent 247d115 commit 42547e5
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 25 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: false }
)
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
8 changes: 5 additions & 3 deletions packages/web-app-files/src/mixins/actions/restore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { mapActions, mapGetters, mapMutations, mapState } from 'vuex'
import { dirname } from 'path'
import { isLocationTrashActive } from '../../router'

import { clientService, LoadingTaskState } from 'web-pkg/src/services'
import { clientService, LoadingTaskCallbackArguments } from 'web-pkg/src/services'
import { Resource, isProjectSpaceResource, extractExtensionFromFile } from 'web-client/src/helpers'
import {
ResolveStrategy,
Expand Down Expand Up @@ -168,7 +168,7 @@ export default {
async $_restore_restoreResources(
resources: Resource[],
missingFolderPaths: string[],
setProgress: (args: LoadingTaskState) => void
{ setProgress }: LoadingTaskCallbackArguments
) {
const restoredResources = []
const failedResources = []
Expand Down Expand Up @@ -283,7 +283,9 @@ export default {
}
return this.$loadingService.addTask(
({ setProgress }) => {
return this.$_restore_restoreResources(resolvedResources, missingFolderPaths, setProgress)
return this.$_restore_restoreResources(resolvedResources, missingFolderPaths, {
setProgress
})
},
{ indeterminate: false }
)
Expand Down
4 changes: 2 additions & 2 deletions packages/web-app-files/src/mixins/deleteResources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,13 +154,13 @@ export default {

$_deleteResources_filesList_delete() {
return this.$loadingService.addTask(
({ setProgress }) => {
(loadingCallbackArgs) => {
return this.deleteFiles({
...this.$language,
space: this.space,
files: this.$_deleteResources_resources,
clientService: this.$clientService,
setProgress
loadingCallbackArgs
}).then(async () => {
this.hideModal()
this.toggleModalConfirmButton()
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
Expand Up @@ -3,7 +3,7 @@ import { createLocationTrash, createLocationSpaces } from '../../../../src/route
import { mock, mockDeep } from 'jest-mock-extended'
import { OwnCloudSdk } from 'web-client/src/types'
import { createStore, defaultPlugins, mount, defaultStoreMockOptions } from 'web-test-helpers'
import { LoadingService } from 'web-pkg'
import { LoadingService, LoadingTaskCallbackArguments } from 'web-pkg'

const Component = {
template: '<div></div>',
Expand Down Expand Up @@ -87,7 +87,7 @@ function getWrapper({
space: { driveType, isEditor: () => false, isManager: () => false },
$loadingService: mock<LoadingService>({
addTask: (callback) => {
return callback({ setProgress: jest.fn() })
return callback(mock<LoadingTaskCallbackArguments>())
}
}),
$client: {
Expand Down
15 changes: 12 additions & 3 deletions packages/web-app-files/tests/unit/mixins/actions/restore.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import Restore from 'web-app-files/src/mixins/actions/restore'
import { createLocationTrash, createLocationSpaces } from 'web-app-files/src/router'
import { mockDeep } from 'jest-mock-extended'
import { mock, mockDeep } from 'jest-mock-extended'
import { OwnCloudSdk } from 'web-client/src/types'
import { createStore, defaultPlugins, mount, defaultStoreMockOptions } from 'web-test-helpers'
import { LoadingTaskCallbackArguments } from 'web-pkg'

const Component = {
template: '<div></div>',
Expand Down Expand Up @@ -46,7 +47,11 @@ describe('restore', () => {
const { wrapper } = getWrapper()
const showMessageStub = jest.spyOn(wrapper.vm, 'showMessage')
const removeFilesFromTrashbinStub = jest.spyOn(wrapper.vm, 'removeFilesFromTrashbin')
await wrapper.vm.$_restore_restoreResources([{ id: '1', path: '/1' }], [], jest.fn())
await wrapper.vm.$_restore_restoreResources(
[{ id: '1', path: '/1' }],
[],
mock<LoadingTaskCallbackArguments>()
)

expect(showMessageStub).toHaveBeenCalledTimes(1)
expect(removeFilesFromTrashbinStub).toHaveBeenCalledTimes(1)
Expand All @@ -58,7 +63,11 @@ describe('restore', () => {
const { wrapper } = getWrapper({ resolveClearTrashBin: false })
const showMessageStub = jest.spyOn(wrapper.vm, 'showMessage')
const removeFilesFromTrashbinStub = jest.spyOn(wrapper.vm, 'removeFilesFromTrashbin')
await wrapper.vm.$_restore_restoreResources([{ id: '1', path: '/1' }], [], jest.fn())
await wrapper.vm.$_restore_restoreResources(
[{ id: '1', path: '/1' }],
[],
mock<LoadingTaskCallbackArguments>()
)

expect(showMessageStub).toHaveBeenCalledTimes(1)
expect(removeFilesFromTrashbinStub).toHaveBeenCalledTimes(0)
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 42547e5

Please sign in to comment.