Ori.livneh has uploaded a new change for review.

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


Change subject: Cache ResourceLoader modules in localStorage
......................................................................

Cache ResourceLoader modules in localStorage

WIP

Change-Id: If2ad2d80db2336fb447817f5c56599667141ec66
---
M resources/mediawiki/mediawiki.js
1 file changed, 68 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core 
refs/changes/67/86867/1

diff --git a/resources/mediawiki/mediawiki.js b/resources/mediawiki/mediawiki.js
index 39093e3..649c6b3 100644
--- a/resources/mediawiki/mediawiki.js
+++ b/resources/mediawiki/mediawiki.js
@@ -290,6 +290,35 @@
                }
        };
 
+       function Cache( name ) {
+               this.name = name;
+               return this;
+       }
+
+       Cache.prototype = $.extend( {
+               init: function ( version ) {
+                       try {
+                               this.values = JSON.parse( localStorage.getItem( 
this.name ) )
+                                       || { version: version };
+                               // If the cached version identifier is 
different than the one
+                               // supplied to the constructor, invalidate the 
cache.
+                               if ( this.get( 'version' ) !== version ) {
+                                       localStorage.removeItem( this.name );
+                               }
+                       } catch ( e ) {
+                               // Pass.
+                       }
+               },
+               update: function () {
+                       try {
+                               localStorage.setItem( this.name, 
JSON.stringify( this.values ) );
+                               return true;
+                       } catch ( e ) {
+                               return false;
+                       }
+               }
+       }, Map.prototype );
+
        /**
         * Base library for MediaWiki.
         *
@@ -444,6 +473,8 @@
                                queue = [],
                                // List of callback functions waiting for 
modules to be ready to be called
                                jobs = [],
+                               // Cache of locally-stored module sources
+                               cache = new Cache( 'ResourceLoader' ),
                                // Selector cache for the marker element. Use 
getMarker() to get/use the marker!
                                $marker = null,
                                // Buffer for addEmbeddedCSS.
@@ -451,7 +482,16 @@
                                // Callbacks for addEmbeddedCSS.
                                cssCallbacks = $.Callbacks();
 
+                               // Do nothing.
+
                        /* Private methods */
+
+                       function makeModuleKey( moduleName ) {
+                               if ( !registry[moduleName] ) {
+                                       return false;
+                               }
+                               return moduleName + ':' + 
registry[moduleName].version;
+                       }
 
                        function getMarker() {
                                // Cached ?
@@ -1259,6 +1299,21 @@
                                                        }
                                                }
                                        }
+
+                                       cache.init( mw.config.get( 'wgVersion' 
) );
+                                       window.onload = function () {
+                                               cache.update();
+                                       }
+
+                                       batch = $.grep( batch, function ( 
module ) {
+                                               source = cache.get( 
makeModuleKey( module ) );
+                                               if ( source ) {
+                                                       $.globalEval( source );
+                                                       return false;
+                                               }
+                                               return true;
+                                       } );
+
                                        // Early exit if there's nothing to 
load...
                                        if ( !batch.length ) {
                                                return;
@@ -1490,6 +1545,19 @@
                                        registry[module].script = script;
                                        registry[module].style = style;
                                        registry[module].messages = msgs;
+
+
+                                       try {
+                                               cache.set( makeModuleKey( 
module ), 'mw.loader.implement(' + [
+                                                       '"' + module + '"',
+                                                       script.toString(),
+                                                       JSON.stringify( style ),
+                                                       JSON.stringify( msgs )
+                                               ].join(',') + ')' );
+                                       } catch ( e ) {
+                                               // Pass.
+                                       }
+
                                        // The module may already have been 
marked as erroneous
                                        if ( $.inArray( registry[module].state, 
['error', 'missing'] ) === -1 ) {
                                                registry[module].state = 
'loaded';

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: If2ad2d80db2336fb447817f5c56599667141ec66
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Ori.livneh <o...@wikimedia.org>

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

Reply via email to