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

Change subject: Cache image scalable promises by filename
......................................................................


Cache image scalable promises by filename

Make sure that each API request per file is delivered once. If the
file appears more than once on the page, the API request for
scalable details will be sent once and cached so there aren't
multiple API requests per image.

Change-Id: I68507a8ceb31b77dbf33d1074939ce6219cf076e
---
M modules/ve-mw/dm/nodes/ve.dm.MWImageNode.js
1 file changed, 39 insertions(+), 21 deletions(-)

Approvals:
  Catrope: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/modules/ve-mw/dm/nodes/ve.dm.MWImageNode.js 
b/modules/ve-mw/dm/nodes/ve.dm.MWImageNode.js
index 6a0c1b9..4743ecb 100644
--- a/modules/ve-mw/dm/nodes/ve.dm.MWImageNode.js
+++ b/modules/ve-mw/dm/nodes/ve.dm.MWImageNode.js
@@ -46,6 +46,16 @@
 
 OO.mixinClass( ve.dm.MWImageNode, ve.dm.ResizableNode );
 
+/* Static Properties */
+
+/**
+ * Cache the scalable promises to make sure that we only make one
+ * API request per image.
+ *
+ * @property {Object}
+ */
+ve.dm.MWImageNode.static.promiseCache = {};
+
 /* Methods */
 
 /**
@@ -167,28 +177,36 @@
        // On the first call set off an async call to update the scalable's
        // original dimensions from the API.
        if ( !this.scalablePromise ) {
-               this.scalablePromise = ve.init.mw.Target.static.apiRequest(
-                       {
-                               'action': 'query',
-                               'prop': 'imageinfo',
-                               'indexpageids': '1',
-                               'iiprop': 'size|mediatype',
-                               'titles': this.getFilename()
-                       },
-                       { 'type': 'POST' }
-               ).then( ve.bind( function ( response ) {
-                       var page = response.query && 
response.query.pages[response.query.pageids[0]],
-                               info = page && page.imageinfo && 
page.imageinfo[0];
+               // Check if the promise is already cached
+               if ( !ve.dm.MWImageNode.static.promiseCache[this.getFilename()] 
) {
+                       this.scalablePromise = 
ve.init.mw.Target.static.apiRequest(
+                               {
+                                       'action': 'query',
+                                       'prop': 'imageinfo',
+                                       'indexpageids': '1',
+                                       'iiprop': 'size|mediatype',
+                                       'titles': this.getFilename()
+                               },
+                               { 'type': 'POST' }
+                       ).then( ve.bind( function ( response ) {
+                               var page = response.query && 
response.query.pages[response.query.pageids[0]],
+                                       info = page && page.imageinfo && 
page.imageinfo[0];
 
-                       if ( info ) {
-                               this.getScalable().setOriginalDimensions( {
-                                       'width': info.width,
-                                       'height': info.height
-                               } );
-                               // Update media type
-                               this.mediaType = info.mediatype;
-                       }
-               }, this ) ).promise();
+                               if ( info ) {
+                                       
this.getScalable().setOriginalDimensions( {
+                                               'width': info.width,
+                                               'height': info.height
+                                       } );
+                                       // Update media type
+                                       this.mediaType = info.mediatype;
+                               }
+                       }, this ) ).promise();
+                       // Cache the promise
+                       
ve.dm.MWImageNode.static.promiseCache[this.getFilename()] = 
this.scalablePromise;
+               } else {
+                       // If there is a promise for this image in cache, 
retrieve it
+                       this.scalablePromise = 
ve.dm.MWImageNode.static.promiseCache[this.getFilename()];
+               }
        }
        return this.scalablePromise;
 };

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

Gerrit-MessageType: merged
Gerrit-Change-Id: I68507a8ceb31b77dbf33d1074939ce6219cf076e
Gerrit-PatchSet: 4
Gerrit-Project: mediawiki/extensions/VisualEditor
Gerrit-Branch: master
Gerrit-Owner: Mooeypoo <mor...@gmail.com>
Gerrit-Reviewer: Catrope <roan.katt...@gmail.com>
Gerrit-Reviewer: Esanders <esand...@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