Krinkle has uploaded a new change for review.

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

Change subject: [WIP] resourceloader: Async all the weay
......................................................................

[WIP] resourceloader: Async all the weay

Elaborate.

Change-Id: Icba6d7a87b239bf127a221bc6bc432cfa71a4a72
---
M includes/resourceloader/ResourceLoaderStartUpModule.php
M resources/src/mediawiki/mediawiki.js
M resources/src/startup.js
3 files changed, 46 insertions(+), 65 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core 
refs/changes/27/227627/1

diff --git a/includes/resourceloader/ResourceLoaderStartUpModule.php 
b/includes/resourceloader/ResourceLoaderStartUpModule.php
index 16424a0..bdf1b21 100644
--- a/includes/resourceloader/ResourceLoaderStartUpModule.php
+++ b/includes/resourceloader/ResourceLoaderStartUpModule.php
@@ -335,7 +335,7 @@
                }, array(
                        '$VARS.wgLegacyJavaScriptGlobals' => 
$this->getConfig()->get( 'LegacyJavaScriptGlobals' ),
                        '$VARS.configuration' => $this->getConfigSettings( 
$context ),
-                       '$VARS.baseModulesScript' => Html::linkedScript( 
self::getStartupModulesUrl( $context ) ),
+                       '$VARS.baseModulesUri' => self::getStartupModulesUrl( 
$context ),
                ) );
                $pairs['$CODE.registrations()'] = str_replace( "\n", "\n\t", 
trim( $this->getModuleRegistrations( $context ) ) );
 
diff --git a/resources/src/mediawiki/mediawiki.js 
b/resources/src/mediawiki/mediawiki.js
index c0b1642..348df19 100644
--- a/resources/src/mediawiki/mediawiki.js
+++ b/resources/src/mediawiki/mediawiki.js
@@ -1111,39 +1111,24 @@
                        }
 
                        /**
-                        * Adds a script tag to the DOM, either using 
document.write or low-level DOM manipulation,
-                        * depending on whether document-ready has occurred yet 
and whether we are in async mode.
+                        * Load and execute a script with callback.
                         *
                         * @private
                         * @param {string} src URL to script, will be used as 
the src attribute in the script tag
                         * @param {Function} [callback] Callback which will be 
run when the script is done
-                        * @param {boolean} [async=false] Whether to load 
modules asynchronously.
-                        *  Ignored (and defaulted to `true`) if the 
document-ready event has already occurred.
                         */
