Ensure we use mw.now() instead of 'new Date' for relative measurement
authorTimo Tijhof <krinklemail@gmail.com>
Sat, 25 Mar 2017 03:37:23 +0000 (20:37 -0700)
committerJforrester <jforrester@wikimedia.org>
Sat, 25 Mar 2017 03:44:19 +0000 (03:44 +0000)
'new Date' is subject to clock drift etc.

mw.now() uses performance.now() when available, which will always
increase at a constant rate independent of the system clock.

Change-Id: Ib653103bf6116544f35c930fb33421f1bb362c7d

resources/src/jquery/jquery.suggestions.js
resources/src/mediawiki/mediawiki.Upload.BookletLayout.js

index fdc8a26..75f1ba6 100644 (file)
@@ -96,7 +96,7 @@
  */
  // jscs:enable checkParamNames
 
-( function ( $ ) {
+( function ( $, mw ) {
 
        var hasOwn = Object.hasOwnProperty;
 
                                        context.data.prevText = val;
                                        // Try cache first
                                        if ( context.config.cache && hasOwn.call( cache, val ) ) {
-                                               if ( +new Date() - cache[ val ].timestamp < context.config.cacheMaxAge ) {
+                                               if ( mw.now() - cache[ val ].timestamp < context.config.cacheMaxAge ) {
                                                        context.data.$textbox.suggestions( 'suggestions', cache[ val ].suggestions );
                                                        if ( typeof context.config.update.after === 'function' ) {
                                                                context.config.update.after.call( context.data.$textbox, cache[ val ].metadata );
                                                                        cache[ val ] = {
                                                                                suggestions: suggestions,
                                                                                metadata: metadata,
-                                                                               timestamp: +new Date()
+                                                                               timestamp: mw.now()
                                                                        };
                                                                }
                                                        },
         * @mixins jQuery.plugin.suggestions
         */
 
-}( jQuery ) );
+}( jQuery, mediaWiki ) );
index 172cac2..2f90fe6 100644 (file)
         */
        mw.Upload.BookletLayout.prototype.uploadFile = function () {
                var deferred = $.Deferred(),
-                       startTime = new Date(),
+                       startTime = mw.now(),
                        layout = this,
                        file = this.getFile();
 
                                deferred.reject( errorMessage );
                        } );
                }, function ( progress ) {
-                       var elapsedTime = new Date() - startTime,
+                       var elapsedTime = mw.now() - startTime,
                                estimatedTotalTime = ( 1 / progress ) * elapsedTime,
                                estimatedRemainingTime = moment.duration( estimatedTotalTime - elapsedTime );
                        layout.emit( 'fileUploadProgress', progress, estimatedRemainingTime );