Skip to content
This repository has been archived by the owner on Aug 15, 2019. It is now read-only.

Commit

Permalink
Merge pull request #19 from dannyrb/fix/server-request-options-config…
Browse files Browse the repository at this point in the history
…uration

Fix/server request options configuration
  • Loading branch information
dannyrb authored May 9, 2019
2 parents 0dc3ef4 + 3aeb559 commit 7ff17f2
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 47 deletions.
15 changes: 15 additions & 0 deletions src/__mocks__/dicomweb-client.js
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 }
85 changes: 38 additions & 47 deletions src/studies/retrieveStudyMetadata.js
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)
}
}
35 changes: 35 additions & 0 deletions src/studies/retrieveStudyMetadata.test.js
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)
})
})

0 comments on commit 7ff17f2

Please sign in to comment.