Modal.createX return thenable which extends onFinished, for async/await

Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>
This commit is contained in:
Michael Telatynski 2019-08-09 16:47:22 +01:00
parent 3214ed8829
commit 7fe078c07a
2 changed files with 27 additions and 19 deletions

View file

@ -23,6 +23,7 @@ import Analytics from './Analytics';
import sdk from './index'; import sdk from './index';
import dis from './dispatcher'; import dis from './dispatcher';
import { _t } from './languageHandler'; import { _t } from './languageHandler';
import Promise from "bluebird";
const DIALOG_CONTAINER_ID = "mx_Dialog_Container"; const DIALOG_CONTAINER_ID = "mx_Dialog_Container";
const STATIC_DIALOG_CONTAINER_ID = "mx_Dialog_StaticContainer"; const STATIC_DIALOG_CONTAINER_ID = "mx_Dialog_StaticContainer";
@ -182,7 +183,7 @@ class ModalManager {
const modal = {}; const modal = {};
// never call this from onFinished() otherwise it will loop // never call this from onFinished() otherwise it will loop
const closeDialog = this._getCloseFn(modal, props); const [closeDialog, onFinishedProm] = this._getCloseFn(modal, props);
// don't attempt to reuse the same AsyncWrapper for different dialogs, // don't attempt to reuse the same AsyncWrapper for different dialogs,
// otherwise we'll get confused. // otherwise we'll get confused.
@ -197,11 +198,13 @@ class ModalManager {
modal.onFinished = props ? props.onFinished : null; modal.onFinished = props ? props.onFinished : null;
modal.className = className; modal.className = className;
return {modal, closeDialog}; return {modal, closeDialog, onFinishedProm};
} }
_getCloseFn(modal, props) { _getCloseFn(modal, props) {
return (...args) => { const deferred = Promise.defer();
return [(...args) => {
deferred.resolve(args);
if (props && props.onFinished) props.onFinished.apply(null, args); if (props && props.onFinished) props.onFinished.apply(null, args);
const i = this._modals.indexOf(modal); const i = this._modals.indexOf(modal);
if (i >= 0) { if (i >= 0) {
@ -223,7 +226,7 @@ class ModalManager {
} }
this._reRender(); this._reRender();
}; }, deferred.promise];
} }
/** /**
@ -256,7 +259,7 @@ class ModalManager {
* @returns {object} Object with 'close' parameter being a function that will close the dialog * @returns {object} Object with 'close' parameter being a function that will close the dialog
*/ */
createDialogAsync(prom, props, className, isPriorityModal, isStaticModal) { createDialogAsync(prom, props, className, isPriorityModal, isStaticModal) {
const {modal, closeDialog} = this._buildModal(prom, props, className); const {modal, closeDialog, onFinishedProm} = this._buildModal(prom, props, className);
if (isPriorityModal) { if (isPriorityModal) {
// XXX: This is destructive // XXX: This is destructive
@ -269,15 +272,21 @@ class ModalManager {
} }
this._reRender(); this._reRender();
return {close: closeDialog}; return {
close: closeDialog,
then: (resolve, reject) => onFinishedProm.then(resolve, reject),
};
} }
appendDialogAsync(prom, props, className) { appendDialogAsync(prom, props, className) {
const {modal, closeDialog} = this._buildModal(prom, props, className); const {modal, closeDialog, onFinishedProm} = this._buildModal(prom, props, className);
this._modals.push(modal); this._modals.push(modal);
this._reRender(); this._reRender();
return {close: closeDialog}; return {
close: closeDialog,
then: (resolve, reject) => onFinishedProm.then(resolve, reject),
};
} }
closeAll() { closeAll() {

View file

@ -931,10 +931,11 @@ export default React.createClass({
}).close; }).close;
}, },
_createRoom: function() { _createRoom: async function() {
const CreateRoomDialog = sdk.getComponent('dialogs.CreateRoomDialog'); const CreateRoomDialog = sdk.getComponent('dialogs.CreateRoomDialog');
Modal.createTrackedDialog('Create Room', '', CreateRoomDialog, { const modal = Modal.createTrackedDialog('Create Room', '', CreateRoomDialog);
onFinished: (shouldCreate, name, noFederate) => {
const [shouldCreate, name, noFederate] = await modal;
if (shouldCreate) { if (shouldCreate) {
const createOpts = {}; const createOpts = {};
if (name) createOpts.name = name; if (name) createOpts.name = name;
@ -942,8 +943,6 @@ export default React.createClass({
createRoom({createOpts}).done(); createRoom({createOpts}).done();
} }
}, },
});
},
_chatCreateOrReuse: function(userId) { _chatCreateOrReuse: function(userId) {
// Use a deferred action to reshow the dialog once the user has registered // Use a deferred action to reshow the dialog once the user has registered