Make Deferreds promise compatible

This patch updates the various usages of $.Deferred for loading
overlays in routes to be ES6 Promise compatible

Bug: T188937
Change-Id: I3fc24bf3471a99e7671d1191bdd46cb741286ee1
This commit is contained in:
jdlrobson 2018-08-21 17:26:46 -07:00
parent 65f2e5ef51
commit 56a10c40e0
7 changed files with 40 additions and 49 deletions

View File

@ -6,40 +6,34 @@
// categories overlay // categories overlay
overlayManager.add( /^\/categories$/, function () { overlayManager.add( /^\/categories$/, function () {
var result = $.Deferred(); return loader.loadModule( 'mobile.categories.overlays', true ).then( function ( loadingOverlay ) {
loader.loadModule( 'mobile.categories.overlays', true ).done( function ( loadingOverlay ) {
var CategoryOverlay = M.require( 'mobile.categories.overlays/CategoryOverlay' ); var CategoryOverlay = M.require( 'mobile.categories.overlays/CategoryOverlay' );
M.on( 'category-added', function () { M.on( 'category-added', function () {
window.location.hash = '#/categories'; window.location.hash = '#/categories';
} ); } );
loadingOverlay.hide(); loadingOverlay.hide();
result.resolve( new CategoryOverlay( { return new CategoryOverlay( {
api: new mw.Api(), api: new mw.Api(),
isAnon: user.isAnon(), isAnon: user.isAnon(),
title: M.getCurrentPage().title title: M.getCurrentPage().title
} ) ); } );
} ); } );
return result;
} ); } );
// add categories overlay // add categories overlay
overlayManager.add( /^\/categories\/add$/, function () { overlayManager.add( /^\/categories\/add$/, function () {
var result = $.Deferred(); return loader.loadModule( 'mobile.categories.overlays' ).then( function ( loadingOverlay ) {
loader.loadModule( 'mobile.categories.overlays' ).done( function ( loadingOverlay ) {
var CategoryAddOverlay = M.require( 'mobile.categories.overlays/CategoryAddOverlay' ); var CategoryAddOverlay = M.require( 'mobile.categories.overlays/CategoryAddOverlay' );
loadingOverlay.hide(); loadingOverlay.hide();
result.resolve( new CategoryAddOverlay( { return new CategoryAddOverlay( {
api: new mw.Api(), api: new mw.Api(),
categories: mw.config.get( 'wgCategories' ), categories: mw.config.get( 'wgCategories' ),
isAnon: user.isAnon(), isAnon: user.isAnon(),
title: M.getCurrentPage().title title: M.getCurrentPage().title
} ) ); } );
} ); } );
return result;
} ); } );
/** /**

View File

@ -171,7 +171,6 @@
overlayManager.add( /^\/editor\/(\d+|all)$/, function ( sectionId ) { overlayManager.add( /^\/editor\/(\d+|all)$/, function ( sectionId ) {
var var
$content = $( '#mw-content-text' ), $content = $( '#mw-content-text' ),
result = $.Deferred(),
preferredEditor = getPreferredEditor(), preferredEditor = getPreferredEditor(),
editorOptions = { editorOptions = {
overlayManager: overlayManager, overlayManager: overlayManager,
@ -201,7 +200,7 @@
function logInit( editor ) { function logInit( editor ) {
// If MobileFrontend is not available this will not be possible so // If MobileFrontend is not available this will not be possible so
// check first. // check first.
mw.loader.using( 'mobile.loggingSchemas.edit' ).done( function () { mw.loader.using( 'mobile.loggingSchemas.edit' ).then( function () {
mw.track( 'mf.schemaEdit', { mw.track( 'mf.schemaEdit', {
action: 'init', action: 'init',
type: 'section', type: 'section',
@ -217,13 +216,14 @@
* @private * @private
* @ignore * @ignore
* @method * @method
* @returns {JQuery.Promise}
*/ */
function loadSourceEditor() { function loadSourceEditor() {
logInit( 'wikitext' ); logInit( 'wikitext' );
loader.loadModule( 'mobile.editor.overlay' ).done( function () { return loader.loadModule( 'mobile.editor.overlay' ).then( function () {
var EditorOverlay = M.require( 'mobile.editor.overlay/EditorOverlay' ); var EditorOverlay = M.require( 'mobile.editor.overlay/EditorOverlay' );
result.resolve( new EditorOverlay( editorOptions ) ); return new EditorOverlay( editorOptions );
} ); } );
} }
@ -254,15 +254,13 @@
editorOverride !== 'SourceEditor' editorOverride !== 'SourceEditor'
) { ) {
logInit( 'visualeditor' ); logInit( 'visualeditor' );
loader.loadModule( 'mobile.editor.ve' ).done( function () { return loader.loadModule( 'mobile.editor.ve' ).then( function () {
var VisualEditorOverlay = M.require( 'mobile.editor.ve/VisualEditorOverlay' ); var VisualEditorOverlay = M.require( 'mobile.editor.ve/VisualEditorOverlay' );
result.resolve( new VisualEditorOverlay( editorOptions ) ); return new VisualEditorOverlay( editorOptions );
} ).fail( loadSourceEditor ); }, loadSourceEditor );
} else { } else {
loadSourceEditor(); return loadSourceEditor();
} }
return result;
} ); } );
updateEditPageButton( true ); updateEditPageButton( true );
// reveal edit links on user pages // reveal edit links on user pages

