r70543 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r70542‎ | r70543 | r70544 >
Date:06:25, 6 August 2010
Author:dale
Status:deferred
Tags:
Comment:
* minor fixes to resource loader for debug log removal
* minor fix to mwEmbed core loader for concurent requests
Modified paths:
  • /branches/MwEmbedStandAlone/ResourceLoader.php (modified) (history)
  • /branches/MwEmbedStandAlone/modules/EmbedPlayer/mw.EmbedPlayer.js (modified) (history)
  • /branches/MwEmbedStandAlone/mwEmbed.js (modified) (history)

Diff [purge]

Index: branches/MwEmbedStandAlone/mwEmbed.js
@@ -281,9 +281,15 @@
282282 * @key Name of resource
283283 * @value Class file path
284284 */
285 - resourcePaths : { },
 285+ resourcePaths : { },
286286
287287 /**
 288+ * Stores resources that have been requested ( to avoid re-requesting the same resources )
 289+ * in concurrent requests )
 290+ */
 291+ requestedResourceQueue: { },
 292+
 293+ /**
288294 * javascript Resource Paths
289295 *
290296 * @key Name of resource
@@ -324,6 +330,7 @@
325331 */
326332 load: function( loadRequest, instanceCallback ) {
327333 // mw.log("mw.load:: " + loadRequest );
 334+ var _this = this;
328335
329336 // Throw out any loadRequests that are not strings
330337 loadRequest = this.cleanLoadRequest( loadRequest );
@@ -366,7 +373,7 @@
367374 if( !resourceSet ){
368375 mw.log( "mw.load:: Error with module loader: " + loadRequest + ' ( no resource set defined )' );
369376 return ;
370 - }
 377+ }
