@blocklet/labels 2.4.27 → 2.4.29

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.es.js CHANGED
@@ -603,6 +603,69 @@ const useLabels = ({ loading, data, createLabel: createLabelAPI }) => {
603
603
  };
604
604
  };
605
605
  const LabelsContainer = createContainer(useLabels);
606
+ const createCacheKey = (args) => {
607
+ return args.map((x) => typeof x === "string" ? x : JSON.stringify(x)).join("-");
608
+ };
609
+ const CACHE_PREFIX = "request-cache-";
610
+ const getStorageKey = (key) => `${CACHE_PREFIX}${key}`;
611
+ const serialize = (data) => {
612
+ try {
613
+ return JSON.stringify(data);
614
+ } catch (error) {
615
+ console.warn("Failed to serialize cache data:", error);
616
+ return "";
617
+ }
618
+ };
619
+ const deserialize = (data) => {
620
+ try {
621
+ return JSON.parse(data);
622
+ } catch (error) {
623
+ console.warn("Failed to deserialize cache data:", error);
624
+ return null;
625
+ }
626
+ };
627
+ const setCache = (key, cachedData, options = {}) => {
628
+ const storageKey = getStorageKey(key);
629
+ const expiresAt = options.cacheTime ? Date.now() + options.cacheTime : Number.MAX_SAFE_INTEGER;
630
+ const recordData = {
631
+ ...cachedData,
632
+ expiresAt
633
+ };
634
+ localStorage.setItem(storageKey, serialize(recordData));
635
+ };
636
+ const getCache = (key) => {
637
+ const storageKey = getStorageKey(key);
638
+ const cachedData = localStorage.getItem(storageKey);
639
+ if (!cachedData) {
640
+ return null;
641
+ }
642
+ const recordData = deserialize(cachedData);
643
+ if (!recordData) {
644
+ return null;
645
+ }
646
+ if (Date.now() > recordData.expiresAt) {
647
+ localStorage.removeItem(storageKey);
648
+ return null;
649
+ }
650
+ return recordData;
651
+ };
652
+ const cleanupExpiredCache = () => {
653
+ const keysToRemove = [];
654
+ for (let i = 0; i < localStorage.length; i++) {
655
+ const key = localStorage.key(i);
656
+ if (key && key.startsWith(CACHE_PREFIX)) {
657
+ const cachedData = localStorage.getItem(key);
658
+ if (cachedData) {
659
+ const recordData = deserialize(cachedData);
660
+ if (recordData?.expiresAt && Date.now() > recordData.expiresAt) {
661
+ keysToRemove.push(key);
662
+ }
663
+ }
664
+ }
665
+ }
666
+ keysToRemove.forEach((key) => localStorage.removeItem(key));
667
+ };
668
+ cleanupExpiredCache();
606
669
  const LabelsContext = createContext({});
607
670
  const useLabelsContext = () => useContext(LabelsContext);
