-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added mappings for each resource across versions
- Loading branch information
Showing
27 changed files
with
452 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"compilerOptions": { | ||
"module": "commonjs", | ||
"target": "es2016", | ||
"jsx": "preserve" | ||
}, | ||
"exclude": ["node_modules", "**/node_modules/*"], | ||
"js-import.excludeFilesToScan": "packages/*/{es,lib,build}/**" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,77 @@ | ||
import { createStore } from 'iiif-redux'; | ||
import { doesResourceExist } from 'iiif-redux/es/api/dereferenced'; | ||
import { iiifResourceRequestUnknownAsync } from 'iiif-redux/es/spaces/iiif-resource'; | ||
import { mappings as presentation2Mapping } from 'iiif-redux/es/api/2.x'; | ||
import { mappings as presentation3Mapping } from 'iiif-redux/es/api/3.x'; | ||
import DataLoader from 'dataloader'; | ||
|
||
const store = createStore(); | ||
|
||
const iiifLoader = new DataLoader(keys => | ||
Promise.all( | ||
keys.map(id => store.dispatch(iiifResourceRequestUnknownAsync(id))) | ||
) | ||
); | ||
|
||
function mapping(version) { | ||
if (version === 3) { | ||
return presentation3Mapping; | ||
} | ||
return presentation2Mapping; | ||
} | ||
|
||
function select(selector) { | ||
return selector(store.getState()); | ||
} | ||
|
||
async function loadResource(id, type) { | ||
return await iiifLoader.load(id); | ||
} | ||
|
||
const selectById = (id, type) => { | ||
return state => state.resources[type][id]; | ||
}; | ||
|
||
function resourceExists(id, type) { | ||
return select(doesResourceExist(selectById(id, type), type)); | ||
} | ||
|
||
function fieldExists(id, type, field) { | ||
const value = getFieldValue(id, type, field); | ||
|
||
return typeof value !== 'undefined' && value !== null; | ||
} | ||
|
||
function resourceDereferenced(id, type) {} | ||
|
||
function getFieldValue(id, type, field) { | ||
const version = getPresentationVersionFromResource( | ||
select(selectById(id, type)) | ||
); | ||
const api = mapping(version)(selectById(id, type)); | ||
const singleField = api[field]; | ||
return select(singleField); | ||
} | ||
|
||
const combinedResolver = (id, type, field) => { | ||
if (!resourceExists(id, type)) { | ||
return loadResource(id, type).then(() => combinedResolver(id, type, field)); | ||
} | ||
if (!fieldExists(id, type, field)) { | ||
if (resourceDereferenced(id, type)) { | ||
return null; | ||
} | ||
return loadResource(id, type).then(() => combinedResolver(id, type, field)); | ||
} | ||
return getFieldValue(id, type, field); | ||
}; | ||
|
||
const context = req => ({ | ||
store, | ||
query: selector => { | ||
return selector(store.getState()); | ||
}, | ||
combinedResolver, | ||
}); | ||
|
||
export default context; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.