Skip to content

Commit

Permalink
fix: 🐛 Handle failed loads when fetching data (#107)
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesAPetts authored Aug 14, 2020
1 parent 3ec7c89 commit d025262
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
16 changes: 14 additions & 2 deletions src/lib/getImageData.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,13 @@ export default function getImageData(imageIds, displaySetInstanceUid) {
});
};

const _publishAllPixelDataInseted = () => {
const _publishPixelDataInsertedError = error => {
imageDataObject.subscriptions.onPixelDataInsertedError.forEach(callback => {
callback(error);
});
};

const _publishAllPixelDataInserted = () => {
imageDataObject.subscriptions.onAllPixelDataInserted.forEach(callback => {
callback();
});
Expand All @@ -90,6 +96,7 @@ export default function getImageData(imageIds, displaySetInstanceUid) {
// Remove all subscriptions on completion.
imageDataObject.subscriptions = {
onPixelDataInserted: [],
onPixelDataInsertedError: [],
onAllPixelDataInserted: [],
};
};
Expand All @@ -108,16 +115,21 @@ export default function getImageData(imageIds, displaySetInstanceUid) {
loaded: false,
subscriptions: {
onPixelDataInserted: [],
onPixelDataInsertedError: [],
onAllPixelDataInserted: [],
},
onPixelDataInserted: callback => {
imageDataObject.subscriptions.onPixelDataInserted.push(callback);
},
onPixelDataInsertedError: callback => {
imageDataObject.subscriptions.onPixelDataInsertedError.push(callback);
},
onAllPixelDataInserted: callback => {
imageDataObject.subscriptions.onAllPixelDataInserted.push(callback);
},
_publishPixelDataInserted,
_publishAllPixelDataInseted,
_publishAllPixelDataInserted,
_publishPixelDataInsertedError,
};

imageDataCache.set(displaySetInstanceUid, imageDataObject);
Expand Down
24 changes: 18 additions & 6 deletions src/lib/loadImageData.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,16 @@ export default function loadImageDataProgressively(imageDataObject) {
const reRenderFraction = numberOfFrames / 5;
let reRenderTarget = reRenderFraction;

const insertPixelDataErrorHandler = error => {
numberProcessed++;
imageDataObject._publishPixelDataInsertedError(error);

if (numberProcessed === numberOfFrames) {
// Done loading, publish complete and remove all subscriptions.
imageDataObject._publishAllPixelDataInserted();
}
};

const insertPixelData = image => {
const { imagePositionPatient } = metaDataMap.get(image.imageId);

Expand Down Expand Up @@ -93,27 +103,29 @@ export default function loadImageDataProgressively(imageDataObject) {

if (numberProcessed === numberOfFrames) {
// Done loading, publish complete and remove all subscriptions.
imageDataObject._publishAllPixelDataInseted();
imageDataObject._publishAllPixelDataInserted();
}
};

prefetchImageIds(imageIds, imageDataObject, insertPixelData);
prefetchImageIds(imageIds, insertPixelData, insertPixelDataErrorHandler);
}

const requestType = 'prefetch';
const preventCache = false;

function prefetchImageIds(imageIds, imageDataObject, insertPixelData) {
const noop = () => {};

function prefetchImageIds(
imageIds,
insertPixelData,
insertPixelDataErrorHandler
) {
imageIds.forEach(imageId => {
requestPoolManager.addRequest(
{},
imageId,
requestType,
preventCache,
insertPixelData,
noop
insertPixelDataErrorHandler
);
});

Expand Down

0 comments on commit d025262

Please sign in to comment.