Skip to content

Commit 45a5f58

Browse files
committed
style: format code with prettier
1 parent 624bb99 commit 45a5f58

2 files changed

Lines changed: 79 additions & 22 deletions

File tree

src/context/ToastContext.tsx

Lines changed: 69 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,20 @@
11
'use client';
22

33
import { DEFAULT_TOAST_DURATION } from '@/constants/app.constants';
4-
import React, { createContext, useContext, useState, useCallback, useMemo, ReactNode, useRef } from 'react';
4+
import React, {
5+
createContext,
6+
useContext,
7+
useState,
8+
useCallback,
9+
useMemo,
10+
ReactNode,
11+
useRef,
12+
} from 'react';
513
import { Toast, ToastType } from '@/components/ui/Toast';
6-
import { createToastCircuitBreaker, CircuitBreakerMetrics } from '@/utils/circuitBreaker';
14+
import {
15+
createToastCircuitBreaker,
16+
CircuitBreakerMetrics,
17+
} from '@/utils/circuitBreaker';
718

819
export interface ToastMessage {
920
id: string;
@@ -27,24 +38,35 @@ const ToastContext = createContext<ToastContextType | undefined>(undefined);
2738

2839
export function ToastProvider({ children }: { children: ReactNode }) {
2940
const [toasts, setToasts] = useState<ToastMessage[]>([]);
30-
const circuitBreaker = useRef(createToastCircuitBreaker({
31-
failureThreshold: 5,
32-
successThreshold: 2,
33-
timeout: 60000,
34-
monitoringPeriod: 10000,
35-
maxConcurrentRequests: 10,
36-
})).current;
41+
const circuitBreaker = useRef(
42+
createToastCircuitBreaker({
43+
failureThreshold: 5,
44+
successThreshold: 2,
45+
timeout: 60000,
46+
monitoringPeriod: 10000,
47+
maxConcurrentRequests: 10,
48+
}),
49+
).current;
3750

3851
const removeToast = useCallback((id: string) => {
39-
setToasts((prev: ToastMessage[]) => prev.filter((toast: ToastMessage) => toast.id !== id));
52+
setToasts((prev: ToastMessage[]) =>
53+
prev.filter((toast: ToastMessage) => toast.id !== id),
54+
);
4055
}, []);
4156

4257
const addToast = useCallback(
43-
(message: string, type: ToastType = 'info', duration = DEFAULT_TOAST_DURATION) => {
58+
(
59+
message: string,
60+
type: ToastType = 'info',
61+
duration = DEFAULT_TOAST_DURATION,
62+
) => {
4463
circuitBreaker.execute(
4564
() => {
4665
const id = Math.random().toString(36).substring(2, 11);
47-
setToasts((prev: ToastMessage[]) => [...prev, { id, type, message, duration }]);
66+
setToasts((prev: ToastMessage[]) => [
67+
...prev,
68+
{ id, type, message, duration },
69+
]);
4870

4971
if (duration > 0) {
5072
setTimeout(() => {
@@ -55,10 +77,21 @@ export function ToastProvider({ children }: { children: ReactNode }) {
5577
},
5678
() => {
5779
// Fallback: Log to console when circuit is open
58-
console.warn('[Toast Circuit Breaker] Toast notification suppressed:', { type, message });
80+
console.warn('[Toast Circuit Breaker] Toast notification suppressed:', {
81+
type,
82+
message,
83+
});
5984
// Optionally show a simplified fallback toast
6085
const id = Math.random().toString(36).substring(2, 11);
61-
setToasts((prev: ToastMessage[]) => [...prev.slice(-2), { id, type: 'info', message: 'Notifications temporarily limited', duration: 3000 }]);
86+
setToasts((prev: ToastMessage[]) => [
87+
...prev.slice(-2),
88+
{
89+
id,
90+
type: 'info',
91+
message: 'Notifications temporarily limited',
92+
duration: 3000,
93+
},
94+
]);
6295
if (duration > 0) {
6396
setTimeout(() => {
6497
removeToast(id);
@@ -77,7 +110,8 @@ export function ToastProvider({ children }: { children: ReactNode }) {
77110
[addToast],
78111
);
79112
const success = useCallback(
80-
(message: string, duration?: number) => addToast(message, 'success', duration),
113+
(message: string, duration?: number) =>
114+
addToast(message, 'success', duration),
81115
[addToast],
82116
);
83117
const info = useCallback(
@@ -94,8 +128,26 @@ export function ToastProvider({ children }: { children: ReactNode }) {
94128
}, [circuitBreaker]);
95129

96130
const value = useMemo(
97-
() => ({ toasts, addToast, removeToast, error, success, info, getCircuitBreakerMetrics, resetCircuitBreaker }),
98-
[toasts, addToast, removeToast, error, success, info, getCircuitBreakerMetrics, resetCircuitBreaker],
131+
() => ({
132+
toasts,
133+
addToast,
134+
removeToast,
135+
error,
136+
success,
137+
info,
138+
getCircuitBreakerMetrics,
139+
resetCircuitBreaker,
140+
}),
141+
[
142+
toasts,
143+
addToast,
144+
removeToast,
145+
error,
146+
success,
147+
info,
148+
getCircuitBreakerMetrics,
149+
resetCircuitBreaker,
150+
],
99151
);
100152

101153
return (

src/utils/circuitBreaker.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
/**
22
* Circuit Breaker Implementation for Toast Notifications
3-
*
3+
*
44
* This utility implements the Circuit Breaker pattern to prevent cascading failures
55
* and provide fallback behavior when the toast notification system is overwhelmed.
6-
*
6+
*
77
* States:
88
* - CLOSED: Normal operation, requests pass through
99
* - OPEN: Circuit is tripped, requests fail fast
@@ -56,7 +56,10 @@ export class CircuitBreaker {
5656
/**
5757
* Execute an operation with circuit breaker protection
5858
*/
59-
async execute<T>(operation: () => Promise<T> | T, fallback?: () => T): Promise<T> {
59+
async execute<T>(
60+
operation: () => Promise<T> | T,
61+
fallback?: () => T,
62+
): Promise<T> {
6063
this.totalRequests++;
6164

6265
// Check if circuit is open and timeout has elapsed
@@ -125,7 +128,7 @@ export class CircuitBreaker {
125128

126129
// Clean up old failures outside monitoring period
127130
this.failureHistory = this.failureHistory.filter(
128-
time => Date.now() - time < this.config.monitoringPeriod
131+
(time) => Date.now() - time < this.config.monitoringPeriod,
129132
);
130133

131134
if (this.state === 'HALF_OPEN') {
@@ -211,6 +214,8 @@ export class CircuitBreaker {
211214
/**
212215
* Create a circuit breaker instance for toast notifications
213216
*/
214-
export function createToastCircuitBreaker(config?: Partial<CircuitBreakerConfig>): CircuitBreaker {
217+
export function createToastCircuitBreaker(
218+
config?: Partial<CircuitBreakerConfig>,
219+
): CircuitBreaker {
215220
return new CircuitBreaker({ ...DEFAULT_CONFIG, ...config });
216221
}

0 commit comments

Comments
 (0)