Skip to content

Commit

Permalink
refactor: remove activeToasts array
Browse files Browse the repository at this point in the history
  • Loading branch information
fkhadra committed Dec 1, 2024
1 parent b51a5f1 commit 2de661b
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 38 deletions.
42 changes: 20 additions & 22 deletions src/core/containerObserver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,8 @@ import {
} from '../types';
import { canBeRendered, getAutoCloseDelay, isFn, isNum, isStr, parseClassName, toToastItem } from '../utils';

interface QueuedToast {
content: ToastContent<any>;
props: ToastProps;
staleId?: Id;
}

type Notify = () => void;

interface ActiveToast {
content: ToastContent<any>;
props: ToastProps;
staleId?: Id;
}

export type ContainerObserver = ReturnType<typeof createContainerObserver>;

export function createContainerObserver(
Expand All @@ -33,8 +21,7 @@ export function createContainerObserver(
) {
let toastKey = 1;
let toastCount = 0;
let queue: QueuedToast[] = [];
let activeToasts: Id[] = [];
let queue: Toast[] = [];
let snapshot: Toast[] = [];
let props = containerProps;
const toasts = new Map<Id, Toast>();
Expand Down Expand Up @@ -63,8 +50,18 @@ export function createContainerObserver(
});
};

const markAsRemoved = (v: Toast) => {
v.props?.onClose?.(v.removedByUser);
v.isActive = false;
};

const removeToast = (id?: Id) => {
activeToasts = id == null ? [] : activeToasts.filter(v => v !== id);
if (id == null) {
toasts.forEach(markAsRemoved);
} else {
const t = toasts.get(id);
if (t) markAsRemoved(t);
}
notify();
};

Expand All @@ -73,14 +70,14 @@ export function createContainerObserver(
queue = [];
};

const addActiveToast = (toast: ActiveToast) => {
const addActiveToast = (toast: Toast) => {
const { toastId, updateId } = toast.props;
const isNew = updateId == null;

if (toast.staleId) toasts.delete(toast.staleId);
toast.isActive = true;

toasts.set(toastId, toast);
activeToasts = [...activeToasts, toastId].filter(v => v !== toast.staleId);
notify();
dispatchChanges(toToastItem(toast, isNew ? 'added' : 'updated'));

Expand All @@ -93,7 +90,6 @@ export function createContainerObserver(
const { toastId, updateId, data, staleId, delay } = options;
const closeToast = (removedByUser?: true) => {
toasts.get(toastId)!.removedByUser = removedByUser;
toasts.get(toastId)!.props.onClose?.(removedByUser);
removeToast(toastId);
};

Expand All @@ -116,7 +112,9 @@ export function createContainerObserver(
progressClassName: parseClassName(options.progressClassName || props.progressClassName),
autoClose: options.isLoading ? false : getAutoCloseDelay(options.autoClose, props.autoClose),
deleteToast() {
const toastToRemove = toasts.get(toastId)!;
const toastToRemove = toasts.get(toastId);

if (toastToRemove == null) return;

dispatchChanges(toToastItem(toastToRemove, 'removed'));
toasts.delete(toastId);
Expand All @@ -125,7 +123,7 @@ export function createContainerObserver(
if (toastCount < 0) toastCount = 0;

if (queue.length > 0) {
addActiveToast(queue.shift() as ActiveToast);
addActiveToast(queue.shift());
return;
}

Expand Down Expand Up @@ -157,7 +155,7 @@ export function createContainerObserver(
content: toastContent,
props: toastProps,
staleId
};
} as Toast;

// not handling limit + delay by design. Waiting for user feedback first
if (props.limit && props.limit > 0 && toastCount > props.limit && isNotAnUpdate) {
Expand Down Expand Up @@ -187,7 +185,7 @@ export function createContainerObserver(
const t = toasts.get(id);
if (t) t.toggle = fn;
},
isToastActive: (id: Id) => activeToasts.some(v => v === id),
isToastActive: (id: Id) => toasts.get(id)?.isActive,
getSnapshot: () => snapshot
};
}
2 changes: 1 addition & 1 deletion src/core/toast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
TypeOptions,
UpdateOptions
} from '../types';
import { Type, isFn, isNum, isStr } from '../utils';
import { isFn, isNum, isStr, Type } from '../utils';
import { genToastId } from './genToastId';
import { clearWaitingQueue, getToast, isToastActive, onChange, pushToast, removeToast, toggleToast } from './store';

Expand Down
27 changes: 12 additions & 15 deletions src/utils/mapper.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
import { Toast, ToastItem, ToastItemStatus } from '../types';

export function toToastItem(toast: Toast, status: ToastItemStatus): ToastItem {
return toast != null
? {
content: toast.content,
containerId: toast.props.containerId,
id: toast.props.toastId,
theme: toast.props.theme,
type: toast.props.type,
data: toast.props.data || {},
isLoading: toast.props.isLoading,
icon: toast.props.icon,
removedByUser: toast.removedByUser,
status
}
: // monkey patch for now
({} as ToastItem);
return {
content: toast.content,
containerId: toast.props.containerId,
id: toast.props.toastId,
theme: toast.props.theme,
type: toast.props.type,
data: toast.props.data || {},
isLoading: toast.props.isLoading,
icon: toast.props.icon,
removedByUser: toast.removedByUser,
status
};
}

0 comments on commit 2de661b

Please sign in to comment.