View File

@ -61,13 +61,13 @@
* @memberof NotificationBadge * @memberof NotificationBadge
* @instance * @instance
* @param {string} moduleName Name of a module to fetch * @param {string} moduleName Name of a module to fetch
* @return {JQuery.Deferred} * @return {JQuery.Promise}
*/ */
_loadModuleScript: function ( moduleName ) { _loadModuleScript: function ( moduleName ) {
var self = this; var self = this;
this.$el.html( this.options.loadingIconHtml ); this.$el.html( this.options.loadingIconHtml );
return mw.loader.using( moduleName ).done( function () { return mw.loader.using( moduleName ).then( function () {
// trigger a re-render once one to remove loading icon // trigger a re-render once one to remove loading icon
self.render(); self.render();
} ); } );
@ -105,7 +105,7 @@
this.$el.on( 'click', $.proxy( this.onClickBadge, this ) ); this.$el.on( 'click', $.proxy( this.onClickBadge, this ) );
this.options.overlayManager.add( /^\/notifications$/, function () { this.options.overlayManager.add( /^\/notifications$/, function () {
return self._loadNotificationOverlay().done( function ( overlay ) { return self._loadNotificationOverlay().then( function ( overlay ) {
mainMenu.openNavigationDrawer( 'secondary' ); mainMenu.openNavigationDrawer( 'secondary' );
overlay.on( 'hide', function () { overlay.on( 'hide', function () {
mainMenu.closeNavigationDrawers(); mainMenu.closeNavigationDrawers();
@ -115,6 +115,7 @@
$( '#mw-mf-page-center' ).one( 'click.secondary', function () { $( '#mw-mf-page-center' ).one( 'click.secondary', function () {
self.options.router.back(); self.options.router.back();
} ); } );
return overlay;
} ); } );
} ); } );
}, },

View File

@ -55,7 +55,7 @@
} }
// Load the notification filter overlay // Load the notification filter overlay
mw.loader.using( 'mobile.notifications.filter.overlay' ).done( function () { mw.loader.using( 'mobile.notifications.filter.overlay' ).then( function () {
var $crossWikiUnreadFilter = $( '.mw-echo-ui-crossWikiUnreadFilterWidget' ), var $crossWikiUnreadFilter = $( '.mw-echo-ui-crossWikiUnreadFilterWidget' ),
$notifReadState = $( '.mw-echo-ui-notificationsInboxWidget-main-toolbar-readState' ), $notifReadState = $( '.mw-echo-ui-notificationsInboxWidget-main-toolbar-readState' ),
NotificationsFilterOverlay = M.require( 'mobile.notifications.filter.overlay/NotificationsFilterOverlay' ); NotificationsFilterOverlay = M.require( 'mobile.notifications.filter.overlay/NotificationsFilterOverlay' );

View File

@ -124,6 +124,12 @@
window.print(); window.print();
hideSpinner(); hideSpinner();
} }
function doPrintBeforeTimeout() {
if ( self.timeout ) {
doPrint();
}
}
// The click handler may be invoked multiple times so if a pending print is occurring // The click handler may be invoked multiple times so if a pending print is occurring
// do nothing. // do nothing.
if ( !this.timeout ) { if ( !this.timeout ) {
@ -134,11 +140,7 @@
// If all image downloads are taking longer to load then the MAX_PRINT_TIMEOUT // If all image downloads are taking longer to load then the MAX_PRINT_TIMEOUT
// abort the spinner and print regardless. // abort the spinner and print regardless.
this.timeout = setTimeout( doPrint, MAX_PRINT_TIMEOUT ); this.timeout = setTimeout( doPrint, MAX_PRINT_TIMEOUT );
this.skin.loadImagesList().always( function () { this.skin.loadImagesList().then( doPrintBeforeTimeout, doPrintBeforeTimeout );
if ( self.timeout ) {
doPrint();
}
} );
} }
}, },
events: { events: {

View File

@ -130,25 +130,23 @@
// Routes // Routes
overlayManager.add( /^\/media\/(.+)$/, loadImageOverlay ); overlayManager.add( /^\/media\/(.+)$/, loadImageOverlay );
overlayManager.add( /^\/languages$/, function () { overlayManager.add( /^\/languages$/, function () {
var result = $.Deferred(), var lang = mw.config.get( 'wgUserLanguage' );
lang = mw.config.get( 'wgUserLanguage' );
loader.loadModule( 'mobile.languages.structured', true ).done( function ( loadingOverlay ) { return loader.loadModule( 'mobile.languages.structured', true ).then( function ( loadingOverlay ) {
var PageGateway = M.require( 'mobile.startup/PageGateway' ), var PageGateway = M.require( 'mobile.startup/PageGateway' ),
gateway = new PageGateway( new mw.Api() ), gateway = new PageGateway( new mw.Api() ),
LanguageOverlay = M.require( 'mobile.languages.structured/LanguageOverlay' ); LanguageOverlay = M.require( 'mobile.languages.structured/LanguageOverlay' );
gateway.getPageLanguages( mw.config.get( 'wgPageName' ), lang ).done( function ( data ) { return gateway.getPageLanguages( mw.config.get( 'wgPageName' ), lang ).then( function ( data ) {
loadingOverlay.hide(); loadingOverlay.hide();
result.resolve( new LanguageOverlay( { return new LanguageOverlay( {
currentLanguage: mw.config.get( 'wgContentLanguage' ), currentLanguage: mw.config.get( 'wgContentLanguage' ),
languages: data.languages, languages: data.languages,
variants: data.variants, variants: data.variants,
deviceLanguage: getDeviceLanguage() deviceLanguage: getDeviceLanguage()
} ) ); } );
} ); } );
} ); } );
return result;
} ); } );
// Setup // Setup

View File

@ -36,16 +36,15 @@
} }
overlayManager.add( /^\/talk\/?(.*)$/, function ( id ) { overlayManager.add( /^\/talk\/?(.*)$/, function ( id ) {
var result = $.Deferred(), var talkOptions = {
talkOptions = { api: new mw.Api(),
api: new mw.Api(), title: title,
title: title, // T184273 using `getCurrentPage` because 'wgPageName' contains underscores instead of spaces.
// T184273 using `getCurrentPage` because 'wgPageName' contains underscores instead of spaces. currentPageTitle: M.getCurrentPage().title,
currentPageTitle: M.getCurrentPage().title, licenseMsg: skin.getLicenseMsg()
licenseMsg: skin.getLicenseMsg() };
};
loader.loadModule( 'mobile.talk.overlays' ).done( function () { return loader.loadModule( 'mobile.talk.overlays' ).then( function () {
var Overlay; var Overlay;
if ( id === 'new' ) { if ( id === 'new' ) {
Overlay = M.require( 'mobile.talk.overlays/TalkSectionAddOverlay' ); Overlay = M.require( 'mobile.talk.overlays/TalkSectionAddOverlay' );
@ -55,9 +54,8 @@
} else { } else {
Overlay = M.require( 'mobile.talk.overlays/TalkOverlay' ); Overlay = M.require( 'mobile.talk.overlays/TalkOverlay' );
} }
result.resolve( new Overlay( talkOptions ) ); return new Overlay( talkOptions );
} ); } );
return result;
} ); } );
/** /**