608
671
  const useLabelsUpdateOnDestroy = () => {
@@ -616,7 +679,11 @@ const useLabelsUpdateOnDestroy = () => {
616
679
  function LabelsProvider({ fetchLabels, createLabel, children }) {
617
680
  const { locale } = useLocaleContext();
618
681
  const [updateCounter, setUpdateCounter] = useState(1);
682
+ const cacheKey = createCacheKey(["labels"]);
619
683
  const { loading, data } = useRequest(fetchLabels, {
684
+ cacheKey,
685
+ getCache: () => getCache(cacheKey),
686
+ setCache: (d) => setCache(cacheKey, d),
620
687
  refreshDeps: [updateCounter]
621
688
  });
622
689
  const labels = useMemo(() => transformLabels(data?.labels || []), [data]);
package/dist/index.umd.js CHANGED
@@ -590,6 +590,69 @@
590
590
  };
591
591
  };
592
592
  const LabelsContainer = unstatedNext.createContainer(useLabels);
593
+ const createCacheKey = (args) => {
594
+ return args.map((x) => typeof x === "string" ? x : JSON.stringify(x)).join("-");
595
+ };
596
+ const CACHE_PREFIX = "request-cache-";
597
+ const getStorageKey = (key) => `${CACHE_PREFIX}${key}`;
598
+ const serialize = (data) => {
599
+ try {
600
+ return JSON.stringify(data);
601
+ } catch (error) {
602
+ console.warn("Failed to serialize cache data:", error);
603
+ return "";
604
+ }
605
+ };
606
+ const deserialize = (data) => {
607
+ try {
608
+ return JSON.parse(data);
609
+ } catch (error) {
610
+ console.warn("Failed to deserialize cache data:", error);
611
+ return null;
612
+ }
613
+ };
614
+ const setCache = (key, cachedData, options = {}) => {
615
+ const storageKey = getStorageKey(key);
616
+ const expiresAt = options.cacheTime ? Date.now() + options.cacheTime : Number.MAX_SAFE_INTEGER;
617
+ const recordData = {
618
+ ...cachedData,
619
+ expiresAt
620
+ };
621
+ localStorage.setItem(storageKey, serialize(recordData));
622
+ };
623
+ const getCache = (key) => {
624
+ const storageKey = getStorageKey(key);
625
+ const cachedData = localStorage.getItem(storageKey);
626
+ if (!cachedData) {
627
+ return null;
628
+ }
629
+ const recordData = deserialize(cachedData);
630
+ if (!recordData) {
631
+ return null;
632
+ }
633
+ if (Date.now() > recordData.expiresAt) {
634
+ localStorage.removeItem(storageKey);
635
+ return null;
636
+ }
637
+ return recordData;
638
+ };
639
+ const cleanupExpiredCache = () => {
640
+ const keysToRemove = [];
641
+ for (let i = 0; i < localStorage.length; i++) {
642
+ const key = localStorage.key(i);
643
+ if (key && key.startsWith(CACHE_PREFIX)) {
644
+ const cachedData = localStorage.getItem(key);
645
+ if (cachedData) {
646
+ const recordData = deserialize(cachedData);
647
+ if (recordData?.expiresAt && Date.now() > recordData.expiresAt) {
648
+ keysToRemove.push(key);
649
+ }
650
+ }
651
+ }
652
+ }
653
+ keysToRemove.forEach((key) => localStorage.removeItem(key));
654
+ };
655
+ cleanupExpiredCache();
593
656
  const LabelsContext = react.createContext({});
594
657
  const useLabelsContext = () => react.useContext(LabelsContext);
595
658
  const useLabelsUpdateOnDestroy = () => {
@@ -603,7 +666,11 @@
603
666
  function LabelsProvider({ fetchLabels, createLabel, children }) {
604
667
  const { locale } = context.useLocaleContext();
605
668
  const [updateCounter, setUpdateCounter] = react.useState(1);
669
+ const cacheKey = createCacheKey(["labels"]);
606
670
  const { loading, data } = ahooks.useRequest(fetchLabels, {
671
+ cacheKey,
672
+ getCache: () => getCache(cacheKey),
673
+ setCache: (d) => setCache(cacheKey, d),
607
674
  refreshDeps: [updateCounter]
608
675
  });
609
676
  const labels = react.useMemo(() => transformLabels(data?.labels || []), [data]);
@@ -0,0 +1,16 @@
1
+ type CachedKey = string;
2
+ export interface CachedData<TData = any, TParams = any> {
3
+ data: TData;
4
+ params: TParams;
5
+ time: number;
6
+ }
7
+ interface RecordData extends CachedData {
8
+ expiresAt: number;
9
+ }
10
+ export declare const createCacheKey: (args: unknown[]) => string;
11
+ declare const setCache: (key: CachedKey, cachedData: CachedData, options?: {
12
+ cacheTime?: number;
13
+ }) => void;
14
+ declare const getCache: (key: CachedKey) => RecordData | null;
15
+ declare const clearCache: (key?: string | string[]) => void;
16
+ export { getCache, setCache, clearCache };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@blocklet/labels",
3
- "version": "2.4.27",
3
+ "version": "2.4.29",
4
4
  "files": [
5
5
  "dist"
6
6
  ],
@@ -29,7 +29,7 @@
29
29
  "react-color": "^2.19.3",
30
30
  "react-select": "^5.8.1",
31
31
  "unstated-next": "^1.1.0",
32
- "@blocklet/translation-input": "^2.4.27"
32
+ "@blocklet/translation-input": "^2.4.29"
33
33
  },
34
34
  "peerDependencies": {
35
35
  "@arcblock/did-connect": "^3.0.1",