Mooeypoo has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/132207

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(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/VisualEditor 
refs/changes/07/132207/1

diff --git a/modules/ve-mw/dm/nodes/ve.dm.MWImageNode.js 
b/modules/ve-mw/dm/nodes/ve.dm.MWImageNode.js
index 6a0c1b9..1729720 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 the same image
+ * doesn't release multiple api requests.
+ *
+ * @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: newchange
Gerrit-Change-Id: I68507a8ceb31b77dbf33d1074939ce6219cf076e
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/VisualEditor
Gerrit-Branch: master
Gerrit-Owner: Mooeypoo <mor...@gmail.com>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to