generator-pninja 2.0.3 → 2.0.4
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.
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import React, { createContext, useContext,
|
|
1
|
+
import React, { createContext, useContext, useCallback, useRef, ReactNode } from 'react';
|
|
2
2
|
import axios from 'axios';
|
|
3
3
|
|
|
4
4
|
interface AuthorizationContextType {
|
|
@@ -20,72 +20,49 @@ interface AuthorizationProviderProps {
|
|
|
20
20
|
|
|
21
21
|
export const AuthorizationProvider: React.FC<AuthorizationProviderProps> = ({
|
|
22
22
|
children,
|
|
23
|
-
cacheDuration = 60 * 1000,
|
|
23
|
+
cacheDuration = 5 * 60 * 1000,
|
|
24
24
|
}) => {
|
|
25
|
-
const
|
|
26
|
-
const
|
|
27
|
-
|
|
28
|
-
const getCacheKey = (resource: string, action: string): string => {
|
|
29
|
-
return `${resource}:${action}`;
|
|
30
|
-
};
|
|
25
|
+
const cacheRef = useRef<Map<string, CacheEntry>>(new Map());
|
|
26
|
+
const pendingRef = useRef<Map<string, Promise<boolean>>>(new Map());
|
|
31
27
|
|
|
32
28
|
const refresh = useCallback(() => {
|
|
33
|
-
|
|
34
|
-
|
|
29
|
+
cacheRef.current = new Map();
|
|
30
|
+
pendingRef.current = new Map();
|
|
35
31
|
}, []);
|
|
36
32
|
|
|
37
|
-
const can = useCallback(
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
// Rimuovi dalla pending (SINCRONO)
|
|
72
|
-
pendingRequestsRef.current.delete(key);
|
|
73
|
-
|
|
74
|
-
return allowed;
|
|
75
|
-
})
|
|
76
|
-
.catch(() => {
|
|
77
|
-
// Rimuovi dalla pending anche in caso di errore
|
|
78
|
-
pendingRequestsRef.current.delete(key);
|
|
79
|
-
return false;
|
|
80
|
-
});
|
|
81
|
-
|
|
82
|
-
// Salva la promise come pending (SINCRONO)
|
|
83
|
-
pendingRequestsRef.current.set(key, request);
|
|
84
|
-
|
|
85
|
-
return request;
|
|
86
|
-
},
|
|
87
|
-
[cache, cacheDuration]
|
|
88
|
-
);
|
|
33
|
+
const can = useCallback(async (resource: string, action: string): Promise<boolean> => {
|
|
34
|
+
const key = `${resource}:${action}`;
|
|
35
|
+
const now = Date.now();
|
|
36
|
+
|
|
37
|
+
// 1. Valid cache
|
|
38
|
+
const cached = cacheRef.current.get(key);
|
|
39
|
+
if (cached && now - cached.timestamp < cacheDuration) {
|
|
40
|
+
return cached.allowed;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// 2. Request pending: reuse the promise
|
|
44
|
+
const pending = pendingRef.current.get(key);
|
|
45
|
+
if (pending) {
|
|
46
|
+
return pending;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// 3. New request
|
|
50
|
+
const request = axios
|
|
51
|
+
.post<{ allowed: boolean }>('/api/user/can', { resource, action })
|
|
52
|
+
.then((response) => {
|
|
53
|
+
const allowed = response.data.allowed;
|
|
54
|
+
cacheRef.current.set(key, { allowed, timestamp: Date.now() });
|
|
55
|
+
pendingRef.current.delete(key);
|
|
56
|
+
return allowed;
|
|
57
|
+
})
|
|
58
|
+
.catch(() => {
|
|
59
|
+
pendingRef.current.delete(key);
|
|
60
|
+
return false;
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
pendingRef.current.set(key, request);
|
|
64
|
+
return request;
|
|
65
|
+
}, [cacheDuration]);
|
|
89
66
|
|
|
90
67
|
return (
|
|
91
68
|
<AuthorizationContext.Provider value={{ can, refresh }}>
|