Debian Codegraph @

extensions/VisualEditor/modules/ve-mw/init/apiresponsecache/ve.init.mw.ApiResponseCache.js

raw symbols SCIP
1 /*!
2 * VisualEditor MediaWiki Initialization ApiResponseCache class.
3 *
4 * @copyright See AUTHORS.txt
5 * @license The MIT License (MIT); see LICENSE.txt
6 */
7
8 /**
9 * MediaWiki API batch queue.
10 *
11 * Used to queue up lists of items centrally to get information about in batches
12 * of requests.
13 *
14 * @class
15 * @extends OO.EventEmitter
16 * @constructor
17 * @param {mw.Api} [api] API object to use. Defaults to new mw.Api()
18 */
19 ve.init.mw.ApiResponseCache = function VeInitMwApiResponseCache( api ) {
20 // Mixin constructor
21 OO.EventEmitter.call( this );
22
23 this.api = api || new mw.Api();
24
25 // Keys are titles, values are deferreds
26 this.deferreds = {};
27
28 // Keys are page names, values are link data objects
29 // This is kept for synchronous retrieval of cached values via #getCached
30 this.cacheValues = {};
31
32 // Array of page titles queued to be looked up
33 this.queue = [];
34
35 this.schedule = ve.debounce( this.processQueue.bind( this ), 0 );
36 };
37
38 /* Inheritance */
39
40 OO.mixinClass( ve.init.mw.ApiResponseCache, OO.EventEmitter );
41
42 /* Static methods */
43
44 /**
45 * Process each page in the response of an API request
46 *
47 * @abstract
48 * @static
49 * @param {Object} page
50 * @return {Object|undefined} Any relevant info that we want to cache and return.
51 */
52 ve.init.mw.ApiResponseCache.static.processPage = null;
53
54 /**
55 * @param {string} title
56 * @return {string}
57 */
58 ve.init.mw.ApiResponseCache.static.normalizeTitle = function ( title ) {
59 const titleObj = mw.Title.newFromText( title );
60 if ( !titleObj ) {
61 return title;
62 }
63 return titleObj.getPrefixedText();
64 };
65
66 /* Methods */
67
68 /**
69 * Look up data about a title. If the data about this title is already in the cache, this
70 * returns an already-resolved promise. Otherwise, it returns a pending promise and schedules
71 * an request to retrieve the data.
72 *
73 * @param {string} title
74 * @return {jQuery.Promise} Promise that will be resolved with the data once it's available
75 */
76 ve.init.mw.ApiResponseCache.prototype.get = function ( title ) {
77 if ( typeof title !== 'string' ) {
78 // Don't bother letting things like undefined or null make it all the way through,
79 // just reject them here. Otherwise they'll cause problems or exceptions at random
80 // other points in this file.
81 return ve.createDeferred().reject().promise();
82 }
83 title = this.constructor.static.normalizeTitle( title );
84 if ( !Object.prototype.hasOwnProperty.call( this.deferreds, title ) ) {
85 this.deferreds[ title ] = ve.createDeferred();
86 this.queue.push( title );
87 this.schedule();
88 }
89 return this.deferreds[ title ].promise();
90 };
91
92 /**
93 * Look up data about a page in the cache. If the data about this page is already in the cache,
94 * this returns that data. Otherwise, it returns undefined.
95 *
96 * @param {string} name Normalized page title
97 * @return {Object|undefined} Cache data for this name.
98 */
99 ve.init.mw.ApiResponseCache.prototype.getCached = function ( name ) {
100 if ( Object.prototype.hasOwnProperty.call( this.cacheValues, name ) ) {
101 return this.cacheValues[ name ];
102 }
103 };
104
105 /**
106 * Fired when a new entry is added to the cache.
107 *
108 * @event ve.init.mw.ApiResponseCache#add
109 * @param {Object} entries Cache entries that were added. Object mapping names to data objects.
110 */
111
112 /**
113 * Add entries to the cache. Does not overwrite already-set entries.
114 *
115 * @param {Object} entries Object keyed by page title, with the values being data objects
116 * @fires ve.init.mw.ApiResponseCache#add
117 */
118 ve.init.mw.ApiResponseCache.prototype.set = function ( entries ) {
119 for ( const name in entries ) {
120 if ( !Object.prototype.hasOwnProperty.call( this.deferreds, name ) ) {
121 this.deferreds[ name ] = ve.createDeferred();
122 }
123 if ( this.deferreds[ name ].state() === 'pending' ) {
124 this.deferreds[ name ].resolve( entries[ name ] );
125 this.cacheValues[ name ] = entries[ name ];
126 }
127 }
128 this.emit( 'add', Object.keys( entries ) );
129 };
130
131 /**
132 * Get an API request promise to deal with a list of titles
133 *
134 * @abstract
135 * @param subqueue
136 * @return {jQuery.Promise}
137 */
138 ve.init.mw.ApiResponseCache.prototype.getRequestPromise = null;
139
140 /**
141 * Perform any scheduled API requests.
142 *
143 * @private
144 * @fires ve.init.mw.ApiResponseCache#add
145 */
146 ve.init.mw.ApiResponseCache.prototype.processQueue = function () {
147 const rejectSubqueue = ( rejectQueue ) => {
148 for ( let i = 0, len = rejectQueue.length; i < len; i++ ) {
149 this.deferreds[ rejectQueue[ i ] ].reject();
150 }
151 };
152
153 const processResult = ( data ) => {
154 const pages = ( data.query && data.query.pages ) || data.pages,
155 processed = {};
156
157 const mappedTitles = [];
158 [ 'redirects', 'normalized', 'converted' ].forEach( ( map ) => {
159 ve.batchPush( mappedTitles, ( data.query && data.query[ map ] ) || [] );
160 } );
161
162 if ( pages ) {
163 let page, processedPage;
164 for ( const pageid in pages ) {
165 page = pages[ pageid ];
166 processedPage = this.constructor.static.processPage( page );
167 if ( processedPage !== undefined ) {
168 processed[ page.title ] = processedPage;
169 }
170 }
171 for ( let i = 0; i < mappedTitles.length; i++ ) {
172 // Locate the title in mapped titles, if any.
173 if ( mappedTitles[ i ].to === page.title ) {
174 const from = mappedTitles[ i ].fromencoded === '' ?
175 decodeURIComponent( mappedTitles[ i ].from ) :
176 mappedTitles[ i ].from;
177 processed[ from ] = processedPage;
178 break;
179 }
180 }
181 this.set( processed );
182 }
183 };
184
185 const queue = this.queue;
186 this.queue = [];
187 while ( queue.length ) {
188 const subqueue = queue.splice( 0, 50 ).map( this.constructor.static.normalizeTitle );
189 this.getRequestPromise( subqueue )
190 .then( processResult )
191
192 // Reject everything in subqueue; this will only reject the ones
193 // that weren't already resolved above, because .reject() on an
194 // already resolved Deferred is a no-op.
195 .then( rejectSubqueue.bind( null, subqueue ) );
196 }
197 };