Skip to content

Commit

Permalink
chore(docs): Add some additional jsdocs to ImageCache (cornerstonejs#314
Browse files Browse the repository at this point in the history
)

* add missing jsdocs

* eslint

* fix babelrc

* missing names
  • Loading branch information
diego0020 authored and swederik committed Oct 15, 2018
1 parent a1f0caf commit a943ea0
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
57 changes: 57 additions & 0 deletions src/imageCache.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ const imageCacheDict = {};
// Array of cachedImage objects
export const cachedImages = [];

/** Sets the maximum size of cache and purges cache contents if necessary.
*
* @param {number} numBytes The maximun size that the cache should occupy.
* @returns {void}
*/
export function setMaximumSizeBytes (numBytes) {
if (numBytes === undefined) {
throw new Error('setMaximumSizeBytes: parameter numBytes must not be undefined');
Expand All @@ -30,6 +35,10 @@ export function setMaximumSizeBytes (numBytes) {
purgeCacheIfNecessary();
}

/**
* Purges the cache if size exceeds maximum
* @returns {void}
*/
function purgeCacheIfNecessary () {
// If max cache size has not been exceeded, do nothing
if (cacheSizeInBytes <= maximumSizeInBytes) {
Expand Down Expand Up @@ -65,6 +74,13 @@ function purgeCacheIfNecessary () {
triggerEvent(events, EVENTS.IMAGE_CACHE_FULL, cacheInfo);
}

/**
* Puts a new image loader into the cache
*
* @param {string} imageId ImageId of the image loader
* @param {Object} imageLoadObject The object that is loading or loaded the image
* @returns {void}
*/
export function putImageLoadObject (imageId, imageLoadObject) {
if (imageId === undefined) {
throw new Error('putImageLoadObject: imageId must not be undefined');
Expand Down Expand Up @@ -128,6 +144,12 @@ export function putImageLoadObject (imageId, imageLoadObject) {
});
}

/**
* Retuns the object that is loading a given imageId
*
* @param {string} imageId Image ID
* @returns {void}
*/
export function getImageLoadObject (imageId) {
if (imageId === undefined) {
throw new Error('getImageLoadObject: imageId must not be undefined');
Expand All @@ -144,6 +166,12 @@ export function getImageLoadObject (imageId) {
return cachedImage.imageLoadObject;
}

/**
* Removes the image loader associated with a given Id from the cache
*
* @param {string} imageId Image ID
* @returns {void}
*/
export function removeImageLoadObject (imageId) {
if (imageId === undefined) {
throw new Error('removeImageLoadObject: imageId must not be undefined');
Expand All @@ -168,6 +196,18 @@ export function removeImageLoadObject (imageId) {
delete imageCacheDict[imageId];
}

/**
* @typedef {Object} CacheInformation
* @property {number} maximumSizeInBytes The maximum size of the cache in bytes
* @property {number} cacheSizeInBytes Currently occupied space in the cache in bytes
* @property {number} numberOfImagesCached Number of ImageLoaders in the cache
* @returns {void}
*/

/**
* Gets the current state of the cache
* @returns {void}
*/
export function getCacheInfo () {
return {
maximumSizeInBytes,
Expand All @@ -178,6 +218,12 @@ export function getCacheInfo () {

// This method should only be called by `removeImageLoadObject` because it's
// The one that knows how to deal with shared cache keys and cache size.
/**
* INTERNAL: Removes and ImageLoader from the cache
*
* @param {Object} imageLoadObject Image Loader Object to remove
* @returns {void}
*/
function decache (imageLoadObject) {
imageLoadObject.promise.then(
function () {
Expand All @@ -193,6 +239,10 @@ function decache (imageLoadObject) {
);
}

/**
* Removes all images from cache
* @returns {void}
*/
export function purgeCache () {
while (cachedImages.length > 0) {
const removedCachedImage = cachedImages[0];
Expand All @@ -201,6 +251,13 @@ export function purgeCache () {
}
}

/**
* Updates the space than an image is using in the cache
*
* @param {string} imageId Image ID
* @param {number} newCacheSize New image size
* @returns {void}
*/
export function changeImageIdCacheSize (imageId, newCacheSize) {
const cacheEntry = imageCacheDict[imageId];

Expand Down
2 changes: 1 addition & 1 deletion src/internal/drawImage.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Internal API function to draw an image to a given enabled element
*
* @param {EnabledElement} enabledElement The Cornerstone Enabled Element to redraw
* @param {Boolean} [invalidated] - true if pixel data has been invalidated and cached rendering should not be used
* @param {Boolean} [invalidated = false] - true if pixel data has been invalidated and cached rendering should not be used
* @returns {void}
* @memberof Internal
*/
Expand Down

0 comments on commit a943ea0

Please sign in to comment.