-                       function addScript( src, callback, async ) {
-                               // Using isReady directly instead of storing it 
locally from a $().ready callback (bug 31895)
-                               if ( $.isReady || async ) {
-                                       $.ajax( {
-                                               url: src,
-                                               dataType: 'script',
-                                               // Force jQuery behaviour to be 
for crossDomain. Otherwise jQuery would use
-                                               // XHR for a same domain 
request instead of <script>, which changes the request
-                                               // headers (potentially missing 
a cache hit), and reduces caching in general
-                                               // since browsers cache XHR 
much less (if at all). And XHR means we retreive
-                                               // text, so we'd need to 
$.globalEval, which then messes up line numbers.
-                                               crossDomain: true,
-                                               cache: true,
-                                               async: true
-                                       } ).always( callback );
-                               } else {
-                                       /*jshint evil:true */
-                                       document.write( mw.html.element( 
'script', { 'src': src }, '' ) );
-                                       if ( callback ) {
-                                               // Document.write is 
synchronous, so this is called when it's done.
-                                               // FIXME: That's a lie. 
doc.write isn't actually synchronous.
-                                               callback();
-                                       }
-                               }
+                       function addScript( src, callback ) {
+                               $.ajax( {
+                                       url: src,
+                                       dataType: 'script',
+                                       // Force jQuery behaviour to be for 
crossDomain. Otherwise jQuery would use
+                                       // XHR for a same domain request 
instead of <script>, which changes the request
+                                       // headers (potentially missing a cache 
hit), and reduces caching in general
+                                       // since browsers cache XHR much less 
(if at all). And XHR means we retreive
+                                       // text, so we'd need to $.globalEval, 
which then messes up line numbers.
+                                       crossDomain: true,
+                                       cache: true
+                               } ).always( callback );
                        }
 
                        /**
@@ -1193,7 +1178,7 @@
                                                        registry[module].state 
= 'ready';
                                                        handlePending( module );
                                                };
-                                               nestedAddScript = function ( 
arr, callback, async, i ) {
+                                               nestedAddScript = function ( 
arr, callback, i ) {
                                                        // Recursively call 
addScript() in its own callback
                                                        // for each element of 
arr.
                                                        if ( i >= arr.length ) {
@@ -1203,12 +1188,12 @@
                                                        }
 
                                                        addScript( arr[i], 
function () {
-                                                               
nestedAddScript( arr, callback, async, i + 1 );
-                                                       }, async );
+                                                               
nestedAddScript( arr, callback, i + 1 );
+                                                       } );
                                                };
 
                                                if ( $.isArray( script ) ) {
-                                                       nestedAddScript( 
script, markModuleReady, registry[module].async, 0 );
+                                                       nestedAddScript( 
script, markModuleReady, 0 );
                                                } else if ( $.isFunction( 
script ) ) {
                                                        registry[module].state 
= 'ready';
                                                        // Pass jQuery twice so 
that the signature of the closure which wraps
@@ -1241,37 +1226,30 @@
                                        mw.templates.set( module, 
registry[module].templates );
                                }
 
-                               if ( $.isReady || registry[module].async ) {
-                                       // Make sure we don't run the scripts 
until all (potentially asynchronous)
-                                       // stylesheet insertions have completed.
-                                       ( function () {
-                                               var pending = 0;
-                                               checkCssHandles = function () {
-                                                       // cssHandlesRegistered 
ensures we don't take off too soon, e.g. when
-                                                       // one of the 
cssHandles is fired while we're still creating more handles.
-                                                       if ( 
cssHandlesRegistered && pending === 0 && runScript ) {
-                                                               runScript();
-                                                               runScript = 
undefined; // Revoke
+                               // Make sure we don't run the scripts until all 
(potentially asynchronous)
+                               // stylesheet insertions have completed.
+                               ( function () {
+                                       var pending = 0;
+                                       checkCssHandles = function () {
+                                               // cssHandlesRegistered ensures 
we don't take off too soon, e.g. when
+                                               // one of the cssHandles is 
fired while we're still creating more handles.
+                                               if ( cssHandlesRegistered && 
pending === 0 && runScript ) {
+                                                       runScript();
+                                                       runScript = undefined; 
// Revoke
+                                               }
+                                       };
+                                       cssHandle = function () {
+                                               var check = checkCssHandles;
+                                               pending++;
+                                               return function () {
+                                                       if ( check ) {
+                                                               pending--;
+                                                               check();
+                                                               check = 
undefined; // Revoke
                                                        }
                                                };
-                                               cssHandle = function () {
-                                                       var check = 
checkCssHandles;
-                                                       pending++;
-                                                       return function () {
-                                                               if ( check ) {
-                                                                       
pending--;
-                                                                       check();
-                                                                       check = 
undefined; // Revoke
-                                                               }
-                                                       };
-                                               };
-                                       }() );
-                               } else {
-                                       // We are in blocking mode, and so we 
can't afford to wait for CSS
-                                       cssHandle = function () {};
-                                       // Run immediately
-                                       checkCssHandles = runScript;
-                               }
+                                       };
+                               }() );
 
                                // Process styles (see also mw.loader.implement)
                                // * back-compat: { <media>: css }
@@ -1338,8 +1316,7 @@
                         * @param {string|string[]} dependencies Module name or 
array of string module names
                         * @param {Function} [ready] Callback to execute when 
all dependencies are ready
                         * @param {Function} [error] Callback to execute when 
any dependency fails
-                        * @param {boolean} [async=false] Whether to load 
modules asynchronously.
-                        *  Ignored (and defaulted to `true`) if the 
document-ready event has already occurred.
+                        * @param {boolean} [async=false] Whether to load 
modules asynchronously. FIXME: Remove this parameter.
                         */
                        function request( dependencies, ready, error, async ) {
                                // Allow calling by single module name
diff --git a/resources/src/startup.js b/resources/src/startup.js
index 1332459..5410902 100644
--- a/resources/src/startup.js
+++ b/resources/src/startup.js
@@ -91,5 +91,9 @@
 
 // Conditional script injection
 if ( isCompatible() ) {
-       document.write( $VARS.baseModulesScript );
+       ( function () {
+               var script = document.createElement( 'script' );
+               script.src = $VARS.baseModulesUri;
+               document.getElementsByTagName( 'head' )[0].appendChild( script 
);
+       }() );
 }

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Icba6d7a87b239bf127a221bc6bc432cfa71a4a72
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Krinkle <krinklem...@gmail.com>

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

Reply via email to