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

fix(interim, toast): add hide queue to prevent multiple interims at same time #6684

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 32 additions & 11 deletions src/core/services/interimElement/interimElement.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ function InterimElementProvider() {
* A service used to control inserting and removing an element into the DOM.
*
*/
var service, stack = [];
var service, stack = [], hideQueue = 0;

// Publish instance $$interimElement service;
// ... used as $mdDialog, $mdToast, $mdMenu, and $mdSelect
Expand Down Expand Up @@ -291,6 +291,10 @@ function InterimElementProvider() {
// This hide()s only the current interim element before showing the next, new one
// NOTE: this is not reversible (e.g. interim elements are not stackable)

if (!hideExisting) {
return $q.reject('Too many interim elements in queue!');
}

hideExisting.finally(function() {

stack.push(interimElement);
Expand Down Expand Up @@ -323,30 +327,43 @@ function InterimElementProvider() {
*/
function hide(reason, options) {
if ( !stack.length ) return $q.when(reason);
if (stack.length - 1 - hideQueue < 0) return;
options = options || {};

if (options.closeAll) {
var promise = $q.all(stack.reverse().map(closeElement));
stack = [];
return promise;
} else if (options.closeTo !== undefined) {
return $q.all(stack.splice(options.closeTo).map(closeElement));
} else {
var interim = stack.pop();
return closeElement(interim);
return $q.all(stack.reverse().map(closeElement));
}

if (options.closeTo) {
return $q.all(stack.slice(options.closeTo).map(closeElement));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why use slice here while all the other functions using splice?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that's by intention, because we don't want to remove items from stack when the close animation starts. They will be removed later, after the animation is done.

}

// Poll the first added interim element, which isn't currently hiding.
return closeElement(stack[stack.length - 1 - hideQueue]);

function closeElement(interim) {
hideQueue++;
interim
.remove(reason, false, options || { })
.catch(function( reason ) {
//$log.error("InterimElement.hide() error: " + reason );
return reason;
})
.finally(function() {
handleElementHide(interim);
});
return interim.deferred.promise;
}
}

function handleElementHide(element) {
var index = stack.indexOf(element);
if (index != -1) {
stack.splice(index, 1);
}
hideQueue--;
}

/*
* @ngdoc method
* @name $$interimElement.$service#cancel
Expand All @@ -360,14 +377,18 @@ function InterimElementProvider() {
*
*/
function cancel(reason, options) {
var interim = stack.shift();
var interim = stack.pop();
if ( !interim ) return $q.when(reason);

hideQueue++;
interim
.remove(reason, true, options || { })
.catch(function( reason ) {
//$log.error("InterimElement.cancel() error: " + reason );
return reason;
})
.finally(function() {
hideQueue--;
});

return interim.deferred.promise;
Expand All @@ -378,7 +399,7 @@ function InterimElementProvider() {
* Note: interim elements are in "interim containers"
*/
function destroy(target) {
var interim = !target ? stack.shift() : null;
var interim = !target ? stack.splice(hideQueue, 1)[0] : null;
var cntr = angular.element(target).length ? angular.element(target)[0].parentNode : null;

if (cntr) {
Expand Down