@majapisoftwares/ui 6.10.0 → 6.10.1
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/docs/STANDARDS.md
CHANGED
|
@@ -91,4 +91,4 @@ Uma página tem um jeito só de subir: o breadcrumb do `AppShell`. Nunca dois co
|
|
|
91
91
|
|
|
92
92
|
## Dados ao vivo
|
|
93
93
|
|
|
94
|
-
Listas e telas com dados se atualizam sozinhas: `useLiveRefresh(load, ms)` (`hooks/use-live-refresh`) chama `load` a cada `ms` (5 s por padrão) com a página visível e logo que a pessoa volta para ela. Não há botão de atualizar. Leituras caras usam um intervalo maior (um scan na nuvem a cada minutos, não segundos), nunca um botão. Tentar de novo depois de uma falha (`onRetry`) continua, porque é outra coisa.
|
|
94
|
+
Listas e telas com dados se atualizam sozinhas: `useLiveRefresh(load, ms)` (`hooks/use-live-refresh`) chama `load` a cada `ms` (5 s por padrão) com a página visível e logo que a pessoa volta para ela. `load` devolve a promise da carga (`useLiveRefresh(load)`, não `() => void load()`): enquanto ela roda, os próximos ticks esperam, e um servidor lento não recebe a mesma requisição empilhada. `npm run test:live-refresh` verifica. Não há botão de atualizar. Leituras caras usam um intervalo maior (um scan na nuvem a cada minutos, não segundos), nunca um botão. Tentar de novo depois de uma falha (`onRetry`) continua, porque é outra coisa.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@majapisoftwares/ui",
|
|
3
|
-
"version": "6.10.
|
|
3
|
+
"version": "6.10.1",
|
|
4
4
|
"description": "Shared Majapi UI and UX standards based on shadcn/ui",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
@@ -47,6 +47,7 @@
|
|
|
47
47
|
"test:chat-markdown": "tsx --test scripts/chat-markdown.test.tsx",
|
|
48
48
|
"test:ai-elements": "tsx --test scripts/ai-elements.test.tsx",
|
|
49
49
|
"test:navigation": "tsx --test scripts/navigation.test.tsx",
|
|
50
|
+
"test:live-refresh": "tsx --test scripts/live-refresh.test.tsx",
|
|
50
51
|
"verify": "node scripts/verify.mjs"
|
|
51
52
|
},
|
|
52
53
|
"peerDependencies": {
|
|
@@ -32,6 +32,9 @@ export function navigationViolations(text) {
|
|
|
32
32
|
found.push("the breadcrumb names the page; keep its h1 for screen readers (sr-only)");
|
|
33
33
|
if (refreshButton(text))
|
|
34
34
|
found.push("no refresh buttons; keep the screen current with useLiveRefresh (@majapisoftwares/ui/hooks/use-live-refresh)");
|
|
35
|
+
// `() => void load()` drops the promise: the hook can't hold the next ticks while it runs.
|
|
36
|
+
if (/\buseLiveRefresh\(\s*\(\)\s*=>\s*void\b/.test(text))
|
|
37
|
+
found.push("useLiveRefresh: return the load's promise (useLiveRefresh(load) or () => load()), so a slow load isn't requested again while it runs");
|
|
35
38
|
return found;
|
|
36
39
|
}
|
|
37
40
|
|
|
@@ -6,15 +6,29 @@ import { useEffect, useRef } from "react";
|
|
|
6
6
|
* Keeps a screen current, so it never needs a refresh button: calls `refresh`
|
|
7
7
|
* every `ms` while the page is visible, and right away when the person comes
|
|
8
8
|
* back to it (focus or a visible tab). The first load stays with the caller.
|
|
9
|
+
*
|
|
10
|
+
* Return the load's promise (`useLiveRefresh(load)` or `() => load()`): while
|
|
11
|
+
* it runs, the next ticks are skipped, so a slow server never gets the same
|
|
12
|
+
* request piled up. A refresh that returns nothing runs on every tick.
|
|
9
13
|
*/
|
|
10
|
-
export function useLiveRefresh(refresh: () =>
|
|
14
|
+
export function useLiveRefresh(refresh: () => unknown, ms = 5000) {
|
|
11
15
|
const latest = useRef(refresh);
|
|
12
16
|
useEffect(() => {
|
|
13
17
|
latest.current = refresh;
|
|
14
18
|
});
|
|
15
19
|
useEffect(() => {
|
|
20
|
+
let running = false;
|
|
16
21
|
const run = () => {
|
|
17
|
-
if (
|
|
22
|
+
if (document.hidden || running) return;
|
|
23
|
+
const result = latest.current();
|
|
24
|
+
if (result && typeof (result as PromiseLike<unknown>).then === "function") {
|
|
25
|
+
running = true;
|
|
26
|
+
Promise.resolve(result)
|
|
27
|
+
.catch(() => undefined)
|
|
28
|
+
.finally(() => {
|
|
29
|
+
running = false;
|
|
30
|
+
});
|
|
31
|
+
}
|
|
18
32
|
};
|
|
19
33
|
const timer = setInterval(run, ms);
|
|
20
34
|
document.addEventListener("visibilitychange", run);
|