2 * MediaWiki Widgets - APIResultsQueue class.
4 * @copyright 2011-2016 VisualEditor Team and others; see http://ve.mit-license.org
9 * API Results Queue object.
12 * @mixins OO.EventEmitter
15 * @param {Object} [config] Configuration options
16 * @cfg {number} limit The default number of results to fetch
17 * @cfg {number} threshold The default number of extra results
18 * that the queue should always strive to have on top of the
19 * individual requests for items.
21 mw
.widgets
.APIResultsQueue
= function MwWidgetsAPIResultsQueue( config
) {
22 config
= config
|| {};
24 this.fileRepoPromise
= null;
26 this.providerPromises
= [];
31 this.limit
= config
.limit
|| 20;
32 this.setThreshold( config
.threshold
|| 10 );
35 OO
.EventEmitter
.call( this );
39 OO
.mixinClass( mw
.widgets
.APIResultsQueue
, OO
.EventEmitter
);
44 * Set up the queue and its resources.
45 * This should be overridden if there are any setup steps to perform.
47 * @return {jQuery.Promise} Promise that resolves when the resources
48 * are set up. Note: The promise must have an .abort() functionality.
50 mw
.widgets
.APIResultsQueue
.prototype.setup = function () {
51 return $.Deferred().resolve().promise( { abort
: $.noop
} );
55 * Get items from the queue
57 * @param {number} [howMany] How many items to retrieve. Defaults to the
58 * default limit supplied on initialization.
59 * @return {jQuery.Promise} Promise that resolves into an array of items.
61 mw
.widgets
.APIResultsQueue
.prototype.get = function ( howMany
) {
62 var fetchingPromise
= null,
65 howMany
= howMany
|| this.limit
;
67 // Check if the queue has enough items
68 if ( this.queue
.length
< howMany
+ this.threshold
) {
69 // Call for more results
70 fetchingPromise
= this.queryProviders( howMany
+ this.threshold
)
71 .then( function ( items
) {
73 me
.queue
= me
.queue
.concat
.apply( me
.queue
, items
);
77 return $.when( fetchingPromise
)
79 return me
.queue
.splice( 0, howMany
);
85 * Get results from all providers
87 * @param {number} [howMany] How many items to retrieve. Defaults to the
88 * default limit supplied on initialization.
89 * @return {jQuery.Promise} Promise that is resolved into an array
90 * of fetched items. Note: The promise must have an .abort() functionality.
92 mw
.widgets
.APIResultsQueue
.prototype.queryProviders = function ( howMany
) {
96 // Make sure there are resources set up
99 // Abort previous requests
100 for ( i
= 0, len
= queue
.providerPromises
.length
; i
< len
; i
++ ) {
101 queue
.providerPromises
[ i
].abort();
103 queue
.providerPromises
= [];
104 // Set up the query to all providers
105 for ( i
= 0, len
= queue
.providers
.length
; i
< len
; i
++ ) {
106 if ( !queue
.providers
[ i
].isDepleted() ) {
107 queue
.providerPromises
.push(
108 queue
.providers
[ i
].getResults( howMany
)
113 return $.when
.apply( $, queue
.providerPromises
)
114 .then( Array
.prototype.concat
.bind( [] ) );
119 * Set the search query for all the providers.
121 * This also makes sure to abort any previous promises.
123 * @param {Object} params API search parameters
125 mw
.widgets
.APIResultsQueue
.prototype.setParams = function ( params
) {
127 if ( !OO
.compare( params
, this.params
, true ) ) {
129 this.params
= $.extend( this.params
, params
);
133 for ( i
= 0, len
= this.providerPromises
.length
; i
< len
; i
++ ) {
134 this.providerPromises
[ i
].abort();
137 for ( i
= 0, len
= this.providers
.length
; i
< len
; i
++ ) {
138 this.providers
[ i
].setUserParams( this.params
);
144 * Reset the queue and all its providers
146 mw
.widgets
.APIResultsQueue
.prototype.reset = function () {
151 for ( i
= 0, len
= this.providerPromises
.length
; i
< len
; i
++ ) {
152 this.providerPromises
[ i
].abort();
155 for ( i
= 0, len
= this.providers
.length
; i
< len
; i
++ ) {
156 this.providers
[ i
].reset();
161 * Get the data parameters sent to the API
163 * @return {Object} params API search parameters
165 mw
.widgets
.APIResultsQueue
.prototype.getParams = function () {
172 * @param {mw.widgets.APIResultsProvider[]} providers An array of providers
174 mw
.widgets
.APIResultsQueue
.prototype.setProviders = function ( providers
) {
175 this.providers
= providers
;
179 * Add a provider to the group
181 * @param {mw.widgets.APIResultsProvider} provider A provider object
183 mw
.widgets
.APIResultsQueue
.prototype.addProvider = function ( provider
) {
184 this.providers
.push( provider
);
190 * @return {mw.widgets.APIResultsProvider[]} providers An array of providers
192 mw
.widgets
.APIResultsQueue
.prototype.getProviders = function () {
193 return this.providers
;
199 * @return {number} Queue size
201 mw
.widgets
.APIResultsQueue
.prototype.getQueueSize = function () {
202 return this.queue
.length
;
206 * Set queue threshold
208 * @param {number} threshold Queue threshold, below which we will
211 mw
.widgets
.APIResultsQueue
.prototype.setThreshold = function ( threshold
) {
212 this.threshold
= threshold
;
216 * Get queue threshold
218 * @return {number} threshold Queue threshold, below which we will
221 mw
.widgets
.APIResultsQueue
.prototype.getThreshold = function () {
222 return this.threshold
;
224 }( jQuery
, mediaWiki
) );