[BUGFIX] Avoid race condition in JS Icon API 70/54470/3
authorTymoteusz Motylewski <t.motylewski@gmail.com>
Tue, 24 Oct 2017 19:46:44 +0000 (21:46 +0200)
committerFrank Naegler <frank.naegler@typo3.org>
Fri, 27 Oct 2017 14:35:42 +0000 (16:35 +0200)
Now promise is cached instead of promise result, to avoid race condition
when requesting the same icon multiple times.
Now API will do just one ajax call per icon, even if the other API call
is made before the first one finishes.

Resolves: #82851
Releases: master, 8.7
Change-Id: I4110f84d32c7f1856c1664a02d3c9a2ba59168a6
Reviewed-on: https://review.typo3.org/54470
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Andreas Wolf <andreas.wolf@typo3.org>
Reviewed-by: Andreas Fernandez <typo3@scripting-base.de>
Reviewed-by: Stefan Neufeind <typo3.neufeind@speedpartner.de>
Tested-by: Stefan Neufeind <typo3.neufeind@speedpartner.de>
Reviewed-by: Frank Naegler <frank.naegler@typo3.org>
Tested-by: Frank Naegler <frank.naegler@typo3.org>
typo3/sysext/backend/Resources/Public/JavaScript/Icons.js

index 9b15b01..0ba0541 100644 (file)
@@ -105,20 +105,19 @@ define(['jquery'], function($) {
                var icon = [identifier, size, overlayIdentifier, state, markupIdentifier],
                        cacheIdentifier = icon.join('_');
 
-               if (Icons.isCached(cacheIdentifier)) {
-                       return Icons.getFromCache(cacheIdentifier);
+               if (!Icons.isCached(cacheIdentifier)) {
+                       Icons.putInCache(cacheIdentifier, $.ajax({
+                               url: TYPO3.settings.ajaxUrls['icons'],
+                               dataType: 'html',
+                               data: {
+                                       icon: JSON.stringify(icon)
+                               },
+                               success: function(markup) {
+                                       return markup;
+                               }
+                       }).promise());
                }
-
-               return $.ajax({
-                       url: TYPO3.settings.ajaxUrls['icons'],
-                       dataType: 'html',
-                       data: {
-                               icon: JSON.stringify(icon)
-                       },
-                       success: function(markup) {
-                               Icons.putInCache(cacheIdentifier, markup);
-                       }
-               });
+               return Icons.getFromCache(cacheIdentifier).done();
        };
 
        /**