From 3d3b078e60b729ba23c9d5381058f8bb6f920e5c Mon Sep 17 00:00:00 2001 From: Leo Vasanko Date: Mon, 13 Nov 2023 14:48:43 +0000 Subject: [PATCH] Refactor replacing Document type with Doc class with computed properties. Switched Doc storage to shallowRefs for extra performance and to keep the Doc class included. --- frontend/src/components/FileExplorer.vue | 55 +++++++++------------ frontend/src/components/FileModified.vue | 4 +- frontend/src/components/FileRenameInput.vue | 6 +-- frontend/src/components/FileSize.vue | 4 +- frontend/src/repositories/Document.ts | 29 ++++++++--- frontend/src/stores/documents.ts | 26 ++++------ 6 files changed, 63 insertions(+), 61 deletions(-) diff --git a/frontend/src/components/FileExplorer.vue b/frontend/src/components/FileExplorer.vue index 3a0cdb0..593e305 100644 --- a/frontend/src/components/FileExplorer.vue +++ b/frontend/src/components/FileExplorer.vue @@ -79,28 +79,28 @@ diff --git a/frontend/src/components/FileRenameInput.vue b/frontend/src/components/FileRenameInput.vue index aaa6977..6f1aff2 100644 --- a/frontend/src/components/FileRenameInput.vue +++ b/frontend/src/components/FileRenameInput.vue @@ -12,7 +12,7 @@ diff --git a/frontend/src/repositories/Document.ts b/frontend/src/repositories/Document.ts index 022b260..cae8774 100644 --- a/frontend/src/repositories/Document.ts +++ b/frontend/src/repositories/Document.ts @@ -1,17 +1,34 @@ +import { formatSize, formatUnixDate, haystackFormat } from "@/utils" + export type FUID = string -export type Document = { +export type DocProps = { loc: string name: string key: FUID size: number - sizedisp: string mtime: number - modified: string - haystack: string dir: boolean } +export class Doc { + private _name: string = "" + public loc: string = "" + public key: FUID = "" + public size: number = 0 + public mtime: number = 0 + public haystack: string = "" + public dir: boolean = false + + constructor(props: Partial = {}) { Object.assign(this, props) } + get name() { return this._name } + set name(name: string) { + this._name = name + this.haystack = haystackFormat(name) + } + get sizedisp(): string { return formatSize(this.size) } + get modified(): string { return formatUnixDate(this.mtime) } +} export type errorEvent = { error: { code: number @@ -36,7 +53,7 @@ export type UpdateEntry = ['k', number] | ['d', number] | ['i', Array // Helper structure for selections export interface SelectedItems { keys: FUID[] - docs: Record - recursive: Array<[string, string, Document]> + docs: Record + recursive: Array<[string, string, Doc]> missing: Set } diff --git a/frontend/src/stores/documents.ts b/frontend/src/stores/documents.ts index 7ea40da..71f2af3 100644 --- a/frontend/src/stores/documents.ts +++ b/frontend/src/stores/documents.ts @@ -1,14 +1,11 @@ -import type { Document, FileEntry, FUID, SelectedItems } from '@/repositories/Document' -import { formatSize, formatUnixDate, haystackFormat } from '@/utils' +import type { FileEntry, FUID, SelectedItems } from '@/repositories/Document' +import { Doc } from '@/repositories/Document' import { defineStore } from 'pinia' import { collator } from '@/utils' import { logoutUser } from '@/repositories/User' import { watchConnect } from '@/repositories/WS' +import { shallowRef } from 'vue' -type FileData = { id: string; mtime: number; size: number; dir: DirectoryData } -type DirectoryData = { - [filename: string]: FileData -} type User = { username: string privileged: boolean @@ -19,7 +16,7 @@ type User = { export const useDocumentStore = defineStore({ id: 'documents', state: () => ({ - document: [] as Document[], + document: shallowRef([]), selected: new Set(), fileExplorer: null as any, error: '' as string, @@ -38,20 +35,17 @@ export const useDocumentStore = defineStore({ let loc = [] as string[] for (const [level, name, key, mtime, size, isfile] of root) { loc = loc.slice(0, level - 1) - docs.push({ + docs.push(new Doc({ name, loc: level ? loc.join('/') : '/', key, size, - sizedisp: formatSize(size), mtime, - modified: formatUnixDate(mtime), - haystack: haystackFormat(name), dir: !isfile, - }) + })) loc.push(name) } - this.document = docs as Document[] + this.document = docs }, login(username: string, privileged: boolean) { this.user.username = username @@ -75,12 +69,12 @@ export const useDocumentStore = defineStore({ isUserLogged(): boolean { return this.user.isLoggedIn }, - recentDocuments(): Document[] { + recentDocuments(): Doc[] { const ret = [...this.document] ret.sort((a, b) => b.mtime - a.mtime) return ret }, - largeDocuments(): Document[] { + largeDocuments(): Doc[] { const ret = [...this.document] ret.sort((a, b) => b.size - a.size) return ret @@ -105,7 +99,7 @@ export const useDocumentStore = defineStore({ for (const key of selected) if (!found.has(key)) ret.missing.add(key) // Build a flat list including contents recursively const relnames = new Set() - function add(rel: string, full: string, doc: Document) { + function add(rel: string, full: string, doc: Doc) { if (!doc.dir && relnames.has(rel)) throw Error(`Multiple selections conflict for: ${rel}`) relnames.add(rel) ret.recursive.push([rel, full, doc])