This repository has been archived by the owner on Aug 15, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from dannyrb/fix/server-request-options-config…
…uration Fix/server request options configuration
- Loading branch information
Showing
3 changed files
with
88 additions
and
47 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,15 @@ | ||
// import { api } from 'dicomweb-client' | ||
|
||
const api = { | ||
DICOMwebClient: jest.fn().mockImplementation(() => { | ||
return { | ||
retrieveStudyMetadata: jest.fn().mockResolvedValue([]), | ||
} | ||
}), | ||
} | ||
|
||
export default { | ||
api, | ||
} | ||
|
||
export { api } |
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,66 +1,57 @@ | ||
import log from '../log.js'; | ||
import RetrieveMetadata from './services/wado/retrieveMetadata.js'; | ||
import RetrieveMetadata from './services/wado/retrieveMetadata.js' | ||
|
||
// Define the StudyMetaDataPromises object. This is used as a cache to store study meta data | ||
// promises and prevent unnecessary subsequent calls to the server | ||
const StudyMetaDataPromises = new Map(); | ||
const module = 'RetrieveStudyMetadata' | ||
// Cache for promises. Prevents unnecessary subsequent calls to the server | ||
const StudyMetaDataPromises = new Map() | ||
|
||
/** | ||
* Delete the cached study metadata retrieval promise to ensure that the browser will | ||
* re-retrieve the study metadata when it is next requested | ||
* | ||
* @param {String} studyInstanceUid The UID of the Study to be removed from cache | ||
* | ||
*/ | ||
export function deleteStudyMetadataPromise(studyInstanceUid) { | ||
if (StudyMetaDataPromises.has(studyInstanceUid)) { | ||
StudyMetaDataPromises.delete(studyInstanceUid); | ||
} | ||
} | ||
|
||
/** | ||
* Retrieves study metadata using a server call | ||
* Retrieves study metadata | ||
* | ||
* @param {String} studyInstanceUid The UID of the Study to be retrieved | ||
* @param {Object} server | ||
* @param {string} studyInstanceUid The UID of the Study to be retrieved | ||
* @returns {Promise} that will be resolved with the metadata or rejected with the error | ||
*/ | ||
export function retrieveStudyMetadata( | ||
server, | ||
studyInstanceUid, | ||
seriesInstanceUids | ||
) { | ||
export function retrieveStudyMetadata(server, studyInstanceUid) { | ||
// @TODO: Whenever a study metadata request has failed, its related promise will be rejected once and for all | ||
// and further requests for that metadata will always fail. On failure, we probably need to remove the | ||
// corresponding promise from the "StudyMetaDataPromises" map... | ||
|
||
// If the StudyMetaDataPromises cache already has a pending or resolved promise related to the | ||
// given studyInstanceUid, then that promise is returned | ||
if (StudyMetaDataPromises.has(studyInstanceUid)) { | ||
return StudyMetaDataPromises.get(studyInstanceUid); | ||
if (!server) { | ||
throw new Error(`${module}: Required 'server' parameter not provided.`) | ||
} | ||
if (!studyInstanceUid) { | ||
throw new Error( | ||
`${module}: Required 'studyInstanceUid' parameter not provided.` | ||
) | ||
} | ||
|
||
const seriesKeys = Array.isArray(seriesInstanceUids) | ||
? '|' + seriesInstanceUids.join('|') | ||
: ''; | ||
const timingKey = `retrieveStudyMetadata[${studyInstanceUid}${seriesKeys}]`; | ||
log.time(timingKey); | ||
// Already waiting on result? Return cached promise | ||
if (StudyMetaDataPromises.has(studyInstanceUid)) { | ||
return StudyMetaDataPromises.get(studyInstanceUid) | ||
} | ||
|
||
// Create a promise to handle the data retrieval | ||
const promise = new Promise((resolve, reject) => { | ||
// If no study metadata is in the cache variable, we need to retrieve it from | ||
// the server with a call. | ||
if ( | ||
server.type === 'dicomWeb' && | ||
server.requestOptions.requestFromBrowser === true | ||
) { | ||
RetrieveMetadata(server, studyInstanceUid).then(function(data) { | ||
resolve(data); | ||
}, reject); | ||
} | ||
}); | ||
RetrieveMetadata(server, studyInstanceUid).then(function(data) { | ||
resolve(data) | ||
}, reject) | ||
}) | ||
|
||
// Store the promise in cache | ||
StudyMetaDataPromises.set(studyInstanceUid, promise); | ||
StudyMetaDataPromises.set(studyInstanceUid, promise) | ||
|
||
return promise | ||
} | ||
|
||
return promise; | ||
/** | ||
* Delete the cached study metadata retrieval promise to ensure that the browser will | ||
* re-retrieve the study metadata when it is next requested | ||
* | ||
* @param {String} studyInstanceUid The UID of the Study to be removed from cache | ||
* | ||
*/ | ||
export function deleteStudyMetadataPromise(studyInstanceUid) { | ||
if (StudyMetaDataPromises.has(studyInstanceUid)) { | ||
StudyMetaDataPromises.delete(studyInstanceUid) | ||
} | ||
} |
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,35 @@ | ||
import { retrieveStudyMetadata } from './retrieveStudyMetadata.js' | ||
|
||
const fakeDicomWebServer = {} | ||
|
||
// Testing Promises: https://jestjs.io/docs/en/asynchronous#promises | ||
describe('retrieveStudyMetadata.js', () => { | ||
it('throws an exception if no server parameter is provided', () => { | ||
const callWithNoServer = () => { | ||
retrieveStudyMetadata(null, 'fake-study-instance-uid') | ||
} | ||
|
||
expect(callWithNoServer).toThrow(Error) | ||
}) | ||
|
||
it('throws an exception if no studyInstanceUid parameter is provided', () => { | ||
const callWithNoStudyInstanceUid = () => { | ||
retrieveStudyMetadata(fakeDicomWebServer, null) | ||
} | ||
|
||
expect(callWithNoStudyInstanceUid).toThrow(Error) | ||
}) | ||
|
||
it('caches and returns the same promise for identical studyInstanceUIDs', () => { | ||
const firstPromise = retrieveStudyMetadata( | ||
fakeDicomWebServer, | ||
'fake-study-instance-uid' | ||
) | ||
const secondPromise = retrieveStudyMetadata( | ||
fakeDicomWebServer, | ||
'fake-study-instance-uid' | ||
) | ||
|
||
expect(firstPromise).toBe(secondPromise) | ||
}) | ||
}) |