Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Commit

Permalink
Merge pull request #3297 from matrix-org/t3chguy/Modal.createX_promise
Browse files Browse the repository at this point in the history
Modal.createX return thenable which extends onFinished, for async/await
  • Loading branch information
t3chguy authored Aug 9, 2019
2 parents 13c7149 + 7fe078c commit a9afdec
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 19 deletions.
25 changes: 17 additions & 8 deletions src/Modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import Analytics from './Analytics';
import sdk from './index';
import dis from './dispatcher';
import { _t } from './languageHandler';
import Promise from "bluebird";

const DIALOG_CONTAINER_ID = "mx_Dialog_Container";
const STATIC_DIALOG_CONTAINER_ID = "mx_Dialog_StaticContainer";
Expand Down Expand Up @@ -182,7 +183,7 @@ class ModalManager {
const modal = {};

// 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,
// otherwise we'll get confused.
Expand All @@ -197,11 +198,13 @@ class ModalManager {
modal.onFinished = props ? props.onFinished : null;
modal.className = className;

return {modal, closeDialog};
return {modal, closeDialog, onFinishedProm};
}

_getCloseFn(modal, props) {
return (...args) => {
const deferred = Promise.defer();
return [(...args) => {
deferred.resolve(args);
if (props && props.onFinished) props.onFinished.apply(null, args);
const i = this._modals.indexOf(modal);
if (i >= 0) {
Expand All @@ -223,7 +226,7 @@ class ModalManager {
}

this._reRender();
};
}, deferred.promise];
}

/**
Expand Down Expand Up @@ -256,7 +259,7 @@ class ModalManager {
* @returns {object} Object with 'close' parameter being a function that will close the dialog
*/
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) {
// XXX: This is destructive
Expand All @@ -269,15 +272,21 @@ class ModalManager {
}

this._reRender();
return {close: closeDialog};
return {
close: closeDialog,
then: (resolve, reject) => onFinishedProm.then(resolve, reject),
};
}

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._reRender();
return {close: closeDialog};
return {
close: closeDialog,
then: (resolve, reject) => onFinishedProm.then(resolve, reject),
};
}

closeAll() {
Expand Down
21 changes: 10 additions & 11 deletions src/components/structures/MatrixChat.js
Original file line number Diff line number Diff line change
Expand Up @@ -931,18 +931,17 @@ export default React.createClass({
}).close;
},

_createRoom: function() {
_createRoom: async function() {
const CreateRoomDialog = sdk.getComponent('dialogs.CreateRoomDialog');
Modal.createTrackedDialog('Create Room', '', CreateRoomDialog, {
onFinished: (shouldCreate, name, noFederate) => {
if (shouldCreate) {
const createOpts = {};
if (name) createOpts.name = name;
if (noFederate) createOpts.creation_content = {'m.federate': false};
createRoom({createOpts}).done();
}
},
});
const modal = Modal.createTrackedDialog('Create Room', '', CreateRoomDialog);

const [shouldCreate, name, noFederate] = await modal;
if (shouldCreate) {
const createOpts = {};
if (name) createOpts.name = name;
if (noFederate) createOpts.creation_content = {'m.federate': false};
createRoom({createOpts}).done();
}
},

_chatCreateOrReuse: function(userId) {
Expand Down

0 comments on commit a9afdec

Please sign in to comment.