-
Notifications
You must be signed in to change notification settings - Fork 159
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11000 from owncloud/feat/restore-web-worker
feat: web worker for resource restore
- Loading branch information
Showing
11 changed files
with
511 additions
and
174 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { HttpError } from '../errors' | ||
|
||
/** | ||
* Create a HttpError based on a given message, status code and x-request-id. | ||
*/ | ||
export const createHttpError = ({ | ||
message, | ||
statusCode, | ||
xReqId | ||
}: { | ||
message: string | ||
statusCode: number | ||
xReqId: string | ||
}) => { | ||
const response = new Response(undefined, { | ||
headers: { 'x-request-id': xReqId }, | ||
status: statusCode | ||
}) | ||
return new HttpError(message, response, statusCode) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from './deleteWorker' | ||
export * from './pasteWorker' | ||
export * from './restoreWorker' | ||
export * from './tokenTimerWorker' |
1 change: 1 addition & 0 deletions
1
packages/web-pkg/src/composables/webWorkers/restoreWorker/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './useRestoreWorker' |
93 changes: 93 additions & 0 deletions
93
packages/web-pkg/src/composables/webWorkers/restoreWorker/useRestoreWorker.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import { unref } from 'vue' | ||
import { useWebWorkersStore } from '../../piniaStores/webWorkers' | ||
import { | ||
HttpError, | ||
createHttpError, | ||
type Resource, | ||
type SpaceResource | ||
} from '@ownclouders/web-client' | ||
import { useConfigStore } from '../../piniaStores' | ||
import { useLoadingService } from '../../loadingService' | ||
import { useRequestHeaders } from '../../requestHeaders' | ||
import RestoreWorker from './worker?worker' | ||
|
||
export type RestoreWorkerReturnData = { | ||
successful: Resource[] | ||
failed: { resource: Resource; message: string; statusCode: number; xReqId: string }[] | ||
} | ||
|
||
type CallbackOptions = { | ||
successful: Resource[] | ||
failed: { resource: Resource; error: HttpError }[] | ||
} | ||
|
||
/** | ||
* Web worker for restoring deleted resources from the trash bin. | ||
*/ | ||
export const useRestoreWorker = () => { | ||
const configStore = useConfigStore() | ||
const loadingService = useLoadingService() | ||
const { headers } = useRequestHeaders() | ||
const { createWorker, terminateWorker } = useWebWorkersStore() | ||
|
||
const startWorker = ( | ||
{ | ||
space, | ||
resources, | ||
missingFolderPaths | ||
}: { space: SpaceResource; resources: Resource[]; missingFolderPaths: string[] }, | ||
callback: (result: CallbackOptions) => void | ||
) => { | ||
const worker = createWorker<RestoreWorkerReturnData>(RestoreWorker as unknown as string, { | ||
needsTokenRenewal: true | ||
}) | ||
|
||
let resolveLoading: (value: unknown) => void | ||
|
||
unref(worker.worker).onmessage = (e: MessageEvent) => { | ||
terminateWorker(worker.id) | ||
resolveLoading?.(true) | ||
|
||
const { successful, failed } = JSON.parse(e.data) as RestoreWorkerReturnData | ||
|
||
// construct http error based on the parsed error data | ||
const failedWithErrors = failed.map(({ resource, ...errorData }) => { | ||
return { resource, error: createHttpError(errorData) } | ||
}) | ||
|
||
callback({ successful, failed: failedWithErrors }) | ||
} | ||
|
||
loadingService.addTask( | ||
() => | ||
new Promise((res) => { | ||
resolveLoading = res | ||
}) | ||
) | ||
|
||
worker.post(getWorkerData({ space, resources, missingFolderPaths })) | ||
} | ||
|
||
const getWorkerData = ({ | ||
space, | ||
resources, | ||
missingFolderPaths | ||
}: { | ||
space: SpaceResource | ||
resources: Resource[] | ||
missingFolderPaths: string[] | ||
}) => { | ||
return JSON.stringify({ | ||
topic: 'startProcess', | ||
data: { | ||
space, | ||
resources, | ||
missingFolderPaths, | ||
baseUrl: configStore.serverUrl, | ||
headers: unref(headers) | ||
} | ||
}) | ||
} | ||
|
||
return { startWorker } | ||
} |
Oops, something went wrong.