Skip to content

Commit

Permalink
perf(files): Cache getContents function used for uploader
Browse files Browse the repository at this point in the history
Instead of trigger a PROPFIND for every new-menu entry clicks,
or conflict handling of uploads, we can just use the cached content from the file store.
If we do not have any cache entry we fetch new, but otherwise this is not needed.

Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>
  • Loading branch information
susnux committed Aug 1, 2024
1 parent f645c9b commit 77d834e
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 13 deletions.
2 changes: 1 addition & 1 deletion apps/files/src/components/NewNodeDialog.vue
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ function submit() {
}
// Reset local name on props change
watch(() => props.defaultName, () => {
watch(() => [props.defaultName, props.otherNames], () => {
localDefaultName.value = getUniqueName(props.defaultName, props.otherNames)
})
Expand Down
2 changes: 1 addition & 1 deletion apps/files/src/newMenu/newFolder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export const entry = {
emit('files:node:created', folder)
window.OCP.Files.Router.goToRoute(
null, // use default route
{ view: 'files', fileid: folder.fileid },
{ view: 'files', fileid: String(folder.fileid) },
{ dir: context.path },
)
}
Expand Down
28 changes: 28 additions & 0 deletions apps/files/src/store/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import logger from '../logger'
import Vue from 'vue'

import { client } from '../services/WebdavClient.ts'
import { usePathsStore } from './paths.ts'

const fetchNode = async (node: Node): Promise<Node> => {
const propfindPayload = davGetDefaultPropfind()
Expand Down Expand Up @@ -63,6 +64,33 @@ export const useFilesStore = function(...args) {
},

actions: {
/**
* Get cached nodes within a given path
*
* @param service The service (files view)
* @param path The path relative within the service
* @returns Array of cached nodes within the path
*/
getNodesByPath(service: string, path?: string): Node[] {
const pathsStore = usePathsStore()
let folder: Folder | undefined

// Get the containing folder from path store
if (!path || path === '/') {
folder = this.getRoot(service)
} else {
const source = pathsStore.getPath(service, path)
if (source) {
folder = this.getNode(source) as Folder | undefined
}
}

// If we found a cache entry and the cache entry was already loaded (has children) then use it
return (folder?._children ?? [])
.map((source: string) => this.getNode(source))
.filter(Boolean)
},

updateNodes(nodes: Node[]) {
// Update the store all at once
const files = nodes.reduce((acc, node) => {
Expand Down
18 changes: 7 additions & 11 deletions apps/files/src/views/FilesList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,12 @@ export default defineComponent({
return async (path?: string) => {
// as the path is allowed to be undefined we need to normalize the path ('//' to '/')
const normalizedPath = normalize(`${this.currentFolder?.path ?? ''}/${path ?? ''}`)
// Try cache first
const nodes = this.filesStore.getNodesByPath(view.id, path)
if (nodes.length > 0) {
return nodes
}
// If not found in the files store (cache)
// use the current view to fetch the content for the requested path
return (await view.getContents(normalizedPath)).contents
}
Expand Down Expand Up @@ -279,7 +285,7 @@ export default defineComponent({
dirContents(): Node[] {
return (this.currentFolder?._children || [])
.map(this.getNode)
.map(this.filesStore.getNode)
.filter((node: Node) => !!node)
},
Expand Down Expand Up @@ -531,16 +537,6 @@ export default defineComponent({
},
/**
* Get a cached note from the store
*
* @param {number} fileId the file id to get
* @return {Folder|File}
*/
getNode(fileId) {
return this.filesStore.getNode(fileId)
},
/**
* Handle the node deleted event to reset open file
* @param node The deleted node
Expand Down

0 comments on commit 77d834e

Please sign in to comment.