Skip to content

Commit

Permalink
Merge branch 'fix-issue-560' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
fkhadra committed Jan 28, 2021
2 parents 9d24ac1 + 8e2382d commit 15b34a4
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 30 deletions.
4 changes: 2 additions & 2 deletions src/hooks/toastContainerReducer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Id } from '../types';

import { hasToastId } from '../utils';
import { isToastIdValid } from '../utils';

export const enum ActionType {
ADD,
Expand All @@ -16,7 +16,7 @@ export function reducer(state: State, action: Action) {
case ActionType.ADD:
return [...state, action.toastId].filter(id => id !== action.staleId);
case ActionType.REMOVE:
return hasToastId(action.toastId)
return isToastIdValid(action.toastId)
? state.filter(id => id !== action.toastId)
: [];
}
Expand Down
51 changes: 24 additions & 27 deletions src/hooks/useToastContainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
isFn,
isNum,
isStr,
hasToastId,
isToastIdValid,
getAutoCloseDelay,
Direction,
Default
Expand Down Expand Up @@ -106,36 +106,12 @@ export function useToastContainer(props: ToastContainerProps) {
}

function removeToast(toastId?: Id) {
const queueLen = queue.length;
toastCount = hasToastId(toastId)
? toastCount - 1
: toastCount - instance.displayedToast;

if (toastCount < 0) toastCount = 0;

if (queueLen > 0) {
const freeSlot = hasToastId(toastId) ? 1 : instance.props.limit!;

if (queueLen === 1 || freeSlot === 1) {
instance.displayedToast++;
dequeueToast();
} else {
const toDequeue = freeSlot > queueLen ? queueLen : freeSlot;
instance.displayedToast = toDequeue;

for (let i = 0; i < toDequeue; i++) dequeueToast();
}
}
dispatch({ type: ActionType.REMOVE, toastId });
}

function dequeueToast() {
const { toastContent, toastProps, staleId } = queue.shift() as QueuedToast;

// ensure that exit transition has been completed, hence the timeout
setTimeout(() => {
appendToast(toastContent, toastProps, staleId);
}, 500);
appendToast(toastContent, toastProps, staleId);
}

/**
Expand Down Expand Up @@ -291,7 +267,28 @@ export function useToastContainer(props: ToastContainerProps) {

function removeFromCollection(toastId: Id) {
delete collection[toastId];
forceUpdate();
const queueLen = queue.length;
toastCount = isToastIdValid(toastId)
? toastCount - 1
: toastCount - instance.displayedToast;

if (toastCount < 0) toastCount = 0;

if (queueLen > 0) {
const freeSlot = isToastIdValid(toastId) ? 1 : instance.props.limit!;

if (queueLen === 1 || freeSlot === 1) {
instance.displayedToast++;
dequeueToast();
} else {
const toDequeue = freeSlot > queueLen ? queueLen : freeSlot;
instance.displayedToast = toDequeue;

for (let i = 0; i < toDequeue; i++) dequeueToast();
}
} else {
forceUpdate();
}
}

function getToastToRender<T>(
Expand Down
2 changes: 1 addition & 1 deletion src/utils/propValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export function parseClassName(v: any) {
return isStr(v) || isFn(v) ? v : null;
}

export function hasToastId(toastId?: Id) {
export function isToastIdValid(toastId?: Id) {
return toastId === 0 || toastId;
}

Expand Down

0 comments on commit 15b34a4

Please sign in to comment.