mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-01-29 17:15:38 +08:00
perf(Notice): replace useState by useSyncExternalStore (#3682)
* fix: the first function call creates multiple axios instances * perf(Notice): replace useState by useSyncExternalStore * chore: prettier --check . * fix: prettier format
This commit is contained in:
@@ -1,24 +1,17 @@
|
|||||||
import React, { useState, useEffect } from "react";
|
import React, { useSyncExternalStore } from "react";
|
||||||
import { Snackbar, Alert, IconButton, Box } from "@mui/material";
|
import { Snackbar, Alert, IconButton, Box } from "@mui/material";
|
||||||
import { CloseRounded } from "@mui/icons-material";
|
import { CloseRounded } from "@mui/icons-material";
|
||||||
import {
|
import {
|
||||||
subscribeNotices,
|
subscribeNotices,
|
||||||
hideNotice,
|
hideNotice,
|
||||||
NoticeItem,
|
getSnapshotNotices,
|
||||||
} from "@/services/noticeService";
|
} from "@/services/noticeService";
|
||||||
|
|
||||||
export const NoticeManager: React.FC = () => {
|
export const NoticeManager: React.FC = () => {
|
||||||
const [currentNotices, setCurrentNotices] = useState<NoticeItem[]>([]);
|
const currentNotices = useSyncExternalStore(
|
||||||
|
subscribeNotices,
|
||||||
useEffect(() => {
|
getSnapshotNotices,
|
||||||
const unsubscribe = subscribeNotices((notices) => {
|
);
|
||||||
setCurrentNotices(notices);
|
|
||||||
});
|
|
||||||
|
|
||||||
return () => {
|
|
||||||
unsubscribe();
|
|
||||||
};
|
|
||||||
}, []);
|
|
||||||
|
|
||||||
const handleClose = (id: number) => {
|
const handleClose = (id: number) => {
|
||||||
hideNotice(id);
|
hideNotice(id);
|
||||||
|
|||||||
@@ -61,13 +61,15 @@ export function hideNotice(id: number) {
|
|||||||
|
|
||||||
// Subscribes a listener function to notice state changes.
|
// Subscribes a listener function to notice state changes.
|
||||||
|
|
||||||
export function subscribeNotices(listener: Listener): () => void {
|
export function subscribeNotices(listener: () => void) {
|
||||||
listeners.add(listener);
|
listeners.add(listener);
|
||||||
listener([...notices]);
|
|
||||||
return () => {
|
return () => {
|
||||||
listeners.delete(listener);
|
listeners.delete(listener);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
export function getSnapshotNotices() {
|
||||||
|
return notices;
|
||||||
|
}
|
||||||
|
|
||||||
// Function to clear all notices at once
|
// Function to clear all notices at once
|
||||||
export function clearAllNotices() {
|
export function clearAllNotices() {
|
||||||
|
|||||||
Reference in New Issue
Block a user