Skip to content

Commit

Permalink
Adjustments to "registryForScheme" (#64)
Browse files Browse the repository at this point in the history
Can now be used for other data types, but only returns providers that support concepts by default (for backward compatibility).
  • Loading branch information
stefandesu committed Nov 24, 2023
1 parent 5256f35 commit 78e708e
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions src/lib/CocodaSDK.js
Original file line number Diff line number Diff line change
Expand Up @@ -405,18 +405,26 @@ export default class CocodaSDK {
return jskos.sortSchemes(schemes.filter(Boolean))
}

registryForScheme(scheme) {
/**
*
* @param {Object} scheme JSKOS concept scheme object
* @param {string} [dataType="concepts"] only use providers that support a certain data type (default is "concepts" for backward compatibility)
* @returns
*/
registryForScheme(scheme, dataType = "concepts") {
let registry = scheme._registry
if (registry) {
return registry
}

for (let { type, ...config } of scheme.API || []) {
const url = config.url
// Use type AND url for caching because the same API URL might be used for multiple API types
const cacheKey = `${type}-${url}`

if (registryCache[url]) {
if (registryCache[cacheKey]) {
// Registry in cache is used
const registry = registryCache[url]
const registry = registryCache[cacheKey]
// Check if scheme is part of registry already; if not, add it
if (Array.isArray(registry._jskos.schemes) && !jskos.isContainedIn(scheme, registry._jskos.schemes)) {
registry._jskos.schemes.push(scheme)
Expand All @@ -425,14 +433,17 @@ export default class CocodaSDK {
} else {
// Some providers need access to the scheme
config.scheme = scheme
// Multiple providers may implement a certain API, so we're looping through providers
// Multiple providers may implement a certain API, so we're looping through providers and returning the first that works
for (const provider of Object.values(providers)) {
if (provider?.providerType !== type) {
continue
}
if (!provider._registryConfigForBartocApiConfig) {
continue
}
if (dataType && !provider?.supports?.[dataType]) {
continue
}
// Get registry config from provider
const providerName = provider.providerName
const registryConfig = providers[providerName]._registryConfigForBartocApiConfig(config)
Expand All @@ -444,7 +455,7 @@ export default class CocodaSDK {
try {
registry = this.initializeRegistry(registryConfig)
if (registry) {
registryCache[url] = registry
registryCache[cacheKey] = registry
return registry
}
} catch (error) {
Expand Down

0 comments on commit 78e708e

Please sign in to comment.