371378
372379 // xxx should use refactor "ready" stuff into a "domReady" class
373380 // So we would not have local scope globals like this:
@@ -375,7 +382,7 @@
376383 // mw.ready has run
377384 this.load( resourceSet, callback );
378385 } else {
379 - this.addToModuleLoaderQueue(
 386+ this.addToModuleLoaderQueue(
380387 loadRequest,
381388 resourceSet,
382389 callback
@@ -391,11 +398,27 @@
392399 }
393400
394401 // Try loading as a "file" or via ScriptLoader
395 - if( loadRequest ) {
 402+ if( loadRequest ) {
 403+ // Check if this resource was already requested
 404+ if( typeof this.requestedResourceQueue[ loadRequest ] == 'object' ){
 405+ this.requestedResourceQueue[ loadRequest ].push( callback );
 406+ return ;
 407+ } else {
 408+ this.requestedResourceQueue[ loadRequest ] = [];
 409+ }
 410+
396411 if( loadRequest.indexOf( '.js' ) == -1 && !mw.getResourceLoaderPath() ) {
397412 mw.log( 'Error: are you sure ' + loadRequest + ' is a file ( is it missing a resource path? ) ' );
398413 }
399 - mw.getScript( loadRequest, callback );
 414+ mw.getScript( loadRequest, function(){
 415+ // Check if we have requestedResources queue items:
 416+ while( _this.requestedResourceQueue[ loadRequest ].length ){
 417+ _this.requestedResourceQueue[ loadRequest ].shift()( loadRequest );
 418+ }
 419+ callback( loadRequest );
 420+ // empty the load request queue:
 421+ _this.requestedResourceQueue[ loadRequest ] = [];
 422+ });
400423 return ;
401424 }
402425
@@ -475,12 +498,12 @@
476499 }
477500
478501 // We are infact loading many:
479 - mw.log("mw.load: LoadMany:: " + loadSet );
 502+ //mw.log("mw.load: LoadMany:: " + loadSet );
480503
481504 // Issue the load request check check loadStates to see if we are
482505 // "done"
483506 for( var loadName in loadStates ) {
484 - // mw.log("loadMany: load: " + loadName );
 507+ mw.log("loadMany: load: " + loadName );
485508 this.load( loadName, function ( loadName ) {
486509 loadStates[ loadName ] = 1;
487510
@@ -533,20 +556,19 @@
534557 // Setup grouped loadStates Set:
535558 var groupClassKey = '';
536559 var coma = '';
 560+ var uniqueResourceName = {};
537561 for( var i=0; i < groupedLoadSet.length; i++ ) {
538 - var loadName = groupedLoadSet[ i ];
539 -
540 -
 562+ var loadName = groupedLoadSet[ i ];
541563 if( this.getResourcePath( loadName ) ) {
542 - // Only add to group request if not already set:
543 - if ( !mw.isset( loadName ) ) {
 564+ // Check if not already in request queue and not defined in global namespace
 565+ if( !mw.isset( loadName ) && ! uniqueResourceName[ loadName] ){
544566 groupClassKey += coma + loadName
545567 coma = ',';
546568
547569 // Check for style sheet dependencies
548570 if( this.resourceStyleDependency[ loadName ] ){
549571 groupClassKey += coma + this.resourceStyleDependency[ loadName ];
550 - }
 572+ }
551573 }
552574 } else if ( this.moduleLoaders[ loadName ] ) {
553575
@@ -556,9 +578,12 @@
557579 loadStates[ groupClassKey ] = 0;
558580 groupClassKey = coma = '';
559581 }
560 - // Add the module to the loadSate
561 - loadStates[ loadName ] = 0;
 582+ if( ! uniqueResourceName[ loadName] ){
 583+ // Add the module to the loadSate
 584+ loadStates[ loadName ] = 0;
 585+ }
562586 }
 587+ uniqueResourceName[ loadName] = true;
563588 }
564589
565590 // Add groupClassKey if set:
@@ -1324,7 +1349,7 @@
13251350 var isCssFile = ( ext == '.css') ? true : false ;
13261351
13271352 if( scriptLoaderPath && isResourceName ) {
1328 - url = scriptLoaderPath + '?class=' + scriptRequest;
 1353+ url = scriptLoaderPath + '?class=' + scriptRequest ;
13291354 } else {
13301355 // Add the mwEmbed path if a relative path request
13311356 url = ( isResourceName ) ? mw.getMwEmbedPath() : '';
@@ -1377,7 +1402,7 @@
13781403 // mw.log(" append script: " + script.src );
13791404 // Append the script to the DOM:
13801405 head.appendChild( script );
1381 - };
 1406+ };
13821407
13831408 /**
13841409 * Add a style sheet string to the document head
@@ -2219,7 +2244,7 @@
22202245 callback();
22212246 return ;
22222247 }
2223 - var callbackSet = $j( targetObject ).data( 'events' )[ triggerName ];
 2248+ var callbackSet = $j( targetObject ).data( 'events' )[ triggerName ];
22242249 if( !callbackSet || callbackSet.length === 0 ){
22252250 mw.log( ' No events run the callback directly: ' + triggerName );
22262251 // No events run the callback directly
@@ -2231,9 +2256,10 @@
22322257
22332258 mw.log(" runTriggersCallback:: " + callbackCount );
22342259 var callInx = 0;
2235 - $j( targetObject ).trigger( triggerName, function() {
 2260+ $j( targetObject ).trigger( triggerName, function() {
22362261 callInx++;
2237 - if( callInx == callbackCount ){
 2262+ if( callInx == callbackCount ){
 2263+ mw.log(" callbackCountReached run:: " + callback);
22382264 // Run callback
22392265 callback();
22402266 }
@@ -2522,6 +2548,10 @@
25232549 */
25242550
25252551 if( window.jQuery ){
 2552+ if( ! mw.versionIsAtLeast( '1.4.0', jQuery.fn.jquery ) ){
 2553+ if( console.log )
 2554+ console.log( 'Error mwEmbed requires jQuery 1.4 or above' );
 2555+ }
25262556 var dollarFlag = false;
25272557 if( $ && $.fn && $.fn.jquery ) {
25282558 // NOTE we could check the version of
Index: branches/MwEmbedStandAlone/ResourceLoader.php
@@ -862,32 +862,32 @@
863863 static function removeLogStatements( $jsString ){
864864 $outputJs = '';
865865 for ( $i = 0; $i < strlen( $jsString ); $i++ ) {
866 - // find next occurance of
867 - preg_match( '/[\n;]\s*mw\.log\s*/', $jsString, $matches, PREG_OFFSET_CAPTURE, $i );
868 - // check if any matches are left:
 866+ // find next occurrence of
 867+ preg_match( '/([\n;]\s*mw\.log\s*)\(/', $jsString, $matches, PREG_OFFSET_CAPTURE, $i );
 868+ // check if any matches are left:
869869 if( count( $matches ) == 0){
870870 $outputJs .= substr( $jsString, $i );
871871 break;
872872 }
873 - if( count( $matches ) > 0 ){
874 - $startOfLogIndex = strlen( $matches[0][0] ) + $matches[0][1];
875 - // append everytnig up to this point:
876 - $outputJs .= substr( $jsString, $i, ( $startOfLogIndex - strlen( $matches[0][0] ) )-$i );
877 -
878 - // Increment i to position of closing ) not inside quotes
879 - $parenthesesDepth = 0;
 873+ if( count( $matches ) > 0 ){
 874+ $startOfLogIndex = strlen( $matches[1][0] ) + $matches[1][1];
 875+ // append everything up to this point:
 876+ $outputJs .= substr( $jsString, $i, ( $startOfLogIndex - strlen( $matches[1][0] ) )-$i );
 877+
 878+ // Increment i to position of closing ) not inside quotes
 879+ $parenthesesDepth = 0;
880880 $ignorenext = false;
881881 $inquote = false;
882882 $inSingleQuote = false;
883 - for ( $i = $startOfLogIndex; $i < strlen( $jsString ); $i++ ) {
 883+ for ( $i = $startOfLogIndex; $i < strlen( $jsString ); $i++ ) {
884884 $char = $jsString[$i];
885885 if ( $ignorenext ) {
886886 $ignorenext = false;
887887 } else {
888 - // Search for a close ) that is not in quotes
 888+ // Search for a close ) that is not in quotes
889889 switch( $char ) {
890890 case '"':
891 - if( !$inSingleQuote ){
 891+ if( !$inSingleQuote ){
892892 $inquote = !$inquote;
893893 }
894894 break;
@@ -897,13 +897,13 @@
898898 }
899899 break;
900900 case '(':
901 - if( !$inquote && !$inSingleQuote ){
 901+ if( !$inquote && !$inSingleQuote ){
902902 $parenthesesDepth++;
903 - }
904 - break;
 903+ }
 904+ break;
905905 case ')':
906 - if( ! $inquote && !$inSingleQuote ){
907 - $parenthesesDepth--;
 906+ if( ! $inquote && !$inSingleQuote ){
 907+ $parenthesesDepth--;
908908 }
909909 break;
910910 case '\\':
@@ -912,18 +912,19 @@
913913 }
914914 // Done with close parentheses search for next mw.log in outer loop:
915915 if( $parenthesesDepth === 0 ){
916 - break;
917 - }
 916+ break;
 917+ }
918918 }
919919 }
920920 }
921 - }
 921+ }
922922 return $outputJs;
923923 }
924924 /* simple function to return addMessageJs without preg_replace back reference substitution */
925925 private static function preg_addMessageJs(){
926926 return self::$addMessageJs;
927927 }
 928+
928929 /**
929930 * Get the "addMesseges" function index ( for replacing msg text with localized json )
930931 *
Index: branches/MwEmbedStandAlone/modules/EmbedPlayer/mw.EmbedPlayer.js
@@ -188,6 +188,7 @@
189189 "type" : null
190190 });
191191
 192+
192193 /**
193194 * The base source attribute checks
194195 * also see: http://dev.w3.org/html5/spec/Overview.html#the-source-element
@@ -269,6 +270,7 @@
270271 *
271272 */
272273 $.fn.embedPlayer = function( attributes, callback ) {
 274+ mw.log( 'EmbedPlayer:: fn.embedPlayer' );
273275 var playerSelect = this.selector;
274276
275277 // Define attributes if unset
@@ -434,10 +436,11 @@
435437 // Update the list of dependent libraries for the player
436438 // ( allows extensions to add to the dependency list )
437439 mw.embedPlayerUpdateLibraryRequest( playerElement, playerDependencyRequest );
438 -
 440+
439441 // Load any skins we need then swap in the interface
440 - mw.load( playerDependencyRequest, function() {
441 - var waitForMeta = true;
 442+ mw.load( playerDependencyRequest, function() {
 443+ var waitForMeta = true;
 444+
442445 // Be sure to "stop" the target ( sometimes firefox keeps playing the video even
443446 // though its been removed from the DOM )
444447 if( playerElement.pause ){
@@ -450,9 +453,9 @@
451454 var eventObject = {
452455 'playerElement':playerElement,
453456 'waitForMeta' : waitForMeta
454 - }
 457+ };
 458+ $j( mw ).trigger( 'addElementWaitForMetaEvent', eventObject );
455459
456 - $j( mw ).trigger( 'addElementWaitForMetaEvent', eventObject );
457460 // update the waitForMeta
458461 waitForMeta = eventObject[ 'waitForMeta' ];
459462
@@ -495,7 +498,7 @@
496499 playerElement.removeEventListener( "loadedmetadata", runPlayerSwap, true );
497500 playerElement.addEventListener( "loadedmetadata", runPlayerSwap, true );
498501
499 - // Time-out of 5 seconds ( maybe still playable but no timely metadata )
 502+ // Time-out of 5 seconds ( maybe still playable but no timely metadata )
500503 setTimeout( runPlayerSwap, 5000 );
501504 return ;
502505 } else {
@@ -1030,6 +1033,7 @@
10311034 if ( $j( videoElement ).attr( "src" ) ) {
10321035 _this.tryAddSource( videoElement );
10331036 }
 1037+
10341038 // Process elements source children
10351039 $j( videoElement ).find( 'source,track' ).each( function( ) {
10361040 _this.tryAddSource( this );
@@ -1124,7 +1128,7 @@
11251129 * Selects the default source via cookie preference, default marked, or by id order
11261130 */
11271131 autoSelectSource: function() {
1128 - mw.log( 'EmbedPlayer::mediaElement::autoSelectSource:' );
 1132+ mw.log( 'EmbedPlayer::mediaElement::autoSelectSource:' + this.id);
11291133 // Select the default source
11301134 var playableSources = this.getPlayableSources();
11311135 var flash_flag = ogg_flag = false;
@@ -1610,7 +1614,7 @@
16111615 // Scope the end of check for player sources so it can be called in a callback
16121616 var finishCheckPlayerSources = function(){
16131617 // Run embedPlayer sources hook
1614 - mw.runTriggersCallback( _this, 'checkPlayerSourcesEvent', function(){
 1618+ mw.runTriggersCallback( _this, 'checkPlayerSourcesEvent', function(){
16151619 _this.checkForTimedText();
16161620 })
16171621 }
@@ -3681,3 +3685,4 @@
36823686 return hasObj;
36833687 }
36843688 };
 3689+

Status & tagging log