generator-pninja 2.0.2 → 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, useState, useCallback, useRef, ReactNode } from 'react';
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 = 1 * 1000,
23
+ cacheDuration = 5 * 60 * 1000,
24
24
  }) => {
25
- const [cache, setCache] = useState<Map<string, CacheEntry>>(new Map());
26
- const pendingRequestsRef = useRef<Map<string, Promise<boolean>>>(new Map());
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
- setCache(new Map());
34
- pendingRequestsRef.current = new Map();
29
+ cacheRef.current = new Map();
30
+ pendingRef.current = new Map();
35
31
  }, []);
36
32
 
37
- const can = useCallback(
38
- async (resource: string, action: string): Promise<boolean> => {
39
- const key = getCacheKey(resource, action);
40
- const now = Date.now();
41
-
42
- // 1. Controlla cache
43
- const cached = cache.get(key);
44
- if (cached && now - cached.timestamp < cacheDuration) {
45
- return cached.allowed;
46
- }
47
-
48
- // 2. Controlla se c'è già una richiesta in corso (SINCRONO con ref)
49
- const pending = pendingRequestsRef.current.get(key);
50
- if (pending) {
51
- return pending;
52
- }
53
-
54
- // 3. Crea nuova richiesta
55
- const request = axios
56
- .post<{ allowed: boolean }>('/api/user/can', {
57
- resource,
58
- action,
59
- })
60
- .then((response) => {
61
- const allowed = response.data.allowed;
62
-
63
- // Aggiorna cache
64
- setCache((prev) =>
65
- new Map(prev).set(key, {
66
- allowed,
67
- timestamp: now,
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 }}>
@@ -11,7 +11,7 @@ createRoot(document.getElementById('root')!).render(
11
11
  <StrictMode>
12
12
  <BrowserRouter>
13
13
  <AuthProvider>
14
- <AuthorizationProvider cacheDuration={1 * 1000}>
14
+ <AuthorizationProvider cacheDuration={60 * 1000}>
15
15
  <App />
16
16
  </AuthorizationProvider>
17
17
  </AuthProvider>
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "generator-pninja",
3
- "version": "2.0.2",
3
+ "version": "2.0.4",
4
4
  "description": "Generator for PHP/Laravel + SQL + Vite + React applications",
5
5
  "type": "module",
6
6
  "files": [