Skip to content

Commit

Permalink
Fix clearing of account status cache. (vufind-org#4200)
Browse files Browse the repository at this point in the history
There was a possibility for the _load method to retrieve old data before the actual function that requested the cache to be cleared was performed.

(cherry picked from commit 7ee4132)
  • Loading branch information
EreMaijala committed Jan 22, 2025
1 parent 5379b1c commit 1faf91e
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -278,15 +278,14 @@ public function testGlobalCacheClearing()
$session = $this->login();
// Seed some fines
$this->setJSStorage(['fines' => ['value' => 30.5, 'display' => '$30.50']]);
// Check storage
$storage = $this->getJSStorage();
$this->assertNotNull($storage['fines']);
// Clear all cache
// Clear all cached data
$session->evaluateScript('VuFind.account.clearCache();');
// Wait for reload
$this->waitForPageLoad($this->getMinkSession()->getPage());
// Check storage again
$storage = $this->getJSStorage();
// Status code MISSING is -2 * Math.PI, but we just round it here to avoid trouble
$this->assertEquals(-6, ceil($storage['fines']));
$this->assertNull($storage['fines']);
}

/**
Expand Down
16 changes: 15 additions & 1 deletion themes/bootstrap3/js/account_ajax.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,15 @@ VuFind.register('account', function Account() {
// Forward declaration for clearAllCaches
var clearAllCaches = function clearAllCachesForward() {};

// Clearing save forces AJAX update next page load
/**
* Clear the specified client data cache; pass in empty/undefined value to clear
* all caches. Note that clearing all caches will prevent further data from loading
* on the current page, and should only be performed when exiting the page via a
* link, form submission, etc. Cleared data will be reloaded by AJAX on the next
* page load.
*
* @param {string|undefined} name Cache to clear (undefined/empty for all)
*/
var clearCache = function clearCache(name) {
if (typeof name === "undefined" || name === '') {
clearAllCaches();
Expand Down Expand Up @@ -114,6 +122,7 @@ VuFind.register('account', function Account() {
var _load = function _load(module) {
if (_clearCaches) {
sessionStorage.removeItem(_sessionDataPrefix + module);
return;
}
var $element = $(_submodules[module].selector);
if (!$element) {
Expand Down Expand Up @@ -179,6 +188,11 @@ VuFind.register('account', function Account() {
}
};

/**
* Clear all account status data cached in the client's browser. This will prevent future data from
* loading on the current page and should only be called when exiting the page by clicking a link,
* submitting a form, etc.
*/
clearAllCaches = function clearAllCachesReal() {
// Set a flag so that any modules yet to be loaded are cleared as well
_clearCaches = true;
Expand Down
16 changes: 15 additions & 1 deletion themes/bootstrap5/js/account_ajax.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,15 @@ VuFind.register('account', function Account() {
// Forward declaration for clearAllCaches
var clearAllCaches = function clearAllCachesForward() {};

// Clearing save forces AJAX update next page load
/**
* Clear the specified client data cache; pass in empty/undefined value to clear
* all caches. Note that clearing all caches will prevent further data from loading
* on the current page, and should only be performed when exiting the page via a
* link, form submission, etc. Cleared data will be reloaded by AJAX on the next
* page load.
*
* @param {string|undefined} name Cache to clear (undefined/empty for all)
*/
var clearCache = function clearCache(name) {
if (typeof name === "undefined" || name === '') {
clearAllCaches();
Expand Down Expand Up @@ -116,6 +124,7 @@ VuFind.register('account', function Account() {
var _load = function _load(module) {
if (_clearCaches) {
sessionStorage.removeItem(_sessionDataPrefix + module);
return;
}
var $element = $(_submodules[module].selector);
if (!$element) {
Expand Down Expand Up @@ -181,6 +190,11 @@ VuFind.register('account', function Account() {
}
};

/**
* Clear all account status data cached in the client's browser. This will prevent future data from
* loading on the current page and should only be called when exiting the page by clicking a link,
* submitting a form, etc.
*/
clearAllCaches = function clearAllCachesReal() {
// Set a flag so that any modules yet to be loaded are cleared as well
_clearCaches = true;
Expand Down

0 comments on commit 1faf91e

Please sign in to comment.