@@ -6,7 +6,6 @@ import type { ContentsWithRoot, File, Folder } from '@nextcloud/files'
66import type { FileStat , ResponseDataDetailed } from 'webdav'
77
88import { getDefaultPropfind , getRootPath , resultToNode } from '@nextcloud/files/dav'
9- import { CancelablePromise } from 'cancelable-promise'
109import { join } from 'path'
1110import logger from '../logger.ts'
1211import { useFilesStore } from '../store/files.ts'
@@ -20,66 +19,55 @@ import { searchNodes } from './WebDavSearch.ts'
2019 * This also allows to fetch local search results when the user is currently filtering.
2120 *
2221 * @param path - The path to query
22+ * @param options - Options
23+ * @param options.signal - Abort signal to cancel the request
2324 */
24- export function getContents ( path = '/' ) : CancelablePromise < ContentsWithRoot > {
25- const controller = new AbortController ( )
25+ export async function getContents ( path = '/' , options : { signal : AbortSignal } ) : Promise < ContentsWithRoot > {
2626 const searchStore = useSearchStore ( getPinia ( ) )
2727
28- if ( searchStore . query . length >= 3 ) {
29- return new CancelablePromise ( ( resolve , reject , cancel ) => {
30- cancel ( ( ) => controller . abort ( ) )
31- getLocalSearch ( path , searchStore . query , controller . signal )
32- . then ( resolve )
33- . catch ( reject )
34- } )
35- } else {
36- return defaultGetContents ( path )
28+ if ( searchStore . query . length < 3 ) {
29+ return await defaultGetContents ( path , options )
3730 }
31+
32+ return await getLocalSearch ( path , searchStore . query , options . signal )
3833}
3934
4035/**
4136 * Generic `getContents` implementation for the users files.
4237 *
4338 * @param path - The path to get the contents
39+ * @param options - Options
40+ * @param options.signal - Abort signal to cancel the request
4441 */
45- export function defaultGetContents ( path : string ) : CancelablePromise < ContentsWithRoot > {
42+ export async function defaultGetContents ( path : string , options : { signal : AbortSignal } ) : Promise < ContentsWithRoot > {
4643 path = join ( getRootPath ( ) , path )
47- const controller = new AbortController ( )
4844 const propfindPayload = getDefaultPropfind ( )
4945
50- return new CancelablePromise ( async ( resolve , reject , onCancel ) => {
51- onCancel ( ( ) => controller . abort ( ) )
46+ const contentsResponse = await client . getDirectoryContents ( path , {
47+ details : true ,
48+ data : propfindPayload ,
49+ includeSelf : true ,
50+ signal : options . signal ,
51+ } ) as ResponseDataDetailed < FileStat [ ] >
5252
53- try {
54- const contentsResponse = await client . getDirectoryContents ( path , {
55- details : true ,
56- data : propfindPayload ,
57- includeSelf : true ,
58- signal : controller . signal ,
59- } ) as ResponseDataDetailed < FileStat [ ] >
53+ const root = contentsResponse . data [ 0 ]
54+ const contents = contentsResponse . data . slice ( 1 )
55+ if ( root ?. filename !== path && `${ root ?. filename } /` !== path ) {
56+ logger . debug ( `Exepected "${ path } " but got filename "${ root . filename } " instead.` )
57+ throw new Error ( 'Root node does not match requested path' )
58+ }
6059
61- const root = contentsResponse . data [ 0 ]
62- const contents = contentsResponse . data . slice ( 1 )
63- if ( root ?. filename !== path && `${ root ?. filename } /` !== path ) {
64- logger . debug ( `Exepected "${ path } " but got filename "${ root . filename } " instead.` )
65- throw new Error ( 'Root node does not match requested path' )
60+ return {
61+ folder : resultToNode ( root ) as Folder ,
62+ contents : contents . map ( ( result ) => {
63+ try {
64+ return resultToNode ( result )
65+ } catch ( error ) {
66+ logger . error ( `Invalid node detected '${ result . basename } '` , { error } )
67+ return null
6668 }
67-
68- resolve ( {
69- folder : resultToNode ( root ) as Folder ,
70- contents : contents . map ( ( result ) => {
71- try {
72- return resultToNode ( result )
73- } catch ( error ) {
74- logger . error ( `Invalid node detected '${ result . basename } '` , { error } )
75- return null
76- }
77- } ) . filter ( Boolean ) as File [ ] ,
78- } )
79- } catch ( error ) {
80- reject ( error )
81- }
82- } )
69+ } ) . filter ( Boolean ) as File [ ] ,
70+ }
8371}
8472
8573/**
0 commit comments