r97176 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r97175‎ | r97176 | r97177 >
Date:16:29, 15 September 2011
Author:catrope
Status:ok
Tags:
Comment:
RL2: Followup r97154, factor out the common code in getForeignGadgetsData() and getForeignGadgetCategories() as promised. These functions work now, except that non-shared gadgets from foreign repositories are not being filtered yet, but I guess that's fine and we can do that filtering in the caller.
Modified paths:
  • /branches/RL2/extensions/Gadgets/modules/ext.gadgets.api.js (modified) (history)

Diff [purge]

Index: branches/RL2/extensions/Gadgets/modules/ext.gadgets.api.js
@@ -83,6 +83,38 @@
8484 }
8585 }
8686
 87+ /**
 88+ * Call an asynchronous function for each repository, and merge
 89+ * their return values into an object keyed by repository name.
 90+ * @param getter function( success, error, repoName ), called for each repo to get the data
 91+ * @param success function( data ), called when all data has successfully been retrieved
 92+ * @param error function( error ), called if one of the getter calls called its error callback
 93+ */
 94+ function mergeRepositoryData( getter, success, error ) {
 95+ var combined = {}, successes = 0, numRepos = 0, repo;
 96+ // Find out how many repos there are
 97+ // Needs to be in a separate loop because we have to have the final number ready
 98+ // before we fire the first potentially (since it could be cached) async request
 99+ for ( repo in mw.gadgets.conf.repos ) {
 100+ numRepos++;
 101+ }
 102+
 103+ // Use $.each instead of a for loop so we can access repoName in the success callback
 104+ // without annoying issues
 105+ $.each( mw.gadgets.conf.repos, function( repoName, repoData ) {
 106+ getter(
 107+ function( data ) {
 108+ combined[repoName] = data;
 109+ if ( ++successes === numRepos ) {
 110+ success( combined );
 111+ }
 112+ }, function( errorCode ) {
 113+ error( errorCode );
 114+ }, repoName
 115+ );
 116+ } );
 117+ }
 118+
87119 /* Public functions */
88120
89121 mw.gadgets = {
@@ -99,26 +131,10 @@
100132 * @param error {Function} To be called with a string (error code) as first argument.
101133 */
102134 getForeignGadgetsData: function( success, error ) {
103 - var combined = {}, successes = 0, numRepos = 0, repo;
104 - // Find out how many repos there are
105 - // Needs to be in a separate loop because we have to have the final number ready
106 - // before we fire the first potentially (since it could be cached) async request
107 - for ( repo in mw.gadgets.conf.repos ) {
108 - numRepos++;
109 - }
110 -
111 - for ( repo in mw.gadgets.conf.repos ) {
112 - mw.gadgets.api.getGadgetData( null,
113 - function( data ) {
114 - combined[repo] = data;
115 - if ( ++successes === numRepos ) {
116 - success( combined );
117 - }
118 - }, function( errorCode ) {
119 - error( errorCode );
120 - }, repo
121 - );
122 - }
 135+ mergeRepositoryData(
 136+ function( s, e, repoName ) { mw.gadgets.api.getGadgetData( null, s, e, repoName ); },
 137+ success, error
 138+ );
123139 },
124140
125141 /**
@@ -128,28 +144,8 @@
129145 * @param success {Function} To be called with an object of arrays of category objects, keyed by repository name, as first argument.
130146 * @param error {Function} To be called with a string (error code) as the first argument.
131147 */
132 - getForeignGadgetCategories: function( success, error ){
133 - // TODO: Almost entirely duplicated from the function above. Factoring this out is easy
134 - var combined = {}, successes = 0, numRepos = 0, repo;
135 - // Find out how many repos there are
136 - // Needs to be in a separate loop because we have to have the final number ready
137 - // before we fire the first async request
138 - for ( repo in mw.gadgets.conf.repos ) {
139 - numRepos++;
140 - }
141 -
142 - for ( repo in mw.gadgets.conf.repos ) {
143 - mw.gadgets.api.getGadgetCategories(
144 - function( data ) {
145 - combined[repo] = data;
146 - if ( ++successes === numRepos ) {
147 - success( combined );
148 - }
149 - }, function( errorCode ) {
150 - error( errorCode );
151 - }, repo
152 - );
153 - }
 148+ getForeignGadgetCategories: function( success, error ) {
 149+ mergeRepositoryData( mw.gadgets.api.getGadgetCategories, success, error );
154150 },
155151 /**
156152 * Get gadget blob from the API (or from cache if available).

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r97154RL2: Rewrite of ext.gadgets.api that Timo and I worked on together at http://......catrope14:17, 15 September 2011

Status & tagging log