Skip to content

Commit

Permalink
Refactor: Move loading concept data to utils.js and improve
Browse files Browse the repository at this point in the history
  • Loading branch information
stefandesu committed Nov 18, 2024
1 parent fc1ae11 commit c63e710
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
6 changes: 3 additions & 3 deletions src/App.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<script setup>
import { ref, watch, computed } from "vue"
import { LoadingIndicator, Modal } from "jskos-vue"
import { getSubjects, getTitleName, sortSuggestionMappings, suggestionsToPica, getMappingsForSubjects } from "./utils.js"
import { getSubjects, getTitleName, sortSuggestionMappings, suggestionsToPica, getMappingsForSubjects, getConceptData } from "./utils.js"
import * as jskos from "jskos-tools"
Expand Down Expand Up @@ -105,7 +105,7 @@ watch(() => state.ppn, async (ppn) => {
if (!scheme.API?.length || !scheme._registry) {
return
}
const concepts = await scheme._registry.getConcepts({ concepts: subjects.map(subject => ({ uri: subject.uri, inScheme: [scheme] })) })
const concepts = await getConceptData({ concepts: subjects, scheme })
concepts.forEach(concept => {
const subject = subjects.find(s => jskos.compare(s, concept))
if (subject) {
Expand Down Expand Up @@ -188,7 +188,7 @@ watch(() => state.ppn, async (ppn) => {
}
let concepts
try {
concepts = await scheme._registry.getConcepts({ concepts: conceptsToLoad.map(subject => ({ uri: subject.uri, inScheme: [scheme] })) })
concepts = await getConceptData({ concepts: conceptsToLoad, scheme })
} catch (error) {
console.warn(`Could not load concept data for ${jskos.notation(scheme)} concepts`, error)
return []
Expand Down
24 changes: 21 additions & 3 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ export async function getSubjects(ppn) {
return subjects
}

async function runInParallelAndCombineResults(promises) {
return (await Promise.all(promises)).reduce((prev, cur) => prev.concat(cur), [])
}
const maxLength = 600 // Prevent URL length issues (very conservative value)

export async function getMappingsForSubjects(subjects) {
if (!subjects.length) {
return []
Expand All @@ -65,8 +70,7 @@ export async function getMappingsForSubjects(subjects) {
toScheme = state.schemes.map(s => s.uri).join("|"),
cardinality = "1-to-1",
configs = [],
limit = 500,
maxLength = 600 // Prevent URL length issues (very conservative value)
limit = 500
let current = []

for (const subject of subjects.concat(null)) {
Expand All @@ -92,5 +96,19 @@ export async function getMappingsForSubjects(subjects) {
}
current.push(subject)
}
return (await Promise.all(configs.map(config => concordanceRegistry.getMappings(config)))).reduce((prev, cur) => prev.concat(cur), [])
return runInParallelAndCombineResults(configs.map(config => concordanceRegistry.getMappings(config)))
}

export async function getConceptData({ concepts, scheme }) {
const promises = []
let current = []
for (const concept of concepts.concat(null)) {
const length = current.map(c => c.uri).join("|").length
if (length >= maxLength || (concept === null && current.length)) {
promises.push(scheme._registry.getConcepts({ concepts: current.map(concept => ({ uri: concept.uri, inScheme: [scheme] })) }))
current = []
}
current.push(concept)
}
return await runInParallelAndCombineResults(promises)
}

0 comments on commit c63e710

Please sign in to comment.