Selaa lähdekoodia

refactor: Return record from loadModels

Owen Diffey 1 vuosi sitten
vanhempi
commit
3c90b52820

+ 4 - 1
frontend/src/components/UserLink.vue

@@ -15,7 +15,10 @@ const { loadModels } = useModels();
 
 onMounted(() => {
 	socket.onConnect(async () => {
-		const [model] = await loadModels("minifiedUsers", props.userId);
+		const { [props.userId]: model } = await loadModels(
+			"minifiedUsers",
+			props.userId
+		);
 
 		if (model) user.value = model;
 	});

+ 31 - 7
frontend/src/composables/useModels.ts

@@ -111,17 +111,41 @@ export const useModels = () => {
 		modelIds: string | string[],
 		relations?: Record<string, string | string[]>
 	) => {
-		const registeredModels = (
-			await modelStore.loadModels(modelName, modelIds, relations)
-		).filter(model => model !== null);
+		modelIds = Array.isArray(modelIds) ? modelIds : [modelIds];
 
-		if (registerModels.length === 0) return registeredModels;
+		const missingModelIds = modelIds.filter(
+			modelId =>
+				!models.value.find(
+					model => model._id === modelId && model._name === modelName
+				)
+		);
+		const existingModels = Object.fromEntries(
+			models.value
+				.filter(model => modelIds.includes(model._id))
+				.map(model => [model._id, model])
+		);
 
-		models.value.push(...registeredModels);
+		if (relations)
+			await forEachIn(Object.values(existingModels), async model =>
+				model.loadRelations(relations)
+			);
 
-		await setupDeletedSubscriptions(registeredModels);
+		if (Object.keys(existingModels).length === modelIds.length)
+			return existingModels;
 
-		return registeredModels;
+		const loadedModels = await modelStore.loadModels(
+			modelName,
+			missingModelIds,
+			relations
+		);
+
+		const missingModels = Object.values(loadedModels).filter(
+			missingModel => !!missingModel
+		);
+		models.value.push(...missingModels);
+		await setupDeletedSubscriptions(missingModels);
+
+		return Object.assign(loadedModels, existingModels);
 	};
 
 	const unregisterModels = async (modelIds: string[]) => {

+ 20 - 5
frontend/src/stores/model.ts

@@ -293,16 +293,31 @@ export const useModelStore = defineStore("model", () => {
 		const modelIds = Array.isArray(modelIdsOrModelId)
 			? modelIdsOrModelId
 			: [modelIdsOrModelId];
+		const existingModels = models.value.filter(model =>
+			modelIds.includes(model._id)
+		);
 		const missingModelIds = modelIds.filter(
 			modelId => !loadedModelIds.value.includes(`${modelName}.${modelId}`)
 		);
-		const missingModels = Object.values(
-			await findManyById(modelName, missingModelIds)
+
+		const fetchedModels = await findManyById(modelName, missingModelIds);
+		const registeredModels = await registerModels(
+			Object.values(fetchedModels)
+				.filter(model => !!model)
+				.concat(existingModels),
+			relations
 		);
-		await registerModels(missingModels, relations);
+		const modelsNotFound = modelIds
+			.filter(
+				modelId =>
+					!registeredModels.find(model => model._id === modelId)
+			)
+			.map(modelId => [modelId, null]);
 
-		return models.value.filter(
-			model => modelIds.includes(model._id) && model._name === modelName
+		return Object.fromEntries(
+			registeredModels
+				.map(model => [model._id, model])
+				.concat(modelsNotFound)
 		);
 	};