jenkins-bot has submitted this change and it was merged.

Change subject: mw.widgets.CategorySelector: Add client-side caching
......................................................................


mw.widgets.CategorySelector: Add client-side caching

The key in the cache hash is the string to be searched and
the number of the searchTypes appended as a string. This
feels a bit hacky but it'll do the job and doesn't seem
wrong.

Change-Id: Idfc931e825aaa30e00b57445d6c4afd67857609e
---
M resources/src/mediawiki.widgets/mw.widgets.CategorySelector.js
1 file changed, 15 insertions(+), 7 deletions(-)

Approvals:
  Bartosz Dziewoński: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/resources/src/mediawiki.widgets/mw.widgets.CategorySelector.js 
b/resources/src/mediawiki.widgets/mw.widgets.CategorySelector.js
index 7fdef25..267eebb 100644
--- a/resources/src/mediawiki.widgets/mw.widgets.CategorySelector.js
+++ b/resources/src/mediawiki.widgets/mw.widgets.CategorySelector.js
@@ -65,6 +65,7 @@
 
                // Initialize
                this.api = config.api || new mw.Api();
+               this.searchCache = {};
        }
 
        /* Setup */
@@ -269,7 +270,13 @@
         * @return {jQuery.Promise} Resolves with an array of categories
         */
        CSP.searchCategories = function ( input, searchType ) {
-               var deferred = $.Deferred();
+               var deferred = $.Deferred(),
+                       cacheKey = input + searchType.toString();
+
+               // Check cache
+               if ( this.searchCache[ cacheKey ] !== undefined ) {
+                       return this.searchCache[ cacheKey ];
+               }
 
                switch ( searchType ) {
                        case CategorySelector.SearchType.OpenSearch:
@@ -363,12 +370,10 @@
                                        var categories = [];
 
                                        $.each( res.query.pages, function ( 
index, page ) {
-                                               if ( !page.missing ) {
-                                                       if ( $.isArray( 
page.categories ) ) {
-                                                               
categories.push.apply( categories, page.categories.map( function ( category ) {
-                                                                       return 
category.title;
-                                                               } ) );
-                                                       }
+                                               if ( !page.missing && 
$.isArray( page.categories ) ) {
+                                                       categories.push.apply( 
categories, page.categories.map( function ( category ) {
+                                                               return 
category.title;
+                                                       } ) );
                                                }
                                        } );
 
@@ -380,6 +385,9 @@
                                throw new Error( 'Unknown searchType' );
                }
 
+               // Cache the result
+               this.searchCache[ cacheKey ] = deferred.promise();
+
                return deferred.promise();
        };
 

-- 
To view, visit https://gerrit.wikimedia.org/r/310533
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: Idfc931e825aaa30e00b57445d6c4afd67857609e
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Prtksxna <psax...@wikimedia.org>
Gerrit-Reviewer: Bartosz Dziewoński <matma....@gmail.com>
Gerrit-Reviewer: Jack Phoenix <j...@countervandalism.net>
Gerrit-Reviewer: Jforrester <jforres...@wikimedia.org>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to