better code

Massimo Melina committed Apr 3, 2023 at 20:30 UTC 42b64cc64a4163042244b77e545d54a479f4e7ac
2 files changed +16 -12
frontend/src/useFetchList.ts
+11 -12
@@ -3,7 +3,7 @@
3 import { state, useSnapState } from './state'
4 import { useEffect, useRef } from 'react'
5 import { apiEvents } from './api'
6 -import { DirEntry, DirList } from './BrowseFiles'
6 +import { DirList } from './BrowseFiles'
7 import _ from 'lodash'
8 import { subscribeKey } from 'valtio/utils'
9 import { useIsMounted } from 'usehooks-ts'
@@ -69,7 +69,7 @@ export default function useFetchList() {
69 const flush = () => {
70 const chunk = buffer.splice(0, Infinity)
71 if (chunk.length)
72 - state.list = sort([...state.list, ...chunk.map(precalculate)])
72 + state.list = sort([...state.list, ...chunk])
73 }
74 const timer = setInterval(flush, 1000)
75 const src = apiEvents('file_list', baseParams, (type, data) => {
@@ -114,7 +114,16 @@ export default function useFetchList() {
114 const { add } = entry
115 if (add) {
116 add.uri = pathEncode(add.n)
117 + add.isFolder = add.n.endsWith('/')
118 + if (add.isFolder) {
119 + const i = add.n.lastIndexOf('.') + 1
120 + add.ext = i ? add.n.substring(i) : ''
121 + }
122 + const t = add.m || add.c
123 + if (t)
124 + add.t = new Date(t)
125 add.name = add.n.slice(add.n.lastIndexOf('/', -1) +1, add.isFolder ? -1 : Infinity)
126 +
127 buffer.push(add)
128 }
129 }
@@ -157,16 +166,6 @@ function sort(list: DirList) {
166 )
167 }
168
160 -function precalculate(rec:DirEntry) {
161 - const i = rec.n.lastIndexOf('.') + 1
162 - rec.ext = i ? rec.n.substring(i) : ''
163 - rec.isFolder = rec.n.endsWith('/')
164 - const t = rec.m || rec.c
165 - if (t)
166 - rec.t = new Date(t)
167 - return rec
168 -}
169 -
169 // generic comparison
170 function compare(a:any, b:any) {
171 return a < b ? -1 : a > b ? 1 : 0
shared/index.ts
+5
@@ -139,3 +139,8 @@ export function formatPerc(p: number) {
139 export function wantArray<T>(x?: void | T | T[]) {
140 return x == null ? [] : Array.isArray(x) ? x : [x]
141 }
142 +
143 +export function _log(...args: any[]) {
144 + console.log('**', ...args)
145 + return args[args.length-1]
146 +}
\ No newline at end of file