Index: branches/RL2/extensions/Gadgets/modules/ext.gadgets.api.js |
— | — | @@ -83,6 +83,38 @@ |
84 | 84 | } |
85 | 85 | } |
86 | 86 | |
| 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 | + |
87 | 119 | /* Public functions */ |
88 | 120 | |
89 | 121 | mw.gadgets = { |
— | — | @@ -99,26 +131,10 @@ |
100 | 132 | * @param error {Function} To be called with a string (error code) as first argument. |
101 | 133 | */ |
102 | 134 | 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 | + ); |
123 | 139 | }, |
124 | 140 | |
125 | 141 | /** |
— | — | @@ -128,28 +144,8 @@ |
129 | 145 | * @param success {Function} To be called with an object of arrays of category objects, keyed by repository name, as first argument. |
130 | 146 | * @param error {Function} To be called with a string (error code) as the first argument. |
131 | 147 | */ |
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 ); |
154 | 150 | }, |
155 | 151 | /** |
156 | 152 | * Get gadget blob from the API (or from cache if available). |