From e53f05bd90b9cc721db4de41c884563cd9561115 Mon Sep 17 00:00:00 2001 From: diego0020 Date: Sun, 7 Oct 2018 17:11:53 -0500 Subject: [PATCH 1/2] add missing jsdocs --- src/imageCache.js | 70 +++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 59 insertions(+), 11 deletions(-) diff --git a/src/imageCache.js b/src/imageCache.js index 4f9031f8..e1246880 100644 --- a/src/imageCache.js +++ b/src/imageCache.js @@ -15,7 +15,12 @@ const imageCacheDict = {}; // Array of cachedImage objects export const cachedImages = []; -export function setMaximumSizeBytes (numBytes) { +/** 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'); } @@ -29,8 +34,11 @@ export function setMaximumSizeBytes (numBytes) { purgeCacheIfNecessary(); } - -function 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) { return; @@ -38,7 +46,7 @@ function purgeCacheIfNecessary () { // Cache size has been exceeded, create list of images sorted by timeStamp // So we can purge the least recently used image - function compare (a, b) { + function compare(a, b) { if (a.timeStamp > b.timeStamp) { return -1; } @@ -65,7 +73,13 @@ function purgeCacheIfNecessary () { triggerEvent(events, EVENTS.IMAGE_CACHE_FULL, cacheInfo); } -export function putImageLoadObject (imageId, imageLoadObject) { +/** + * 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 + */ +export function putImageLoadObject(imageId, imageLoadObject) { if (imageId === undefined) { throw new Error('putImageLoadObject: imageId must not be undefined'); } @@ -128,7 +142,12 @@ export function putImageLoadObject (imageId, imageLoadObject) { }); } -export function getImageLoadObject (imageId) { +/** + * Retuns the object that is loading a given imageId + * + * @param {string} imageId + */ +export function getImageLoadObject(imageId) { if (imageId === undefined) { throw new Error('getImageLoadObject: imageId must not be undefined'); } @@ -144,7 +163,12 @@ export function getImageLoadObject (imageId) { return cachedImage.imageLoadObject; } -export function removeImageLoadObject (imageId) { +/** + * Removes the image loader associated with a given Id from the cache + * + * @param {string} imageId + */ +export function removeImageLoadObject(imageId) { if (imageId === undefined) { throw new Error('removeImageLoadObject: imageId must not be undefined'); } @@ -168,7 +192,17 @@ export function removeImageLoadObject (imageId) { delete imageCacheDict[imageId]; } -export function getCacheInfo () { +/** + * @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 + */ + +/** + * Gets the current state of the cache + */ +export function getCacheInfo() { return { maximumSizeInBytes, cacheSizeInBytes, @@ -178,7 +212,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. -function decache (imageLoadObject) { +/** + * INTERNAL: Removes and ImageLoader from the cache + * + * @param {Object} imageLoadObject + */ +function decache(imageLoadObject) { imageLoadObject.promise.then( function () { if (imageLoadObject.decache) { @@ -193,7 +232,10 @@ function decache (imageLoadObject) { ); } -export function purgeCache () { +/** + * Removes all images from cache + */ +export function purgeCache() { while (cachedImages.length > 0) { const removedCachedImage = cachedImages[0]; @@ -201,7 +243,13 @@ export function purgeCache () { } } -export function changeImageIdCacheSize (imageId, newCacheSize) { +/** + * Updates the space than an image is using in the cache + * + * @param {string} imageId + * @param {number} newCacheSize + */ +export function changeImageIdCacheSize(imageId, newCacheSize) { const cacheEntry = imageCacheDict[imageId]; if (cacheEntry) { From 692241681c6b66e309899cc06d2ce68a6e0ece29 Mon Sep 17 00:00:00 2001 From: diego0020 Date: Sun, 7 Oct 2018 17:20:54 -0500 Subject: [PATCH 2/2] eslint --- src/imageCache.js | 45 +++++++++++++++++++++++---------------------- 1 file changed, 23 insertions(+), 22 deletions(-) diff --git a/src/imageCache.js b/src/imageCache.js index e1246880..117afe74 100644 --- a/src/imageCache.js +++ b/src/imageCache.js @@ -15,12 +15,12 @@ const imageCacheDict = {}; // Array of cachedImage objects export const cachedImages = []; -/** Sets the maximum size of cache and purges cache contents if necessary. - * +/** 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) { +export function setMaximumSizeBytes (numBytes) { if (numBytes === undefined) { throw new Error('setMaximumSizeBytes: parameter numBytes must not be undefined'); } @@ -34,11 +34,12 @@ export function setMaximumSizeBytes(numBytes) { purgeCacheIfNecessary(); } + /** * Purges the cache if size exceeds maximum * @returns {void} */ -function purgeCacheIfNecessary() { +function purgeCacheIfNecessary () { // If max cache size has not been exceeded, do nothing if (cacheSizeInBytes <= maximumSizeInBytes) { return; @@ -46,7 +47,7 @@ function purgeCacheIfNecessary() { // Cache size has been exceeded, create list of images sorted by timeStamp // So we can purge the least recently used image - function compare(a, b) { + function compare (a, b) { if (a.timeStamp > b.timeStamp) { return -1; } @@ -75,11 +76,11 @@ function purgeCacheIfNecessary() { /** * 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 */ -export function putImageLoadObject(imageId, imageLoadObject) { +export function putImageLoadObject (imageId, imageLoadObject) { if (imageId === undefined) { throw new Error('putImageLoadObject: imageId must not be undefined'); } @@ -144,10 +145,10 @@ export function putImageLoadObject(imageId, imageLoadObject) { /** * Retuns the object that is loading a given imageId - * - * @param {string} imageId + * + * @param {string} imageId */ -export function getImageLoadObject(imageId) { +export function getImageLoadObject (imageId) { if (imageId === undefined) { throw new Error('getImageLoadObject: imageId must not be undefined'); } @@ -165,10 +166,10 @@ export function getImageLoadObject(imageId) { /** * Removes the image loader associated with a given Id from the cache - * - * @param {string} imageId + * + * @param {string} imageId */ -export function removeImageLoadObject(imageId) { +export function removeImageLoadObject (imageId) { if (imageId === undefined) { throw new Error('removeImageLoadObject: imageId must not be undefined'); } @@ -202,7 +203,7 @@ export function removeImageLoadObject(imageId) { /** * Gets the current state of the cache */ -export function getCacheInfo() { +export function getCacheInfo () { return { maximumSizeInBytes, cacheSizeInBytes, @@ -214,10 +215,10 @@ export function getCacheInfo() { // The one that knows how to deal with shared cache keys and cache size. /** * INTERNAL: Removes and ImageLoader from the cache - * - * @param {Object} imageLoadObject + * + * @param {Object} imageLoadObject */ -function decache(imageLoadObject) { +function decache (imageLoadObject) { imageLoadObject.promise.then( function () { if (imageLoadObject.decache) { @@ -235,7 +236,7 @@ function decache(imageLoadObject) { /** * Removes all images from cache */ -export function purgeCache() { +export function purgeCache () { while (cachedImages.length > 0) { const removedCachedImage = cachedImages[0]; @@ -245,11 +246,11 @@ export function purgeCache() { /** * Updates the space than an image is using in the cache - * - * @param {string} imageId - * @param {number} newCacheSize + * + * @param {string} imageId + * @param {number} newCacheSize */ -export function changeImageIdCacheSize(imageId, newCacheSize) { +export function changeImageIdCacheSize (imageId, newCacheSize) { const cacheEntry = imageCacheDict[imageId]; if (cacheEntry) {