Skip to content

Commit 597b14c

Browse files
anonrigkeithwillcodezomars
authored
perf: improve application list execution (#8301)
* perf: improve application list execution * Removed unused interface * Type fix --------- Co-authored-by: Keith Williams <[email protected]> Co-authored-by: zomars <[email protected]>
1 parent 2bdd6df commit 597b14c

File tree

1 file changed

+32
-45
lines changed
  • packages/trpc/server/routers/viewer

1 file changed

+32
-45
lines changed

packages/trpc/server/routers/viewer/apps.tsx

Lines changed: 32 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,6 @@ import { TRPCError } from "@trpc/server";
1212

1313
import { authedAdminProcedure, authedProcedure, router } from "../../trpc";
1414

15-
interface FilteredApp {
16-
name: string;
17-
slug: string;
18-
logo: string;
19-
title?: string;
20-
type: string;
21-
description: string;
22-
dirName: string;
23-
keys: Prisma.JsonObject | null;
24-
enabled: boolean;
25-
isTemplate?: boolean;
26-
}
27-
2815
export const appsRouter = router({
2916
listLocal: authedAdminProcedure
3017
.input(
@@ -34,9 +21,7 @@ export const appsRouter = router({
3421
)
3522
.query(async ({ ctx, input }) => {
3623
const category = input.category === "conferencing" ? "video" : input.category;
37-
const localApps = getLocalAppMetadata().filter(
38-
(app) => app.categories?.some((appCategory) => appCategory === category) || app.category === category
39-
);
24+
const localApps = getLocalAppMetadata();
4025

4126
const dbApps = await ctx.prisma.app.findMany({
4227
where: {
@@ -52,56 +37,58 @@ export const appsRouter = router({
5237
},
5338
});
5439

55-
const filteredApps: FilteredApp[] = [];
40+
return localApps.flatMap((app) => {
41+
// Filter applications that does not belong to the current requested category.
42+
if (!(app.category === category || app.categories?.some((appCategory) => appCategory === category))) {
43+
return [];
44+
}
5645

57-
for (const app of localApps) {
5846
// Find app metadata
5947
const dbData = dbApps.find((dbApp) => dbApp.slug === app.slug);
6048

6149
// If the app already contains keys then return
6250
if (dbData?.keys) {
63-
filteredApps.push({
51+
return {
6452
name: app.name,
6553
slug: app.slug,
6654
logo: app.logo,
6755
title: app.title,
6856
type: app.type,
6957
description: app.description,
7058
// We know that keys are going to be an object or null. Prisma can not type check against JSON fields
71-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
72-
//@ts-ignore
73-
keys: dbData.keys,
59+
keys: dbData.keys as Prisma.JsonObject | null,
7460
dirName: app.dirName || app.slug,
7561
enabled: dbData?.enabled || false,
7662
isTemplate: app.isTemplate,
77-
});
78-
} else {
79-
const keysSchema = appKeysSchemas[app.dirName as keyof typeof appKeysSchemas];
63+
};
64+
}
8065

81-
const keys: Record<string, string> = {};
66+
const keysSchema = appKeysSchemas[app.dirName as keyof typeof appKeysSchemas];
8267

83-
if (typeof keysSchema !== "undefined") {
84-
Object.values(keysSchema.keyof()._def.values).reduce((keysObject, key) => {
85-
keys[key as string] = "";
86-
return keysObject;
87-
}, {} as Record<string, string>);
88-
}
68+
const keys: Record<string, string> = {};
8969

90-
filteredApps.push({
91-
name: app.name,
92-
slug: app.slug,
93-
logo: app.logo,
94-
type: app.type,
95-
title: app.title,
96-
description: app.description,
97-
enabled: dbData?.enabled || false,
98-
dirName: app.dirName || app.slug,
99-
keys: Object.keys(keys).length === 0 ? null : keys,
100-
});
70+
// `typeof val === 'undefined'` is always slower than !== undefined comparison
71+
// it is important to avoid string to string comparisons as much as we can
72+
if (keysSchema !== undefined) {
73+
// TODO: Remove the Object.values and reduce to improve the performance.
74+
Object.values(keysSchema.keyof()._def.values).reduce((keysObject, key) => {
75+
keys[key as string] = "";
76+
return keysObject;
77+
}, {} as Record<string, string>);
10178
}
102-
}
10379

104-
return filteredApps;
80+
return {
81+
name: app.name,
82+
slug: app.slug,
83+
logo: app.logo,
84+
type: app.type,
85+
title: app.title,
86+
description: app.description,
87+
enabled: dbData?.enabled ?? false,
88+
dirName: app.dirName ?? app.slug,
89+
keys: Object.keys(keys).length === 0 ? null : keys,
90+
};
91+
});
10592
}),
10693
toggle: authedAdminProcedure
10794
.input(

0 commit comments

Comments
 (0)