better code

Massimo Melina committed Apr 20, 2023 at 23:16 UTC acc3261357d2d4a6edb878a1da3c5fba56f5db78
4 files changed +19 -24
admin/src/VfsPage.ts
+5 -6
@@ -30,12 +30,11 @@ export default function VfsPage() {
30 const { vfs, selectedFiles } = useSnapState()
31 const { data, reload, element } = useApiEx('get_vfs')
32 useMemo(() => vfs || reload(), [vfs, reload])
33 - const anyMask = useMemo(() => {
34 - return recur(vfs)
35 - function recur(node: typeof vfs) {
36 - return !_.isEmpty(node?.masks) || node?.children?.some(recur)
37 - }
38 - }, [vfs])
33 + const anyMask = useMemo(() =>
34 + (function someMask(node: typeof vfs) {
35 + return !_.isEmpty(node?.masks) || node?.children?.some(someMask)
36 + })(vfs),
37 + [vfs])
38 const sideBreakpoint = 'md'
39 const isSideBreakpoint = useBreakpoint(sideBreakpoint)
40 const [status] = useApi('get_status')
src/api.vfs.ts
+1 -1
@@ -59,7 +59,7 @@ const apis: ApiHandlers = {
59 let byMasks = node.original && _.pickBy(node, (v,k) =>
60 v !== (node.original as any)[k] // something is changing me...
61 && v !== (node.parent as any)[k] // ...and it's not inheritance...
62 - && PERM_KEYS.includes(k)) // ...must be masks. Please limit this to perms
62 + && PERM_KEYS.includes(k as any)) // ...must be masks. Please limit this to perms
63 if (_.isEmpty(byMasks))
64 byMasks = undefined
65 return {
src/plugins.ts
+4 -6
@@ -316,17 +316,15 @@ function deleteModule(id: string) {
316 for (const child of wantArray(cache[k]?.children))
317 getOrSet(requiredBy, child.id, ()=> [] as string[]).push(k)
318 const deleted: string[] = []
319 - recur(id)
320 -
321 - function recur(id: string) {
322 - let mod = cache[id]
319 + ;(function deleteCache(id: string) {
320 + const mod = cache[id]
321 if (!mod) return
322 delete cache[id]
323 deleted.push(id)
324 for (const child of mod.children)
325 if (! _.difference(requiredBy[child.id], deleted).length)
328 - recur(child.id)
329 - }
326 + deleteCache(child.id)
327 + })(id)
328 }
329
330 onProcessExit(() =>
src/vfs.ts
+9 -11
@@ -53,7 +53,7 @@ export const defaultPerms: VfsPerm = {
53 can_delete: WHO_NO_ONE,
54 }
55
56 -export const PERM_KEYS = Object.keys(defaultPerms)
56 +export const PERM_KEYS = typedKeys(defaultPerms)
57
58 export const MIME_AUTO = 'auto'
59
@@ -323,19 +323,17 @@ function renameUnderPath(rename:undefined | Record<string,string>, path: string)
323 }
324
325 events.on('accountRenamed', (from, to) => {
326 - recur(vfs)
327 - saveVfs()
328 -
329 - function recur(n: VfsNode) {
330 - for (const k of typedKeys(defaultPerms))
331 - replace(n[k])
326 + ;(function renameInNode(n: VfsNode) {
327 + for (const k of PERM_KEYS)
328 + renameInPerm(n[k])
329
330 if (n.masks)
334 - Object.values(n.masks).forEach(recur)
335 - n.children?.forEach(recur)
336 - }
331 + Object.values(n.masks).forEach(renameInNode)
332 + n.children?.forEach(renameInNode)
333 + })(vfs)
334 + saveVfs()
335
338 - function replace(a?: Who) {
336 + function renameInPerm(a?: Who) {
337 if (!Array.isArray(a)) return
338 for (let i=0; i < a.length; i++)
339 if (a[i] === from)